android 项目使用 cmake 生成so库

2019-11-20  本文已影响0人  古都旧城

使用CMake开发Jni相对传统方式要简单一些(传统方式参考:https://www.jianshu.com/p/7e0a7ede540b

配置环境

使用CMake进行Jni开发需要使用CMake插件、LLDB插件、NDK插件,这些都可以通过Android Studio很快地安装。
打开SDK Manager,找到Android SDK->SDK Tool选项,安装CMake、LLDB、NDK插件。

image.png

创建支持c++代码的工程

勾选Include C++ support选项,勾选后Android Studio可以更好地支持及帮助我们检查jni代码,之后一路next即可。


image.png

如此构建就会自动生成一个demo示例,直接跑就可以正常调用演示了(具体自动生成的文件,同下方介绍)。

如果项目已经存在,想用cmake,那么可以对比上方demo,看看做出了什么变化,进行对应调整即可:

externalNativeBuild {
    cmake {
        path "CMakeLists.txt"
    }
}
externalNativeBuild {
    cmake {
        cppFlags "-std=c++14"
    }
}
#include <jni.h>
#include <string>

extern "C"
JNIEXPORT jstring

JNICALL
Java_com_xxx_xxx_MainActivity_stringFromJNI(//注意这里的路径要和包名对应 Java_包名
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}
# 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.
# 创建并命名库,可以将库的类型设置为静态
# 或共享,并提供该库源代码的相对路径。
# 你可以定义多个library库(如果有多个so要生成,就可以在这里配置多个add_library),并使用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).
             # 库所在位置的相对路径
             src/main/cpp/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.
# NDK提供了一套实用的原生API和库,可以使用find_library搜索NDK中存在的库,只需要加入所需要使用库的名称即可,如下面的日志库log-lib。
# 更多的库可参考https://developer.android.com/ndk/guides/stable_apis
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连接到目标库中,可以链接多个库,如上面的native-lib库以及NDK中自带的log-lib库(这里也可以同时添加多个target_link_libraries)
target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       #
                       ${log-lib} ) 

整体目录结构:

image.png

看目录可以知道,相比普通工程,AndroidStudio主动为我们生成了一个cpp的目录,该目录主要存放调用的c++头文件,源代码及jni代码,还有一个CMakeLists.txt的文件,cpp目录下面包含一个native-lib.cpp文件(稍后会讲解),这里需要关注的主要文件有3个,native-llib.cpp、MainActivity和CMakeList.txt。下面重点讲解一下这几个文件的作用。

调用方式

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();
}

这样项目直接运行,就会自动把生产的so打到apk里面了,就可以完成正常调用了。

都做了什么:

说明:本文仅用做个人笔记,参考自:https://blog.csdn.net/u013564742/article/details/86512271

上一篇下一篇

猜你喜欢

热点阅读