NDK开发之------Error: Flag android.useDeprecatedNdk is no longer supported爬坑

来源:互联网 发布:网络大电影预算表 编辑:程序博客网 时间:2024/06/08 17:42

先把完整的错误日志贴上:

Error:Execution failed for task ':app:compileDebugNdk'.> Error: Flag android.useDeprecatedNdk is no longer supported and will be removed in the next version of Android Studio.  Please switch to a supported build system.  Consider using CMake or ndk-build integration. For more information, go to:   https://d.android.com/r/studio-ui/add-native-code.html#ndkCompile   To get started, you can use the sample ndk-build script the Android   plugin generated for you at:   E:\androidproject\JniDemo2\app\build\intermediates\ndk\debug\Android.mk  Alternatively, you can use the experimental plugin:   https://developer.android.com/r/tools/experimental-plugin.html  To continue using the deprecated NDK compile for another 60 days, set   android.deprecatedNdkCompileLease=1511489294355 in gradle.properties



其实比较好理解,就是useDeprecatedNdk这个东西不再支持了,请切换到 CMake or ndk-build来编译Jni的C换C++代码。很好理解但是不知道说的有什么用,怎么解决也不知道。这个情况发生在自己使用NDK进行开发时,没有选择包含C/C++支持,Android studio没有自动生成对CMake or ndk-build的支持,当你编译的时候就报这个错了。好了,下面是解决的办法,是比较笨的办法,照葫芦画瓢来解决。

1、先看下目前可以正常运行的工程包含哪些文件和目录


2、build.gradle文件

android {    compileSdkVersion 26    defaultConfig {        applicationId "wentsai.hisign.com.jnidemo2"        minSdkVersion 14        targetSdkVersion 26        ndk {            moduleName "JniDemo2" //设置库(so)文件名称            // 设置支持的SO库架构            abiFilters  'armeabi-v7a', 'arm64-v8a'            ldLibs "log"//            ,'x86_64','x86''armeabi',        }        externalNativeBuild {            cmake {                cppFlags ""            }        }        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }    sourceSets.main {        jniLibs.srcDirs = ['src/main/libs'];    }    externalNativeBuild {        cmake {            path "CMakeLists.txt"        }    }}dependencies {    implementation fileTree(include: ['*.jar'], dir: 'libs')    implementation 'com.android.support:appcompat-v7:26.1.0'    implementation 'com.android.support.constraint:constraint-layout:1.0.2'    testImplementation 'junit:junit:4.12'    androidTestImplementation 'com.android.support.test:runner:1.0.1'    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'    implementation project(':TestLibrary')}

3、gradle.properties文件

# Project-wide Gradle settings.# IDE (e.g. Android Studio) users:# Gradle settings configured through the IDE *will override*# any settings specified in this file.# For more details on how to configure your build environment visit# http://www.gradle.org/docs/current/userguide/build_environment.html# Specifies the JVM arguments used for the daemon process.# The setting is particularly useful for tweaking memory settings.org.gradle.jvmargs=-Xmx1536m# When configured, Gradle will run in incubating parallel mode.# This option should only be used with decoupled projects. More details, visit# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects# org.gradle.parallel=true#android.useDeprecatedNdk=true   

4、local.properties文件

## This file is automatically generated by Android Studio.# Do not modify this file -- YOUR CHANGES WILL BE ERASED!## This file must *NOT* be checked into Version Control Systems,# as it contains information specific to your local configuration.## Location of the SDK. This is only used by Gradle.# For customization when using a Version Control System, please read the# header note.#Fri Nov 24 09:16:12 CST 2017ndk.dir=C\:\\Users\\hongzhen\\AppData\\Local\\Android\\Sdk\\ndk-bundlesdk.dir=C\:\\Users\\hongzhen\\AppData\\Local\\Android\\Sdk

5、CMake.list文件

# For more information about using CMake with Android Studio, read the# documentation: https://d.android.com/studio/projects/add-native-code.html# Sets the minimum version of CMake required to build the native library.cmake_minimum_required(VERSION 3.4.1)# Creates and names a library, sets it as either STATIC# or SHARED, and provides the relative paths to its source code.# You can define multiple libraries, and CMake builds them for you.# Gradle automatically packages shared libraries with your APK.add_library( # Sets the name of the library.             native-lib             # Sets the library as a shared library.             SHARED             # Provides a relative path to your source file(s).             src/main/cpp/native-lib.cpp )# Searches for a specified prebuilt library and stores the path as a# variable. Because CMake includes system libraries in the search path by# default, you only need to specify the name of the public NDK library# you want to add. CMake verifies that the library exists before# completing its build.find_library( # Sets the name of the path variable.              log-lib              # Specifies the name of the NDK library that              # you want CMake to locate.              log )# Specifies libraries CMake should link to your target library. You# can link multiple libraries, such as libraries you define in this# build script, prebuilt third-party libraries, or system libraries.target_link_libraries( # Specifies the target library.                       native-lib                       # Links the target library to the log library                       # included in the NDK.                       ${log-lib} )

6、native-lib.cpp文件,这个文件是新建工程时,如果包含C的支持,自动生成的,内容不重要

#include <jni.h>#include <string>extern "C"JNIEXPORT jstring JNICALLJava_com_hisign_wentsai_data2ga_MainActivity_stringFromJNI(        JNIEnv* env,        jobject /* this */) {    std::string hello = "Hello from C++";    return env->NewStringUTF(hello.c_str());}

好了,总结一下,按照上面的内容,全部补齐到你的工程应该可以解决问题,除了Jni目录下的自己的C文件。


问题:

1、native-lib.cpp这个文件到底到底用?

其实没有卵用,就是为了不让CMake.list文件报错,请看CMake.list文件包含native-lib.cpp这个文件的引用,我有懒得改,所以这个文件直接拷过来,不报错就行。

add_library( # Sets the name of the library.             native-lib             # Sets the library as a shared library.             SHARED             # Provides a relative path to your source file(s).             src/main/cpp/native-lib.cpp )

2、Android.mk和Application.mk这两个文件的配置,不是本文讨论的关键,自行上网百度,但是这两个文件很重要,没有这两个文件,是无法生成so库的

3、如何生成so库?

使用ndk-build命令来生成



4、为什么要有TestLibrary这样库?

生成so库,是为了调用,必须配合一个jar包,因为native方法必须对应Java的包和调用类名,所以必须有库来协助。


代码地址:https://github.com/qiantanlong/JniDemo2



阅读全文
0 0
原创粉丝点击