ANDROID 3.0jni,Android studio 3.0 and Gradle3.0 JNI

本文记录了Android Studio 3.0版本后,由于不再支持android.useDeprecatedNdk,如何切换到使用CMake编译JNI库的过程。包括创建C/C++代码,使用javah生成头文件,编写C接口,配置CMakeLists.txt文件,以及编译生成.so文件的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、最近更新Android studio 到3.0 版本,发现编写jni 时,报错了,错误如下:

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:

/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找到了很久,都是按照以前的方法编写和配置,应该还不会错,结果仔细查看,3.0版本的android studio 不支持

android.useDeprecatedNdk这种方式配置ndk,错误提示使用CMake 或则另外一种方式,上网找了一下,大多都是使用CMake 方式。

因此,写这个文章记录下来。

二、概念普及一下

1、我们先将写好的C/C++代码编译成对应平台的动态库(windows是.dll文件,linux是.so文件)。

2、下面我们来举个栗子:使用AndroidStudio来实现JNI

3、要实现JNI先下载NDK,那么NDK又是什么呢?(面试宝典来了,赶紧掏出小本本)

NDK是一系列工具的集合

NDK提供了一份稳定、功能有限的API头文件声明

NDK的发布,使“Java+C”的开发方式终于转正,成为官方支持的开发方式

NDK将使Android平台支持C开发的开端三、举个例子,(编写android 串口库)

1、工具:Android studio3.0

0fa598c013161eb7b68e921daf90fb77.png

2、新建工程

a43316901350c12672fa6b103e626b49.png

2.1 在src\main\java\chenxi\com 包下创建serialportjni 包

f2bf5b53dfb813a2b69846087e44247f.png

2.2 将Serialport类拷贝到serialportjni包下面,如图

86cb05a8e5515251ed95bb4f5b8ab405.png

2.3 使用javah 命令生成.h头文件

2.3.1 确认当前系统已经安装相应的JDK,并且在windows 控制台使用java -version 查看是否配置好相关的环境变量。

2.3.2 在android studio 打开控制台到相应工程目录下

368c071ce76b27fd798dca98ed3c8a14.png

* javah

* javah native方法声明的java类的全类名

2.3.3 生成头文件

0cb54a05c837e63e092a1314237c8123.png

javah -classpath . -jni chenxi.com.serialportjni.SerialPort

2.4 创建jni目录,将相应的接口 xx.h文件复制到jni目录下,并且创建同名.c文件

16cf8e4d714375ff6a03657a9f858e7f.png

2.5 编写相应的接口函数

757278ca2eff760f89a06fa32ed246bb.png

三、接下来编译库文件,上面一开始提到,android studio 3.0在gradle3.0 不支持android.useDeprecatedNdk.

3.1 上网看,发现需要使用CMake, 因此需要下载CMake 和 LLDB

9eac73f987106557c01befdbac543c7c.png

3.2 在工程目录下的build.gradle的defaultConfig节点中加入

// 使用Cmake工具

externalNativeBuild {

cmake {

cppFlags ""

//生成多个版本的so文件

abiFilters 'arm64-v8a','armeabi-v7a','x86','x86_64'

}

}3.3 在build.gradle的android节点中,加入:

// 配置CMakeLists.txt路径

externalNativeBuild {

cmake {

path "CMakeLists.txt" // 设置所要编写的c源码位置,以及编译后so文件的名字

}

}

d36c365f9530843eaf5cadcd81c26a55.png

3.4 添加CMakeLists.txt文件到build.gradle文件同级目录下,具体内容如下:

c9bc87298d8791a56c70d6782f03f98c.png

849a9bbc6b3f585024f216b5a2a0789f.png

f31003a53debc22387f5344948433cca.png

3.5 复制如下文本

# 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.

#CMakeLists.txt

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.

# 设置so文件名称.

serial_port

# Sets the library as a shared library.

SHARED

# 设置这个so文件为共享.

# Provides a relative path to your source file(s).

# 设置这个so文件为共享.

src/main/jni/chenxi_com_serialportjni_SerialPort.c)

# 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.

# 制定目标库.

serial_port

# Links the target library to the log library

# included in the NDK.

${log-lib} )3.6 点击菜单“Build - Make project”

18ee382efa008967e108b12565f7288b.png最后,借用大神一句原话:

至此,我们所有的流程都做完了,下面来检查一下我们的成果,见证奇迹的时候到了:

9bc110121f6015ef675a7b66b73b57e8.png

OK, 记录到此为止。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值