android - 使用 android gradle experimental 链接到 .aar 文件中的 .so 文件

标签 android gradle android-ndk gradle-experimental

我正在使用 android gradle 实验插件来构建一个带有一些 native 代码的应用程序模块。此 native 代码使用带有预构建 .so 文件的库,我通过 android 库模块将其 bundle 到 .aar 文件中。 .aar 文件构建良好,但是如何将 app 模块中的 native 代码模块链接到 .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')
}

最佳答案

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

android.enablePrefab=true
在 gradle 4.1 中:将以下内容添加到模块 build.gradle 文件的 android block 中:
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

关于android - 使用 android gradle experimental 链接到 .aar 文件中的 .so 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43989324/

相关文章:

visual-studio - 如何让 Visual Studio 2015 安装程序知道我已经拥有 Android SDK?

android - Android 上的证书固定

java - 不基于 JNI 的 Android 共享库

gradle - 如何编写gradle脚本,其中两个任务具有相同的依赖关系但配置不同

android - 如何从 Android 原生代码 (NDK r5) 访问打包资源

android - 从 github 导入 Telegram 源代码时出现 NDK 错误

java - 哪里使用 StringBuffer/StringBuilder 而不是 String

java - Android 中的内置布局是否可样式化?

eclipse-plugin - 从Eclipse运行 “gradle assemble”

java - 使用 Gradle 将 .jar 发布到私有(private) Maven