Android JNI項(xiàng)目創(chuàng)建,方法梳理以及生成So庫(kù)

簡(jiǎn)介

項(xiàng)目需要涉及到JNI調(diào)用C++ 代碼所以所以學(xué)習(xí)了一了一下Android Studio 工具下 Cmake方式的 Jni 調(diào)用的一些知識(shí)特此記錄一下

環(huán)境版本

  • Android Studio 4.1.3
  • Gradle 4.1.3
  • Cmake 3.10.2
  • ndk 21.1.6352462
  • buildToolsVersion 30.0.3

1.項(xiàng)目配置

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdkVersion 31
// 31.0.0 版本有異常不能用
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.wkq.cdemo"
        minSdkVersion 21
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"
        //設(shè)置NDK版本(生成動(dòng)態(tài)so庫(kù)的時(shí)候要用)
        ndkVersion "21.1.6352462"
        // gradle 執(zhí)行的任務(wù)名字
        externalNativeBuild {
            cmake {
                cppFlags ""
                // 過(guò)濾生成的庫(kù)類型
//                abiFilters 'armeabi-v7a'
                //  庫(kù)格式'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
//生成指定版本的 CMakeLists.txt 文件
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
}
文件.png

2.CMakeLists 文件介紹

# 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.
#設(shè)置CMake的最低版本
cmake_minimum_required(VERSION 3.10.2)

# Declares and names the project.
project("cdemo")


#這些 native-lib.cpp資源打包設(shè)置成一個(gè)名為hellow 的共享庫(kù)(SHARED 替換為STATIC則設(shè)置為靜態(tài)庫(kù))
#  native-lib.cpp  
add_library( # Sets the name of the library.
        hellow
        # 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.

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.

#(設(shè)置要鏈接的庫(kù)文件的名稱 hellow)
target_link_libraries( # Specifies the target library.
        hellow

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

3.cpp 文件簡(jiǎn)介

// include 是導(dǎo)包
#include <jni.h>
#include <string>
// extern "C" :C++兼容C代碼的
// JNIEXPORT  類似于public修飾符
// JNICALL   用來(lái)表示函數(shù)的調(diào)用規(guī)范
// jstring  返回?cái)?shù)據(jù)類型 這里是String
// Java_<包名>_<類名>_<方法名>
extern "C"
JNIEXPORT jstring JNICALL Java_com_wkq_cdemo_MyJniUtil_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "1122211";
    return env->NewStringUTF(hello.c_str());
}

4. JNI 工具類

package com.wkq.cdemo;

/**
 * @author wkq
 * @date 2022年06月28日 16:46
 * @des
 */

public class MyJniUtil {
    //加載 so庫(kù)
    static{
        System.loadLibrary("hellow");
    }

    //  jni 調(diào)用
    public  static native String stringFromJNI();

}

5.調(diào)用

 String content=MyJniUtil.stringFromJNI();

6.生成動(dòng)態(tài)So庫(kù)

生成動(dòng)態(tài)So庫(kù)有兩種方式

  • make project
  • Gradle任務(wù)中執(zhí)行 externalNativeBuildRelease/externalNativeBuildRelease或者externalNativeBuildDebug 任務(wù)


    生成方式.png

    結(jié)果.png

總結(jié)

簡(jiǎn)單的梳理了C++類型項(xiàng)目中一些配置,然后生成了動(dòng)態(tài)的So庫(kù).
那些打不死我的終將使我強(qiáng)大,繼續(xù)死磕JNI

資源

1.Demo地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容