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 ""
                // 過濾生成的庫(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   用來表示函數(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)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子蔬胯,更是在濱河造成了極大的恐慌沽瞭,老刑警劉巖鹿榜,帶你破解...
    沈念sama閱讀 222,729評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件命爬,死亡現(xiàn)場(chǎng)離奇詭異施敢,居然都是意外死亡周荐,警方通過查閱死者的電腦和手機(jī)狭莱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來概作,“玉大人腋妙,你說我怎么就攤上這事⊙堕牛” “怎么了骤素?”我有些...
    開封第一講書人閱讀 169,461評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)愚屁。 經(jīng)常有香客問我济竹,道長(zhǎng),這世上最難降的妖魔是什么霎槐? 我笑而不...
    開封第一講書人閱讀 60,135評(píng)論 1 300
  • 正文 為了忘掉前任送浊,我火速辦了婚禮,結(jié)果婚禮上丘跌,老公的妹妹穿的比我還像新娘袭景。我一直安慰自己,他們只是感情好闭树,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,130評(píng)論 6 398
  • 文/花漫 我一把揭開白布耸棒。 她就那樣靜靜地躺著,像睡著了一般蔼啦。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上仰猖,一...
    開封第一講書人閱讀 52,736評(píng)論 1 312
  • 那天捏肢,我揣著相機(jī)與錄音,去河邊找鬼饥侵。 笑死鸵赫,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的躏升。 我是一名探鬼主播辩棒,決...
    沈念sama閱讀 41,179評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼膨疏!你這毒婦竟也來了一睁?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,124評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤佃却,失蹤者是張志新(化名)和其女友劉穎者吁,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體饲帅,經(jīng)...
    沈念sama閱讀 46,657評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡复凳,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,723評(píng)論 3 342
  • 正文 我和宋清朗相戀三年瘤泪,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片育八。...
    茶點(diǎn)故事閱讀 40,872評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡对途,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出髓棋,到底是詐尸還是另有隱情实檀,我是刑警寧澤,帶...
    沈念sama閱讀 36,533評(píng)論 5 351
  • 正文 年R本政府宣布仲锄,位于F島的核電站劲妙,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏儒喊。R本人自食惡果不足惜镣奋,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,213評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望怀愧。 院中可真熱鬧侨颈,春花似錦、人聲如沸芯义。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,700評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽扛拨。三九已至耘分,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間绑警,已是汗流浹背求泰。 一陣腳步聲響...
    開封第一講書人閱讀 33,819評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留计盒,地道東北人渴频。 一個(gè)月前我還...
    沈念sama閱讀 49,304評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像北启,于是被迫代替她去往敵國(guó)和親卜朗。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,876評(píng)論 2 361