-
在Eclipse环境下进行JNI环境配置非常的复杂,需要记忆的东西很多,相比较,IDE变为AS之后整个过程都变的更简单,AS2.2版本发布后让我觉得很有用的更新内容之一就是NDK的支持更加便捷,之前的NDK使用需要在Android.mk等文件中进行诸多配置,在java代码中写明了native方法之后要生成cpp文件示例依赖于在终端中编写的代码(这一块我没有做过只是了解可能说明有错误的地方,等我看了这块回来改正)
现在在AS2.2中进行JNI开发更容易,它将原先的jni文件夹取消,取而代之的是cpp文件夹,并且进行编译的方式也改成了cmake。
首先简要介绍一下一个新的工程,怎么支持AS2.2的新特性
第一步:在AS中下载cmake等工具
tools->android->sdkManager里面勾选CMake、LLDB、NDK这三项,在安装完成之后AS会自动的配好NDK的路径,如果想手动设置可以在项目上app右键选择open module setting里面的sdk设定NDK路径

第二步:新建工程,并勾选对C++的支持选项

下面全部next一路到底,在最后一步可以选择C++规范,这里我选择默认不改变

然后就可以finish了,创建好项目之后的项目结构是这样的:

(Android结构)

(project结构)
说明一下,cpp文件夹是由工程创建的时候勾选的支持C++来完成的,cmakeList.txt也是,如果是自己的老项目,需要手动的建,我认为比较好的方法就是新建一个支持C++的新项目,然后对照着在老项目的project视图里建立cpp和cmakelist.txt,还可以直接把cmakelist里的内容粘贴过来。
第三步:编写(更改)cmakeList的内容
为了要会写或者说会改,首先看一下这个文件里有哪些配置的内容
-
# Sets the minimum version of CMake required to build the native
-
# library. You should either keep the default value or only pass a
-
# value of 3.4.0 or lower.
-
-
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 it 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).
-
# Associated headers in the same location as their source
-
# file are automatically included.
-
src/main/cpp/native-lib.cpp )
-
-
# Searches for a specified prebuilt library and stores the path as a
-
# variable. Because system libraries are included 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 the
-
# 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)#说明要求的cmake最低版本
-
add_library( #这里三行从下往上看分别是源文件、生成库类型、生成库名称
-
native-lib
-
SHARED
-
src/main/cpp/native-lib.cpp )
-
find_library( #这里是找到两个库
-
log-lib
-
log )
-
target_link_libraries( #这里链接了我们上面生成的库
-
native-lib
-
${log-lib} )
第四步:module里的gradle编写(改写)
-
apply plugin: 'com.android.application'
-
-
android {
-
compileSdkVersion 25
-
buildToolsVersion "25.0.1"
-
defaultConfig {
-
applicationId "cn.ndk.jni1"
-
minSdkVersion 22
-
targetSdkVersion 25
-
versionCode 1
-
versionName "1.0"
-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
-
externalNativeBuild {
-
cmake {
-
cppFlags ""
-
}
-
}
-
}
-
buildTypes {
-
release {
-
minifyEnabled false
-
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
-
}
-
}
-
externalNativeBuild {
-
cmake {
-
path "CMakeLists.txt"
-
}
-
}
-
}
-
-
dependencies {
-
compile fileTree(dir: 'libs', include: ['*.jar'])
-
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
-
exclude group: 'com.android.support', module: 'support-annotations'
-
})
-
compile 'com.android.support:appcompat-v7:25.0.1'
-
testCompile 'junit:junit:4.12'
-
}
其中要注意到的地方我用红框框下来了(或者截图了下面一个忘记画框了尴尬)


最后看一下整个调用过程,基本和没有改变编译方式之前是一样的,首先是在java代码里面写一个静态块,用来加载lib库
-
// Used to load the 'native-lib' library on application startup.
-
static {
-
System.loadLibrary("native-lib");
-
}
然后是申明一下要调用这个库里的底层函数的名字同名的方法
-
/**
-
* A native method that is implemented by the 'native-lib' native library,
-
* which is packaged with this application.
-
*/
-
public native String stringFromJNI();
最后是在java代码里调用这个方法就可以了,具体的函数实现交给cpp文件里面去写
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
// Example of a call to a native method
-
TextView tv = (TextView) findViewById(R.id.sample_text);
-
tv.setText(stringFromJNI());//这一行调用了这个底层函数对应的方法
-
}
然后具体的这个函数会在cpp里面实现
-
#include <jni.h>
-
#include <string>
-
-
extern "C"
-
jstring
-
Java_cn_ndk_jni1_MainActivity_stringFromJNI(
-
JNIEnv *env,
-
jobject /* this */) {
-
std::string hello = "Hello from C++";
-
return env->NewStringUTF(hello.c_str());
-
}
这里可以看到方法的后半段和java的方法名字是一样的,这个函数的名字和基本内容是可以自动生成的,这一块留到下面讲,集中说一下解决的问题和可以使用的技巧。
解决的问题和学到的技巧:
1.想到的第一个问题就是如果我要自己往里面加cpp怎么办,毕竟上面都是点点点没有自己建一个cpp啊,那自己建cpp要怎么做呢
首先建文件这些操作我建议到project视图里去做,因为这个视图才是真正的项目文件结构,如果出现了建了文件之后没有编写gradle和cmakelist没有刷新android结构的时候可以看到文件在哪里,心里踏实,然后可以看到在src/main/下面有cpp文件夹,我们自己建立的cpp就放在这里面,我这里以一个test.cpp为例,并且给他一个头文件,头文件选项就在建立cpp文件的时候可以勾选(虽然我不用头文件主要是试一下万一以后要用),建立后是这样的

但是这时候如果你切回去会发现在android视图里还是没有这个文件夹,不截图了,看到这里的可以自己试一下,这就是第二个问题了
2.为什么我建了cpp它不见了啊?
这个问题常见于在android视图下就建立这个cpp文件的情况,这样会更直观,建完了cpp文件在cpp文件夹下面,结果什么都没有发生(最怕空气突然安静),这是因为你在cmakelist里面还没有加上这个源文件呢,这时候你要这样做:
-
cmake_minimum_required(VERSION 3.4.1)#说明要求的cmake最低版本
-
add_library( #这里三行从下往上看分别是源文件、生成库类型、生成库名称
-
native-lib
-
SHARED
-
src/main/cpp/native-lib.cpp src/main/cpp/test.cpp)
-
find_library( #这里是找到两个库
-
log-lib
-
log )
-
target_link_libraries( #这里链接了我们上面生成的库
-
native-lib
-
${log-lib} )
在源文件那里添加一下这个新加进来的cpp文件,最后回到你建立好的cpp文件编辑界面,会发现它说没有同步(sy..那个),这时候你要点击一下让它自己去同步
进来啦!完美,这个问题困扰我很久,是在网上找到的一个小伙伴的方法救了我,我一时找不到当时那个页面了,等我找到了我会把原链接贴上来,感谢这个小天使!
特别说明一下,这是把这两个cpp都合到一个库里面去了,没有生成两个库,如果是生成两个库,我做了尝试,就是直接把add_lib那里复制了一遍,并且把源文件改为新的cpp文件,把生成的库文件名字改了一下,在link那里把新的库也添加上,这样生成的项目结构是(android视图中)会有两个子文件夹,都是shared的库,然后每个文件夹下面有一个cpp,分别是这两个库引入的cpp文件,说的罗嗦,看代码和图
-
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).
-
# Associated headers in the same location as their source
-
# file are automatically included.
-
src/main/cpp/native-lib.cpp )
-
add_library( # Sets the name of the library.
-
mylib
-
-
# Sets the library as a shared library.
-
SHARED
-
-
# Provides a relative path to your source file(s).
-
# Associated headers in the same location as their source
-
# file are automatically included.
-
src/main/cpp/test.cpp )
-
target_link_libraries( # Specifies the target library.
-
native-lib
-
mylib
-
# Links the target library to the log library
-
# included in the NDK.
-
${log-lib} )

3.解决好了添加cpp问题,再来看cpp示例的自动生成问题,我现在有一个新的方法交返回值为String的叫做JNI的方法,怎么让软件自动生成一个对应的c++函数呢,这样做:
首先在java代码里写你的native方法名字,仿照原来的那个写就好,当然这时候肯定是标红的,毕竟你静态调用进来的库里面并没有会这么个函数对应啊,自然就调用不到了,这时候等..别方,等到这个native方法的前面出现一个红色的小灯泡(总觉得小灯泡有槽要吐)这时候点一下,就会自动创建一个cpp的标准啦,对应的参数也会帮我们填上去啦,很方便对吧。(亲测要把原先的那个直接复制下来,然后改名字之后更容易出现这个红色的小灯泡,比较奇怪,可能有什么原因)
会在cpp文件里添加这一段
-
JNIEXPORT jstring JNICALL
-
Java_cn_ndk_jni1_MainActivity_jni(JNIEnv *env, jobject instance) {
-
-
// TODO
-
-
-
return env->NewStringUTF(returnValue);
-
}
最后连上之后两边的行号栏都会有一个双向箭头,点击可以在两边切换。
我还一个更极端的情况,用来测试这种自动生成方式,首先是有两个cpp,这两个cpp分别用add_lib那里生成两个库,然后在link里面把两个库都链接上,再在java代码里用静态块把两个库都导入进来,这时候再写底层函数对应的方法,用自动生成的方法,会发现会自动生成到某一个cpp文件里去(没找到规律),这时候只要把这一段复制粘贴到我们自己想要它在的cpp里就可以了(也就是不知道自动会生成到哪里去,但是可以手动调整),亲测复制粘贴到任意的cpp里都会自动链接上去的,双向箭头都在哦,当然啊这种比较复杂的情况是后面需要考虑的,如果再出现什么问题,我会回来更正的。
最后的最后以一个比较蠢蠢的问题结束:

为什么提示我们说这段代码无效呢,明明就是c++代码,为什么还是灰色的,也不像自带的那样有编辑器的颜色呢,其实,这是写的语句没有意义造成的,只要改成这样子就好了:
test.h
-
//
-
// Created by Sli.D on 2016/12/30.
-
//
-
-
#ifndef JNI1_TEST_H
-
-
#include <iostream>
-
#define JNI1_TEST_H
-
-
#endif //JNI1_TEST_H
test.cpp

当然真正JNi的的cpp是不能这样写的,是像前面那样子的,不过问题原因找到了。
Android studio 2.2+下的JNi环境搭建就是这样,欢迎一起交流学习。