上一小节已经通过Android Studio创建了一个支持NDK的项目,这小节来看看AS帮助我们写了哪些代码,与普通的通常有什么不一样。
1、从项目结构上来看,AS帮我们在main目录先创建了一个cpp的文件夹,用来存放CMake脚本,以及native源文件。
2、主Module(即app Module)的build.gradle文件也新增了一些内容。
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
}
}
-std=c++11是在创建项目时选择的C++标准。
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
这里声明了CMake的脚本路径,版本号,下面看看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.10.2)
# Declares and names the project.
project("ndktutorials")
# 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).
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} )
早期其实可以先只关注这几行:
add_library( # Sets the name of the library.
native-lib //生成so库的名字
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp ) //源码
接下来看看MainActivity
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 = 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();
}
1、声明了一个native方法 stringFromJNI(); 2、System.loadLibrary("native-lib")加载so库; 3、调用native方法
下面看看这个native方法的实现:
extern "C" JNIEXPORT jstring JNICALL
Java_com_jdqm_ndktutorials_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
jstring:返回值 com_jdqm_ndktutorials:包名点换成了下划线 MainActivity:声明native方法的类名 stringFromJNI:方法名