NDK 开发流程
25.JNI_Android Studio下的 NDK 环境配置
26?JNI_快速开发
参考文档: 1 ,https://developer.android.google.cn/ndk/guides
操作流程一
1 :参考上述视频或者文档,先安装了NDK和cmake
? ? 1.1??安装NDK
? ? ? ? ? ? ? ? 1.1.1 下载
? ? ? ? ? ? ? ? ?1.1.2? 解压缩
? ? ? ? ? ? ? ? 1.1.3? 将路径放入环境变量path里
? ? 1.2? 安装cmake
? ? ? ? ? ? ? ? 1.2.1 下载
? ? ? ? ? ? ? ? ?1.2.2? 解压缩
? ? ? ? ? ? ? ? 1.2.3? 将路径放入环境变量path里
2 :修改已有项目的配置,使之支持c++
? ?2.1 修改app/build.gradle文件。增加内容,具体内容见附文
? ? ?2.1? 在src/main目录下新建一个文件夹,命名为cpp;
? ? ?2.2? 在cpp文件夹下建文件,具体内容见附文;
? ? ?2.3? 在cpp文件夹下建文件native-lib.cpp,具体内容见附文;
? ? ;
?app/build.gradle文件
android {
defaultConfig {
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
}
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
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.
# 创建并命名一个库,第一个参数是名称,第二个参数是模式,第三个参数是源码位置。
# 可以定义多个库,CMake会自动构建他们
# Gradle会为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.
# 搜索指定的预构建库并将路径存储为变量
# CMake默认情况下包含搜索路径下的系统库,我们只需要指定我们想添加的库在ndk库的里名称。CMake会在编译他之前验证他是否存在。
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.
#告知CMake需要去连接哪些目标库,我们可以链接多个库,例如你在这个脚本中定义的库、预编译的三方库还可以是系统库。
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
src/main/cpp/native-lib.cpp
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_apkdecryptapplication_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
3 :java调用jni
关键代码在
1:System.loadLibrary("native-lib"); //加载类库
2:public native String stringFromJNI(); //声明方法
3:String ticks = stringFromJNI(); //调用
4:System.out.println("sayHello方法返回值为:"+ticks);? //打印返回结果,可选
package com.example.apkdecryptapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
TextView tickView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String ticks = stringFromJNI();
System.out.println("sayHello方法返回值为:"+ticks);
setContentView(R.layout.activity_main);
tickView = (TextView) findViewById(R.id.tickView);
MainActivity.this.tickView.setText("sayHello方法返回值为:"+ticks);
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xxx!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/tickView"/>
</androidx.constraintlayout.widget.ConstraintLayout>
4 :最终效果图
?
|