Android-studio生成.so库

前奏:网上大多数资料,用的都是android.useDeprecatedNdk=true这种方法。这是gradle3.0以前生成方式,gradle3.0以后会报错。


报错的关键信息是:

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: 
/Users/apple/Desktop/AndroidJNITest/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=1512283120054 in gradle.properties

提示中的在gradle.properties 

android.deprecatedNdkCompileLease = 1512283120054根本不起作用。垃圾!

好了,不废话。上解决方式:

1.先通过SDKManager下载:CMake和LLDB 



2.在app的build.gradle添加cmake和externalNativeBuild配置,如图



3.新建一个CMakeLists.txt文件到build.gradle文件同级目录。内容如图


4 CMakeLists.txt文件内容如下

cmake_minimum_required(VERSION 3.4.1)



add_library( # Sets the name of the library. 
# 设置so文件名称. 
jniutil
# Sets the library as a shared library. 
SHARED 
# 设置这个so文件为共享. 
# Provides a relative path to your source file(s). 
# 设置这个so文件为共享. 
src/main/jni/jniutil.c)


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 )

target_link_libraries( # Specifies the target library. 
# 制定目标库. 
jniutil
# Links the target library to the log library 
# included in the NDK. 
${log-lib} ) 

ok,运行项目。


成功完成jni调用,成功生成.so文件。目录如图:



如果还有不懂,下载源码一看便知:

源码链接 https://download.csdn.net/download/micotale/10454647

有问题加qq:963345050

猜你喜欢

转载自blog.csdn.net/micotale/article/details/80549513