1.第一次创建NDK项目
使用Android studio创建NDK的步骤如下:
打开Android Studio中的File->Settings->Android SDK
打开SDK Tools tab页 选择NDK和CMake下载
下载完毕之后 看看项目的NDK路径有没有识别 如果识别即可创建C++项目
下载完毕之后点击 invalidate Caches/Restart 重启Android Studio
创建完毕 后等待sync结束即可运行项目 可以从c++层获取到一个string 如果编译不成功,去项目的local.properties查看ndk的路径是否指定 如果没有指定 则指定为下载好的ndk路径
Android Studio不同的版本下载的ndk路径不一样 较老的版本似乎下载到
C:\Users<username>\AppData\Local\Android\Sdk
android 4.1.1的话则下载到指定sdk的路径去了
我的ndk路径如下
2.将已有的普通项目转变为NDK project
如果一个项目已经创建完毕 那么我们怎么让他变成支持NDK的项目呢
2.1.指定ndk路径
打开项目的local.properties文件 加入ndk的路径
sdk.dir=C\:\\codebase\\android-sdk_60_windows
ndk.dir=C\:\\codebase\\android-sdk_60_windows\\ndk\\23.0.7272597
然后使用as中的invalid/restart 选项重启
重启完了进入项目中查看是否修改成功
如图所示即修改成功
2.2 修改moudle的build.gradle文件
加入两个节点(externalNativeBuild和externalNativeBuild) 如下注释部分
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 29
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.testrecyclerview"
minSdkVersion 24
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
//1 加入该节点
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
//2 加入该节点
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
}
2.3 创建编译文件
在src/main下创建cpp/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("testRecyclerView")
# 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.
# so文件与c++文件的关联 如下这段则是native-lib.so 关联 native-lib.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).
native-lib.cpp )
# 以下add_library是另外加的 为了测试添加一个新的c++文件
add_library( # Sets the name of the library.
native-mylib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-mylib.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.
# 最终用到哪些so文件 既可以是自己编译的 也可以是第三方so库 也可以是系统的so库
# 这里我指定了两个so库
target_link_libraries( # Specifies the target library.
native-lib
native-mylib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
2.4 创建c++文件
native-lib.cpp
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_testrecyclerview_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_testrecyclerview_MainActivity_myStringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "My String from C++";
return env->NewStringUTF(hello.c_str());
}
native-mylib.cpp
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_testrecyclerview_MainActivity_testString1(
JNIEnv *env,
jobject /* this */) {
std::string hello = "testString1 C++";
return env->NewStringUTF(hello.c_str());
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_testrecyclerview_MainActivity_testString2(
JNIEnv *env,
jobject /* this */) {
std::string hello = "testString2 C++";
return env->NewStringUTF(hello.c_str());
}
c++中的方法名取自Java文件的完整包名+类名+方法名,用下划线代替分隔符
2.5 在java文件中调用native方法
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
System.loadLibrary("native-mylib");
}
public native String myStringFromJNI();
public native String stringFromJNI();
public native String testString1();
public native String testString2();
TextView tv = findViewById(R.id.test);
tv.setText(myStringFromJNI()+stringFromJNI()+testString1()+testString2());
最终能够看到textView上的文字是从c++获取的
另外打开app 也能看到apk内部编译了两个so文件