为本机 Android 项目构建 apk 时目标不兼容

incompatible target while building apk for Native Android Project

我正在尝试在 Android 中实施 Alexa 语音服务 SDK。 我在我的 Android 项目中使用了预构建库。 我在 src/main/jniLibs/armeabi-v7a/*.so 下有 .so 个文件 我可以为 x86 平台成功编译和构建 apk,但我的目标是 运行 它在 armeabi-v7a 设备上。

但是在生成 apk 时我遇到了目标不兼容的错误。

完整错误如下所示,

FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing process /home/ajinkya/Androidsdk/cmake/3.10.2.4988404/bin/cmake with arguments {--build /root/AndroidStudioProjects/alexaProtoApp/app/.externalNativeBuild/cmake/debug/armeabi-v7a --target native-lib}
[1/2] Building CXX object CMakeFiles/native-lib.dir/native-lib.cpp.o
[2/2] Linking CXX shared library /root/AndroidStudioProjects/alexaProtoApp/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so
 FAILED: /root/AndroidStudioProjects/alexaProtoApp/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so 
 : && /home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi24 --gcc-toolchain=/home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -march=armv7-a -mthumb -Wa,--noexecstack -Wformat -Werror=format-security   -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -shared -Wl,-soname,libnative-lib.so -o /root/AndroidStudioProjects/alexaProtoApp/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o  /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/../../main/jniLibs/armeabi-v7a/libSampleApp.so /home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/24/liblog.so /home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/24/liblog.so -latomic -lm && :
 /home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/../../main/jniLibs/armeabi-v7a/libSampleApp.so: incompatible target
 /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:25: error: undefined reference to 'my_initialize(_JavaVM*, _jobject*)'
 /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:31: error: undefined reference to 'jni_wakeAlexa()'
 /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:37: error: undefined reference to 'jni_wakeWithHoldAlexa()'
 /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:43: error: undefined reference to 'jni_resetAlexa()'
 /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:49: error: undefined reference to 'jni_reauthorizeAlexa()'
 /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:55: error: undefined reference to 'jni_quitAlexa()'
 /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:61: error: undefined reference to 'jni_infoAlexa()'
 clang++: error: linker command failed with exit code 1 (use -v to see invocation)
 ninja: build stopped: subcommand failed.

我的build.gradle喜欢,

apply plugin: 'com.android.application'

android {

compileSdkVersion 29
defaultConfig {

    applicationId "my.project.vdi"
    minSdkVersion 24
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    multiDexEnabled true

    externalNativeBuild {
        cmake {

            cppFlags ""
           // abiFilters "armeabi-v7a"
           //     arguments "-DANDROID_ARM_MODE=arm" , "-DANDROID_TOOLCHAIN=clang"

        }
    }

    ndk {
        // Specifies the ABI configurations of your native
        // libraries Gradle should build and package with your APK. , x86 , armeabi-v7a
        abiFilters "armeabi-v7a"
    }

 }

  buildTypes {

    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

  externalNativeBuild {
     cmake {
        path "src/main/cpp/CMakeLists.txt"
        version "3.10.2"
     }
  }

  sourceSets {
     main {
        jni.srcDirs = []
        // let gradle pack the shared library into apk
        jniLibs.srcDirs = ['src/main/jniLibs/']
    }
  }

 //    packagingOptions {
 //        exclude 'lib/arm64-v8a/libnative-lib.so'
 //        exclude 'lib/x86_64/libnative-lib.so'
 //        exclude 'lib/x86/libnative-lib.so'
 //    }

 }

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.squareup.picasso:picasso:2.5.2'

}

还有 CMakeLists.txt

 cmake_minimum_required(VERSION 3.4.1)

 add_library( # Sets the name of the library.
    native-lib

    SHARED

    native-lib.cpp)

 add_library(SampleApp SHARED IMPORTED)

 set_target_properties(SampleApp PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libSampleApp.so)

 find_library(
    log-lib
    log)

 find_library(ANDROID_LOG_LIB log)

 target_link_libraries( 
    native-lib
    SampleApp
    ${ANDROID_LOG_LIB}
    ${log-lib} )

请指导我解决这个问题。

.../armeabi-v7a/libSampleApp.so: incompatible target

该库实际上并不是为 armeabi-v7a 构建的。下面应该告诉你它是什么:

$ file /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/../../main/jniLibs/armeabi-v7a/libSampleApp.so

下面是 NDK 中 libc++_shared.so 的示例输出:

$ file android-ndk-r21-canary/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/libc++_shared.so
android-ndk-r21-canary/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/libc++_shared.so: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, BuildID[sha1]=4dc00a833014c1ddd2e1ec64c5dd72237c4179ee, with debug_info, not stripped

有趣的部分是:

ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV)

我找到了解决方案,这对我来说很不寻常,编译时生成的 .so 文件没有被替换。旧版本的文件没有被新版本更新。我删除了文件夹然后编译,生成的新文件是正确的。