Android Studio3.0之NDK环境搭建
最近在公司项目中需要用到串口通讯扫描头,所以需要已有项目中进行NDK开发,故将NDK环境搭建过程记录,并编写测试demo验证。
1.配置NDK环境
下载ndk需要的工具:打开SDKManager 然后选择SDK Tools
理论上以上三个都可以在这里下载并安装,如果下载失败,可以自行去浏览器下载,下载地址:https://developer.android.google.cn/ndk/downloads/
下载完成以后打开 File – Project Structure – SDK Location
如果是通过SDK Tools下载成功的,点击 Select default NDK就好了,如果是通过浏览器自行下载的,就如上图一样,需要选择NDK的安装路径
2.创建项目并开始NDK开发
- 第一步:创建一个普通项目
- 第二步:创建一个 HelloNDK.java 的一个类
public class HelloNDK {
// 动态导入 so 库
static {
System.loadLibrary("HelloNDK");
}
//创建一个 native 方法
public native static String helloWorld();
}
- 第三步:创建 C 语言文件,创建 so 库
在命令行中执行如下代码:cd app/src/main/java/
javah com.hanshow.ndkdemo.HelloNDK
生成了一个com_hanshow_ndkdemo_HelloNDK.h 文件
然后在main目录下创建cpp目录,并将上面生成的.h文件移动到该目录
com_hanshow_ndkdemo_HelloNDK.h代码如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_hanshow_ndkdemo_HelloNDK */
#ifndef _Included_com_hanshow_ndkdemo_HelloNDK
#define _Included_com_hanshow_ndkdemo_HelloNDK
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_hanshow_ndkdemo_HelloNDK
* Method: helloWorld
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_hanshow_ndkdemo_HelloNDK_helloWorld
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
注意:需要手动把入参中的第二个参数改为jobject,否则会报java.lang.UnsatisfiedLinkError: No implementation found for java.lang.String这个错误。
- 编写 c++ 实现方法
在cpp目录下创建HelloNDK.h和HelloNDK.cpp文件,并在HelloNDK.cpp编写如下代码:
#include "HelloNDK.h"
#include "com_hanshow_ndkdemo_HelloNDK.h"
JNIEXPORT jstring JNICALL Java_com_hanshow_ndkdemo_HelloNDK_helloWorld
(JNIEnv *env, jobject jobject1){
return env->NewStringUTF("hello this is a ndk test");
}
- 创建一个 CMakeLists.txt 文件
在app目录下创建CMakeLists.txt,然后复制如下代码:
# 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.
HelloNDK
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/HelloNDK.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.
HelloNDK
# Links the target library to the log library
# included in the NDK.
${log-lib} )
- 引用 CMakeLists.txt 文件
在app目录下的build.gradle中加上如下代码:
- 生成so文件
然后我们去build目录下找对应的.so文件
最后我们在MainActivity 中打印 HelloNDK的 helloWorld() 方法获取到的 String 值:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("NDK_info:" , HelloNDK.helloWorld());
}
}
然后运行,查看日志:
然后我们就查看到了我们获取的 String 了,“hello this is a ndk test”,就是我们在 C 语言文件中所返回的字符串。