Android studio 3.0 and Gradle3.0 JNI

一、最近更新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又是什么呢?(面试宝典来了,赶紧掏出小本本)

  1. NDK是一系列工具的集合
  2. NDK提供了一份稳定、功能有限的API头文件声明
  3. NDK的发布,使“Java+C”的开发方式终于转正,成为官方支持的开发方式
  4. NDK将使Android平台支持C开发的开端
三、举个例子,(编写android 串口库)

1、工具:Android studio3.0 


2、新建工程


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


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


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

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

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


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

2.3.3 生成头文件


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

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


2.5 编写相应的接口函数


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

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


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文件的名字
    }
  }

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




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”

最后,借用大神一句原话:

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


OK, 记录到此为止。

猜你喜欢

转载自blog.csdn.net/hktkfly6/article/details/79593044