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

时间:2019-07-09 15:33:41

标签: android cmake android-ndk java-native-interface native

我正在尝试在Android中实现Alexa语音服务SDK。 我有预构建的库,正在我的Android项目中使用。 我在.so下有src/main/jniLibs/armeabi-v7a/*.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} )

请指导我解决此问题。

2 个答案:

答案 0 :(得分:1)

.../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位LSB共享对象,ARM,EABI5版本1(SYSV)

答案 1 :(得分:0)

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