android studio 将 cpp 文件编译的so 库 给其它工程使用

这篇文章本来是作为另一篇 http://blog.csdn.net/handsonn/article/details/78253536 的一部分,但是感觉会有很多图片,就单独拿了出来

以下是我的运行环境:

操作系统:Ubuntu 16.0.4
IDE:Android Studio 3.0. beta4

网上大多数是另一种打包的流程,个人感觉比较麻烦,所以记录下自己尝试的这两种

这两种的前提都要有 ndk 和 cmake,主要区别是第一种有 “include c++ support”,比较方便,第二种没有 “include c++ support”:

第一种:

一共需要创建两个 android project
首先,要确认 SDK里面有安装了 CMake 和 NDK,如下

这里写图片描述

(1)创建第一个 project,记得勾选 c++ support

这里写图片描述

然后一路到finish,第一个 project 就建好了

(2)打开 MainActivity,长这样的,并且可以看到旁边有个 cpp 文件

这里写图片描述

打开 cpp 文件 ,看一下 具体内容

这里写图片描述

可以看到这里的类名是 包名+类名+方法名

(3)为了方便,我们建一个类,名为 NativeUtils,将 MainActivity 中的 native 方法移动到到这个类里面,结果如下:

这里写图片描述

这里写图片描述

然后,点击 Build –> Make Project 或者 ctrl + F9 , 然后切换到 project 目录,可以看到已经生成 so 库,
如下:

这里写图片描述

这就是要给其他工程用的,可以看到这样就生成了,默认是全部类型的 so 都有,如果要部分的话可以在
app 的 build.gradle 中添加过滤,即 abiFilters 指定生成什么格式的 so 文件,moduleName 指定 so 文件 名字,这里没有进行指定,

(4)将这些 so 文件拷贝出来,建立第二个工程,第二个工程随意,然后将这些 so 文件拷贝到 libs 目录下,并在 app 的 build.gradle 中添加依赖

这里写图片描述

(5)建立一个新的包,和第一个工程 NativeUtils 所在的包名一样,并且将第一个工程的 NativeUtils 类拷贝过来到这个新建的包下:

这里写图片描述

(6)这个时候我们在 MainActivity 调用 NativeUtils 中的 方法,毫无疑问,报了错误,如下

这里写图片描述

原因是:开始第一步的时候,我们将 第一个工程 MainActivity 中生成 的 native 方法移动 到 NativeUtils中,cpp 文件的类名称并没有修改,打开第一个工程,开始是这样的

这里写图片描述

我们需要改成这样,因为 这里的类名是 “ 包名+类名+方法名” ,我们改成了 NativieUtils,所以要改成如下:

这里写图片描述

重新 MakeProject,将 so 文件 复制到第二个工程,再次编译运行,即可成功运行,打个 Log即可验证

这里写图片描述

两个小点:
1、关于 so 的 加载,也就是 System.loadLibrary(“native-lib”); 可以看到 目录里面 这个 so 文件的名称是
“libnative-lib.so”,这里加载 没有 “lib” 和 “. so”

2、这 7 种 so 文件常用的 abi 只有三种

第二种:

上面是建立项目的时候勾选了 “include c++ support”,如果没有勾选呢,也可以自己来,首先创建一个 普通类 JniTest.java

这里写图片描述

然后 ALT + F12 呼出控制台 , cd到 main 文件夹,我的包名是 com.example.z.sotest,最后命令如下:
这里写图片描述

接着可以看到生成了 jni 的文件夹和对应的 h 文件,

这里写图片描述

接着,右键新建一个 c++ source file,名字叫 native-lib.cpp,然后内容如下:

这里写图片描述

这样导入一个 h 文件有可能后面编译会出现一个错误,如下:

这里写图片描述

根据提示,很简单的,将导入的尖括号改成双引号,即第一句改成

#include "com_example_z_sotest_JniTest.h" 后续的编译即可成功,

在进行编译之前,还需要配置一下 gradle,具体如下:

这里写图片描述

可以看到,还需要一个 CMakeLists.txt,我们需要在 app 下新建立一个 CMakeLists.txt,如下:

这里写图片描述

顺便将 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.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.

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/jni/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} )

然后 make project,或者 ctrl + F9,即可看到生成了 so 库,最后通过 textview 简单输出一段文字验证下:

这里写图片描述
这里写图片描述

更多可以参考:
https://developer.android.google.cn/ndk/guides/cmake
https://developer.android.com/studio/projects/add-native-code

猜你喜欢

转载自blog.csdn.net/handsonn/article/details/78106200