【问题标题】:Android Studio JNI build Error with cmakeAndroid Studio JNI 使用 cmake 构建错误
【发布时间】:2019-08-11 02:44:06
【问题描述】:

我想用 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\3.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\3.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\3.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'

ninja:构建停止:子命令失败。

【问题讨论】:

    标签: android android-studio cmake ndk-build


    【解决方案1】:

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

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

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

    文件/项目结构/SDK位置

    我希望它对你有用。

    【讨论】:

      【解决方案2】:

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

      1. 将新项目创建为 Native C++。
      2. 插入您的代码,然后重试。

      这解决了我的问题

      【讨论】:

      • 我用 Native C++ 创建了新项目,但我得到了同样的错误。
      猜你喜欢
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      • 2013-07-04
      • 2014-05-08
      相关资源
      最近更新 更多