Android Studio JNI 使用 cmake 构建错误

Android Studio JNI build Error with cmake

我想用 android studio 构建 JNI,以便我可以使用 .so 文件。 我为此使用了 Cmake,但是当我构建 JNI 时,出现了构建错误。

我的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.ss.raynor.nativehttprequest"
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++14"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

构建命令失败。 执行过程时出错

E:\environment\sdk\cmake.6.4111459\bin\cmake.exe with arguments {-HE:\Work\NativeHttpRequest\NativeHttpRequest\app\src\main\cpp -BE:\Work\NativeHttpRequest\NativeHttpRequest\app\.externalNativeBuild\cmake\debug\armeabi-v7a -DANDROID_ABI=armeabi-v7a -DANDROID_PLATFORM=android-18 -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=E:\Work\NativeHttpRequest\NativeHttpRequest\app\build\intermediates\cmake\debug\obj\armeabi-v7a -DCMAKE_BUILD_TYPE=Debug -DANDROID_NDK=E:\environment\sdk\ndk-bundle -DCMAKE_CXX_FLAGS=-std=c++14 -DCMAKE_TOOLCHAIN_FILE=E:\environment\sdk\ndk-bundle\build\cmake\android.toolchain.cmake -DCMAKE_MAKE_PROGRAM=E:\environment\sdk\cmake.6.4111459\bin\ninja.exe -GAndroid Gradle - Ninja}
-- Check for working C compiler: E:/environment/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
-- Check for working C compiler: E:/environment/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe -- broken
-- Configuring incomplete, errors occurred!
See also "E:/Work/NativeHttpRequest/NativeHttpRequest/app/.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/CMakeOutput.log".
See also "E:/Work/NativeHttpRequest/NativeHttpRequest/app/.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/CMakeError.log".

CMake Error at E:/environment/sdk/cmake/3.6.4111459/share/cmake-3.6/Modules/CMakeTestCCompiler.cmake:61 (message):
  The C compiler
  "E:/environment/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe"
  is not able to compile a simple test program.

  It fails with the following output:

   Change Dir: E:/Work/NativeHttpRequest/NativeHttpRequest/app/.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/CMakeTmp



  Run Build Command:"E:\environment\sdk\cmake.6.4111459\bin\ninja.exe"
  "cmTC_2ada3"

  [1/2] Building C object CMakeFiles/cmTC_2ada3.dir/testCCompiler.c.o


  FAILED:
  E:\environment\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe
  --target=armv7-none-linux-androideabi18
  --gcc-toolchain=E:/environment/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64
  --sysroot=E:/environment/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot
  -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 -fPIE -o
  CMakeFiles/cmTC_2ada3.dir/testCCompiler.c.o -c
  E:\Work\NativeHttpRequest\NativeHttpRequest\app\.externalNativeBuild\cmake\debug\armeabi-v7a\CMakeFiles\CMakeTmp\testCCompiler.c

clang.exe:错误:未知参数:'-fno-addrsig'

忍者:构建停止:子命令失败。

我有构建JNI的经验。您应该尝试以下操作

  1. 将新项目创建为本机 C++。
  2. 插入您的代码并重试。

这解决了我的问题

我认为您的错误与 NDK 版本有关。

我检查了你的 build.gradle 没有问题。

请下载 android-ndk-r16b 并设置新的 NDK 位置。

文件/项目结构/SDK 位置

希望对你有用。