android studio搭建cmake的ndk环境

下载所需资源

  1. android studio中下载Ndk、LLDB、CMake

创建可开发的jni项目环境

  1. 新建jni文件路径

项目右键: new -> folder -> Jni Folder

文件存放路径:“src/main/cpp/”

  1. 创建cmake配置文件

CMakeLists.txt

  1. 配置信息详见地址

https://d.android.com/studio/projects/add-native-code.html

  1. CMakeLists.txt配置
# 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库的名称。
find_library( # Sets the name of the path variable.
			  # 设置path变量的名称。
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              # 指定要CMake定位的NDK库的名称。
              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.
                       # 将目标库链接到包含在NDK中的日志库。
                       ${log-lib} )
  1. build.gradle中的配置
android {
	defaultConfig {
        //使用Cmake工具
        externalNativeBuild {
            cmake {
            	//生成模式
                cppFlags ""
                //设置支持的SO库架构。arm64-v8a, armeabi-v7a, x86, x86_64
                abiFilters 'arm64-v8a', 'x86', 'armeabi-v7a' , 'x86_64'
            }
        }
    }
	// 配置CMakeLists.txt路径,放在哪里不规定,只要能够通过路径找到
	externalNativeBuild {
        cmake {
            path "src/main/jni/CMakeLists.txt"
        }
    }
}
  1. 创建原生方法java类
public class TestJni {
    //首先要制定加载的库
    static {
        System.loadLibrary("native-lib");
    }

    //原生方法
    public native String getHelloJni();
}
  1. 创建实现java方法的c++代码
#include <jni.h>
#include <string>

extern "C" {//如果使用了.cpp文件,需要生命{}中的方法以c的方式编译

JNIEXPORT jstring JNICALL
Java_com_lh_jni_JniTest_getHelloJni(JNIEnv *env, jobject) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

JNIEXPORT jint JNICALL
Java_com_lh_jni_JniTest_add(JNIEnv *env, jobject instance, jint a, jint b) {
    return a + b;
}

}
  1. 运行,测试
JniTest jniTest = new JniTest();
String content = jniTest.getHelloJni();
int addNum = jniTest.add(10,15);
发布了113 篇原创文章 · 获赞 48 · 访问量 34万+

猜你喜欢

转载自blog.csdn.net/yehui928186846/article/details/93631033