前言
对于用AndroidStudio的同学来说,是不是看到那个 cpp
的文件夹开始浮想联翩。知道Android的NDK开发,但是一直举步维艰。下面我介绍一下一个比hallo world更深一点的JNI开发,同时提供一个C的json库编译so来爽爽。
默认的AndroidStudio的JNI工程学习
如果你对AndroidStudio感兴趣,你一定建立过一个AndroidStudio默认的JNI工程,可以通过New一个Project进行选择 include C++ support
,之后next到底。
这个工程有很多是值得我们学习的。譬如,对于 CMakeLists.txt
的编写等。首先默认的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.
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} )
精简后为
cmake_minimum_required(VERSION 3.4.1)
add_library()
find_library()
target_link_libraries( )
就是这四句话而已。有英文解析。不懂也没有关系,但是可以依样画葫芦。
画葫芦
这里我们可以去下载一个c解析json的库。下载地址https://sourceforge.net/projects/cjson/ 下载后,看到文件夹中的json
就拷贝出来,才两个文件,放到我们的cpp目录下如图
问题
- 怎么编译cjson的代码?
- java如何使用c代码?
- native方法如何生成头文件?
- gradle文件是如何关联CMakeLists.txt?
回答
对于以上问题,或多或少大家都可以给一个答案,但是如何实操呢?其实不难,按照问题来一步一步解答。
怎么编译cjson代码
在默认工程中我们已经知道了编译主要通过CMakeLists.txt文件。只有我们依样画葫芦编写了CMakeLists.txt如下
#JOSON.so
add_library(
#库的名字
JSON
SHARED
#源文件
src/main/cpp/json/cjson.c
src/main/cpp/json/cjson.h
)
find_library(
log-lib
log
)
target_link_libraries(
JSON
${log-lib}
)
在这里我们吧h文件都加入了,其实可以忽略。我们编译一下工程发现下面有一个os了如图,图中的libJSON.so
是生成的。其他的是我写的别的库,这里忽略。主要要知道对于各种平台CPU的so类型。
到这里第一个问题解决了。
java如何使用c代码和生成头文件
同理,我们通过hello world的默认工程知道了我们要写一个native的方法来调用。因此,我也写一个JNIjsonUtil.java
代码如下
public class JNIJSONUtil {
static {
//加载库
System.loadLibrary("JSON");
}
//native方法
public native String getDefaultJson();
}
发现我们的native方法是红色的。我们通过alt+enter就发现AndroidStudio已经帮我们生成了一个方法在c的里.如下
extern "C"
JNIEXPORT jstring JNICALL
Java_com_richdataco_testos_util_JNIJSONUtil_getDefaultJson(JNIEnv *env, jobject instance) {
}
之后我们就开始使用cjson的方法(导入头文件后),代码如下
//
// Created by aven on 2017/7/6.
//
#include <jni.h>
#include <string>
#include "../json/cJSON.h"
extern "C"
JNIEXPORT jstring JNICALL
Java_com_richdataco_testos_util_JNIJSONUtil_getDefaultJson(JNIEnv *env, jobject instance) {
cJSON *pJson = NULL;
pJson = cJSON_CreateObject();
if (NULL == pJson) {
return 0;
}
cJSON_AddStringToObject(pJson, "name", "Ola");
cJSON_AddNumberToObject(pJson, "age", 25);
cJSON *subJson = NULL;
subJson = cJSON_CreateObject();
if (NULL == subJson) {
cJSON_Delete(subJson);
return 0;
}
cJSON_AddStringToObject(subJson, "school_name", "AIB");
cJSON_AddStringToObject(subJson, "school_room", "503");
cJSON_AddItemToObject(pJson, "school", subJson);
const char *result = cJSON_Print(pJson);
return env->NewStringUTF(result);
}
值得我们注意的是env->NewStringUTF(result)
这里返回了一个jstring。
写一个方法调用一下native方法发现运行起来了。。。
gradle文件关联CMakeLists.txt
我们发现build.gradle的文件只是多了这些代码
//如果ndk这个文件没有他就编译所有的平台
ndk {
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a'
}
externalNativeBuild {
cmake {
//这里是编译的解析器,如果没有写就用默认的编译
cppFlags "-std=c++11"
}
}
externalNativeBuild {
//关联CMakeLists文件
cmake {
path "CMakeLists.txt"
}
}
总结
关键是知道CMakeLists.txt的编写和native方法,其他都是C了,对于C要转为java对应的jstring才能使用。希望大家有用。