Android-NDK/JNIJNI&NDK

JNI&NDK开发最佳实践(三):CMake实现调用已有

2019-06-29  本文已影响25人  taoyyyy

目标

CMake实现调用已有so库中的本地方法。

实现步骤梳理

  1. 在需要调用本地方法的java文件中加载so库,并声明本地函数。
  2. 新建.h头文件,对需要调用的so库中的方法作声明。
  3. 新建与java同级的cpp文件夹,在其中新建c/c++文件,include上面的头文件,实现对so库中方法的调用。
  4. 新建CMakeList.txt文件,在其中作相应so库名称、C文件路径等相关配置。
  5. 在build.gradle中配置CMakeList.txt文件路径、ABI类别、so库的路径等。
  6. 拷贝已有的第三方so库到本地(默认目录为src/main/jniLibs,如果要拷贝到其他目录需要在build.gradle文件中配置)。

具体实现

1. 在需要调用本地方法的java文件中加载so库,并声明本地函数。

package com.example.taoying.newjnitest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    static {
        System.loadLibrary("native-libaa");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = findViewById(R.id.tv);
        tv.setText(stringFromJNI());
    }

    public native String stringFromJNI();
}

2. 新建.h头文件,对需要调用的so库中的方法作声明。

com_example_taoying_testndkapp_MainActivity.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_taoying_testndkapp_MainActivity */

#ifndef _Included_com_example_taoying_testndkapp_MainActivity
#define _Included_com_example_taoying_testndkapp_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_example_taoying_testndkapp_MainActivity
 * Method:    stringFromJNI
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_taoying_testndkapp_MainActivity_stringFromJNI
  (JNIEnv *, jobject);

/*
 * Class:     com_example_taoying_testndkapp_MainActivity
 * Method:    stringFromJNIwithp
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_taoying_testndkapp_MainActivity_stringFromJNIwithp
  (JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif

接下来对该头文件做一个小小的分析:

#ifndef _Included_com_example_taoying_testndkapp_MainActivity
#define _Included_com_example_taoying_testndkapp_MainActivity

#ifdef __cplusplus
#extern "C" {
#endif

3. 新建与java同级的cpp文件夹,在其中新建c/c++文件,include上面的头文件,实现对so库中方法的调用。

配置文件路径.png

native-lib.cpp

# 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库名称
add_library(native-libaa SHARED src/main/cpp/native-lib.cpp )

#动态方式加载的第三方so库名称
add_library(native-lib SHARED IMPORTED)

#设置第三方so库路径  ${CMAKE_SOURCE_DIR}是CMakeLists.txt的路径   ${ANDROID_ABI} 标识cup类型
set_target_properties(native-lib PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libnative-lib.so)


#导入.h头文件的路劲,这样在native-lib.cpp里就可以#include <CNativeFunction.h>,然后就可以使用CNativeFunction里面的方法啦
include_directories(${CMAKE_SOURCE_DIR}/src/main/jniLibs/include)


# 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.


# 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.

#相关联的so库
target_link_libraries( # Specifies the target library.
                       native-libaa native-lib android ${log-lib} )
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 )


5. 在build.gradle中配置CMakeList.txt文件路径、ABI类别、so库的路径等。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28


    defaultConfig {
        applicationId "com.example.taoying.newjnitest"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }

        ndk {
            abiFilters "arm64-v8a"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    externalNativeBuild {
        // 指定CMake编译配置文件路径
        cmake {
            path "CMakeLists.txt"
            version "3.10.2"
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

注意如果只提供了arm64-v8a架构的第三方so库,那么咱也只能去生成对应架构的so库。

ndk {
    abiFilters "arm64-v8a"
}

6. 拷贝已有的第三方so库到本地(默认目录为src/main/jniLibs,如果要拷贝到其他目录需要在build.gradle文件中配置)。

配置文件路径.png

运行结果

结果.png

更多JNI&NDK系列文章,参见:
JNI&NDK开发最佳实践(一):开篇
JNI&NDK开发最佳实践(二):CMake实现调用已有C/C++文件中的本地方法
JNI&NDK开发最佳实践(三):CMake实现调用已有so库中的本地方法
JNI&NDK开发最佳实践(四):JNI数据类型及与Java数据类型的映射关系
JNI&NDK开发最佳实践(五):本地方法的静态注册与动态注册
JNI&NDK开发最佳实践(六):JNI实现本地方法时的数据类型转换
JNI&NDK开发最佳实践(七):JNI之本地方法与java互调
JNI&NDK开发最佳实践(八):JNI局部引用、全局引用和弱全局引用
JNI&NDK开发最佳实践(九):调试篇

上一篇下一篇

猜你喜欢

热点阅读