Android gradle,用于不同架构的本机库

时间:2016-01-15 12:48:38

标签: android gradle java-native-interface native

我正在尝试为所有架构构建通用 apk。这是我的项目结构:

-App
    -appModule
    -libraryModule
         -libs
             -armeabi
                 -lib.so
         -src
             -java
             -jni

这是libraryModule的gradle文件:

apply plugin: 'com.android.library'

//    http://stackoverflow.com/questions/28485309/how-to-build-single-apk-with-andoid-ndk-and-gradle


android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        ndk {
            moduleName "ProxyResolver" // <-- This is the name of AndroidProxy native module
            stl "gnustl_shared"
            cFlags "-std=c++11"
            abiFilters = ['armeabi']
            ldLibs (projectDir.absolutePath + "/libs/armeabi/libresolver.so")
        }
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

正如您所见,库的路径是硬编码。现在它在arm-v7处理器上工作。但我需要添加对x86 的支持而不添加风味

2 个答案:

答案 0 :(得分:2)

我想:

ndk {

    moduleName "resolver"
    stl "gnustl_shared"
    cFlags "-std=c++11"
    abiFilters = ['armeabi','arm-v7']
}

并删除

sourceSets {
    main {
        jniLibs.srcDirs = ['libs']
    }
}

答案 1 :(得分:0)

Setting abiFilters in your defaultConfig only adds information to the app's capabilities (only useful for the Installer, i.e. the Installer gives an error when the device you try to install to is not compatible) and does not change which binaries are added to the final apk, see also https://stackoverflow.com/a/39445685/1827156.

I guess what you want to accomplish is to have different apk for different architectures. This is a common usecase, i.e. to reduce build sizes. You can accomplish this by using splits in your app's build.gradle. Please refer to https://developer.android.com/studio/build/configure-apk-splits.html.

I highly recommend https://www.neotechsoftware.com/blog/native-libraries-part-i-common-pitfalls (I'm not the author).

Note on working with libraries/aars: However, there's one major pitfall when you work with libraries (aar instead of apk). Imho, you can't (automagically) create different ABI-specific aars. Instead, you create aars containing the binaries for all ABIs. When adding the aar to an android app, you specify splits in the apps build.gradle as mentioned above.