原文地址 http://blog.csdn.net/qq_25806863/article/details/70739717
从AndroidStudio2.2开始,AS就整合了CMake来使用NDK。这种方式比之前用过的简单多了。这里记录一下简单使用。
当然,这些东西要先装好:
在新项目中使用
创建
在创建新项目时可以直接选中增加JNI。
然后一路next
点击finish完成创建。这时,AS就生成了ndk开发环境.
和不带JNI的项目新的区别
- 首先看目录结构,多了个cpp文件夹和cpp文件,多了个CMakeLists.txt文件
下面是native-lib.cpp文件,返回Hello from C++
:
#include <jni.h>
#include <string>
extern "C"
JNIEXPORT jstring JNICALL
Java_com_sunlinlin_jniapp_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
下面是CMakeLists.txt:
cmake_minimum_required(VERSION 3.4.1)
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 )
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 )
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
- 然后是构建文件的配置
使用
在Activity中就正常使用:
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@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());
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
}
直接运行就是下图:
在现有项目上增加jni
其实就是照猫画虎,手动加上上面的那些文件就行了。
- 在Java中定义载入库的名字和native方法名:
public class MainActivity extends AppCompatActivity {
static{
System.loadLibrary("myjni");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(strFromJNI());
}
public native String strFromJNI();
}
- 新建myjin.cpp文件在src/main/cpp/myjni.cpp,里面实现strFromJNI这个方法。
方法名是Java_包名_方法名
,把包名的.
改成_
。
#include <jni.h>
#include <string>
extern "C"
JNIEXPORT jstring JNICALL
Java_com_sunlinlin_myapp_MainActivity_strFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from MYJNI";
return env->NewStringUTF(hello.c_str());
}
- 在app目录下建立CMakeLists.txt文件,必须说这个名字
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
myjni
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/myjni.cpp )
target_link_libraries( myjni
android
log)
- 然后在app的build.gradle文件中配置
然后运行就能得到下图:
增加cpp文件
要增加一个hisjni.cpp ,就在cpp文件夹中建立hisjni.cpp:
#include <jni.h>
#include <string>
extern "C"
JNIEXPORT jstring JNICALL
Java_com_sunlinlin_myapp_MainActivity_strFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from MYJNI";
return env->NewStringUTF(hello.c_str());
}
然后在CMakeLists.txt中增加配置:
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
myjni
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/myjni.cpp )
target_link_libraries( myjni
android
log)
add_library( # Sets the name of the library.
hisjni
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/hisjni.cpp )
target_link_libraries( hisjni
android
log)
就能在Java中使用了:
public class MainActivity extends AppCompatActivity {
static{
System.loadLibrary("myjni");
System.loadLibrary("hisjni");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(strFromHisJNI());
}
public native String strFromJNI();
public native String strFromHisJNI();
}
更多配置。
上面只是简单的使用,在build.gradle中还能有更多的灵活的配置,参考官方文档