9

我正在使用 android gradle 实验插件来构建一个带有一些本机代码的应用程序模块。此本机代码使用带有预构建 .so 文件的库,我通过 android 库模块将其捆绑到 .aar 文件中。.aar 文件构建良好,但是如何将 app 模块中的本机代码模块链接到 .aar 模块中预构建的 .so 文件?gradle 实验文档没有提到这种情况。

另外,如果可能的话,我想将包含文件打包到 .aar 文件中(尽管它们不应该与最终应用程序一起打包)。

在 /prebuiltlib 中:

build.gradle
-src/
--main/
---jniLibs/
----libfoo.so

这是gradle文件:

/prebuiltlib/build.gradle

apply plugin: "com.android.model.library"
model {
    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.3"

        defaultConfig {
            minSdkVersion.apiLevel = 21
            targetSdkVersion.apiLevel = 21
            versionCode 1
            versionName "1.0"

        }

        buildTypes {
            release {
                minifyEnabled false
                proguardFiles.add(file("proguard-rules.pro"))
            }
        }
    }
}

dependencies {
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:25.3.1"
}

这里是 /app/build.gradle,注意 ??? 我不知道该放什么:

import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'com.android.model.application'

model {
    repositories {
        libs(PrebuiltLibraries) {
            // ??? is this right, and does this go into app/build.gradle or mylib/build.gradle?
            foo {
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file('???/libfoo.so')
                }
            }
        }
    }
    android {
        compileSdkVersion = 25
        buildToolsVersion = '25.0.3'

        defaultConfig {
            applicationId = 'com.jeremy.stackoverflow.sample'
            minSdkVersion.apiLevel = 21
            targetSdkVersion.apiLevel = 21
            versionCode = 1
            versionName = '1.0'
        }

        ndk {
            platformVersion = 21
            moduleName = 'native-activity'
            toolchain = 'gcc'
            toolchainVersion = '4.9'
            stl = 'gnustl_shared'
            abiFilters.add('armeabi-v7a')
            ldLibs.addAll(['log',
                           'android',
                           'EGL',
                           'GLESv2'
            ])
            // ??? How do I add include paths from the .aar module?
            cppFlags.add('-I' + file('???/include'))
            cppFlags.addAll(['-std=c++11', '-fexceptions'])
        }

        sources {
            main {
                jni {
                    dependencies {
                        // ??? Is this right?
                        library 'foo' linkage 'shared'
                    }
                }
                jniLibs {
                    source {
                        // ??? Do I need one of these for the libs in the .aar?
                        srcDir file('???/jniLibs')
                    }
                    dependencies {
                        // ??? Is this right?
                        library 'foo'
                    }
                }
            }
        }
        buildTypes {
            release {
                minifyEnabled = false
                proguardFiles.add(file('proguard-rules.pro'))
            }
        }
    }
}

dependencies {
    println rootProject.getName()
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile project(':prebuiltlib')
}
4

1 回答 1

0

从 Android Gradle 插件 4.0 开始,可以从 build.gradle 文件中链接的 AAR 导入 C/C++ 依赖项。Gradle 会自动将这些提供给本机构建系统,但您的构建系统必须配置为使用导入的库和头文件。

在 gradle 4.0 中:将以下内容添加到项目的 gradle.properties 文件中:

android.enablePrefab=true

在 gradle 4.1 中:将以下内容添加到模块 build.gradle 文件的 android 块中:

buildFeatures {
  prefab true
}

,如果您的应用程序定义了 libapp.so 并且它使用 cURL,那么您的 CMakeLists.txt 应该包括以下内容:

add_library(app SHARED app.cpp)

# Add these two lines.
find_package(curl REQUIRED CONFIG)
target_link_libraries(app curl::curl)

app.cpp 现在能够#include "curl/curl.h",libapp.so 将在构建时自动链接到 libcurl.so,并且 libcurl.so 将包含在 APK 中。

来源:https ://developer.android.com/studio/build/native-dependencies

于 2020-11-03T15:45:18.940 回答