如何使用仅针对 ARM 目标的 NDK 构建基于 Android Gradle 的应用程序?

How to build an Android Gradle based app with NDK only for the ARM target?

我有一个来自供应商的 .so 文件,它只支持 "arm"。目前它非常适合我的 Android 应用程序。我想以某种方式使用 Android Studio 模块分离实现,因此我可以按照本教程 https://www.youtube.com/watch?v=1i4I-Nph-Cw 将模块导出为 Jar。

当我导出 JAR 时,构建过程returns出错

/Users/zoom/android-ndk-r9d/toolchains/mipsel-linux-android-4.8/prebuilt/darwin-x86_64/bin/../lib/gcc/mipsel-linux-android/4.8/../../../../mipsel-linux-android/bin/ld: skipping incompatible src/main/jniLibs/armeabi/libremote_client.so when searching for -lremote_client
/Users/zoom/android-ndk-r9d/toolchains/mipsel-linux-android-4.8/prebuilt/darwin-x86_64/bin/../lib/gcc/mipsel-linux-android/4.8/../../../../mipsel-linux-android/bin/ld: cannot find -lremote_client
collect2: error: ld returned 1 exit status

:app:linkMipsDebugRemoteDesktopSharedLibrary FAILED

FAILURE: Build failed with an exception.

日志显示 gradle 试图针对 mips 进行构建,但由于库不兼容而失败,因为我只有 arm 库。 我的问题是如何跳过针对 mips 的构建过程?或者是否可以针对 ARM 架构?

build.gradle

apply plugin: 'com.android.model.library'

model {
android {
    compileSdkVersion = 23
    buildToolsVersion = "22.0.1"

    defaultConfig.with {
        //applicationId = "com.test.remote"
        minSdkVersion.apiLevel = 19
        targetSdkVersion.apiLevel = 21
        //versionCode = 1
        //versionName = "1.0"
    }

}

android.ndk {
    moduleName = "remote_client"
    //CFlags += "-DANDROID_NDK"
    CFlags += ['-std=c99', '-fstrict-aliasing']
    ldLibs += ["log", "remoted_client"]
}

android.buildTypes {
    release {

        minifyEnabled = false
        proguardFiles += file('proguard-rules.pro')
    }
}

android.sources {
    main {
        jni {
            source {
                srcDir 'src/main/jni'
            }
        }
        jniLibs {
            source {
                srcDir 'src/main/jniLibs'
            }
        }
    }
}

android.productFlavors {
    create("arm") {
        ndk.with {
            abiFilters += "armeabi"
            ldFlags += "-Lsrc/main/jniLibs/armeabi"
        }
    }
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
}


task clearJar(type: Delete) {
delete 'mylib.jar'
}

task makeJar(type: Copy) {
   from('build/intermediates/bundles/release/')
   into('release/')
   include('classes.jar')
   rename ('classes.jar', 'mylib.jar')
}

makeJar.dependsOn(clearJar, build)

只需将 abiFilters 移动到 android.ndk 部分:

model {
    android.ndk {
        moduleName = "remote_client"
        CFlags += ['-std=c99', '-fstrict-aliasing']
        ldLibs += ["log", "remoted_client"]
        abiFilters = ['armeabi']
    }
}

我终于成功了。 这是禁用特定任务的示例。 在您的 build.gradle

中添加此行
tasks.getByPath(":app:linkMipsDebugRemoteDesktopSharedLibrary").enabled = false

已在 Android SDK 26、NDK 15.2

上测试

关于文件 app/build.gradle:

android {
    defaultConfig {
        ndk {
            abiFilters 'arm64-v8a'

将仅为 arm64-v8a 或所有当前未弃用的 ARM 目标构建:

abiFilters 'arm64-v8a', 'armeabi-v7a'

ABI 列表当前位于:https://developer.android.com/ndk/guides/abis.html

在 Ubuntu 17.10 主机、Android Studio 3、Android SDK 26、NDK 15.2 和 Android 6.0.1 设备上测试。

示例项目on GitHub