Android Studio CMakeList编译JNI

1. 准备工作

下载NDK,CMake、LLDB工具

准备工作注意两个坑

  1. 下载NDK后可能会报缺少 mips64el-linux-android-4.9 文件,需要去官网重新下载一份android-ndk-r16b版本NDK,解压后 将里面的上述文件copy到你的NDK文件夹里,如下图:
    Android Studio CMakeList编译JNI_第1张图片
  2. 下载Cmake记得选择3.6版本,目前博主AS3.0.1,下载3.10会报错,记得下面NDK也需要勾选,不另附截图了。
    Android Studio CMakeList编译JNI_第2张图片

2. 配置gradle文件和 CMakeList

  1. gradle.properties文件里添加如下: android.useDeprecatedNdk=true
  2. 在app 下 build.gradle 文件配置如下
android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.xxx.xxx"
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        ndk{
            moduleName "BVCurrencyManage"// 构建so库名称,记得修改
            abiFilters "arm64-v8a","armeabi-v7a","x86","x86_64"
        }
        // 使用Cmake工具 ,这里未打包 'armeabi'
        externalNativeBuild {
            cmake {
                cppFlags ""
                abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
            }
        }
    }

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt" 
        }
    }

    sourceSets {
        main {
            jni.srcDirs = []
            jniLibs.srcDirs = ['libs']
        }
    }
}
  1. 添加CMakeList文件,可以手动创建一个CMakeList.txt 文件,然后将下面代码copy,注意修改里面so文件名、连接的目标库,直接放在app文件夹下
# 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.
# 设置生成的so动态库最后输出的路径
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI})

add_library( # Sets the name of the library.  设置生成so文件名
            BVCurrencyManage

            # Sets the library as a shared library.
            SHARED

            # Provides a relative path to your source file(s).  要编译的C/C++文件,可能会有N多个相关调用的 cpp文件,都要在这里声明
            src/main/jni/BVCurrencyManage.cpp
            src/main/jni/BVCurrencyManageDLL.cpp
            src/main/jni/CurrencyInfo_ARS.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. 指定连接的目标库
                      BVCurrencyManage

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

Android Studio CMakeList编译JNI_第3张图片

3. 定义Java 类,创建头文件

  1. 定义一个Java,并声明C++ 中的接口,在java定义为native 方法
public class BVCurrencyManage {
    static {
        System.loadLibrary("BVCurrencyManage");
    }


   // 获取版本号,
    public static native int getVersion(String version);
}
  1. 通过Tools 工具配置生成头文件,也可以通过命令方式生成,这里介绍Tools配置的简单方法
    通过 File–Settings–>Tools --> external tools–>’+’–>进入如下图
    Android Studio CMakeList编译JNI_第4张图片
name JNI Create(可自定义)
  javah
  -v -jni -d $ModuleFileDir$/src/main/jni $FileClass$    
  $SourcepathEntry$

点击选中创建的java 文件,右键如下图
Android Studio CMakeList编译JNI_第5张图片
会在src/main/jni 目录下生成一个对应 .h头文件

4. 创建Cpp文件

在与jni 文件夹下创建一个 Cpp文件,copy 头文件中的方法到 cpp文件中

#include 
#include "BVCurrencyManageDLL.h"

extern "C" {

JNIEXPORT jint JNICALL Java_com_包_名_BVCurrencyManage_getVersion
        (JNIEnv *env, jclass, jstring version) {
    char *t = (char *) env->GetStringUTFChars(version, 0);
    int res = GetDllVersion(t);// 这里是调用别的库里方法
    return (jint) res;
}
}

生成so文件

在工具栏 build–>Make project,会在 jniLibs生成对应架构的so库,或者先尝试运行检查会不会报错。
最后生成的文件:
Android Studio CMakeList编译JNI_第6张图片

你可能感兴趣的:(Android,开发随笔)