Android 引用三方So庫

效果.gif

簡介

日益奔放的Android生存環(huán)境下,工作中出現(xiàn)了各種各樣稀奇古怪的需求,項目中對接各部門各種各樣的三方庫也成了現(xiàn)在的常見需求,這里就三方So庫對接做個簡單的例子

需求

添加三方So 庫 根據(jù).h(頭文件)調(diào)用三方So庫c/c++代碼

環(huán)境

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

實現(xiàn)

1.gradle.bulid 配置

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

android {
    compileSdkVersion 31
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.wkq.jnidemo"
        minSdkVersion 21
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"
        //設(shè)置NDK版本
        ndkVersion "21.1.6352462"
        // gradle 執(zhí)行的任務(wù)名字
        externalNativeBuild {
            cmake {
                cppFlags ""
                abiFilters "armeabi-v7a"
            }
        }
        ndk{
            abiFilters 'armeabi-v7a'
        }
    }
// 處理 gradle 4.0+
// 異常: More than one file was found with OS independent path 'lib/armeabi-v7a/libHancNetSDK.so'. If you are using jniLibs and CMake IMPORTED targets
    sourceSets {
        main{
            jniLibs.srcDirs=['libs']
        }
    }


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

    externalNativeBuild {
        cmake {
            path "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'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'

}

注意
Gradle4.+搭配Cmake中add_library引用三方庫的IMPORTED屬性會異常,需要配置Gradle配置一下

異常.png
gradle4.+ 搭配add_library(xx SHARED IMPORTED )異常.png

2.導(dǎo)入so和.h頭文件

頭文件so導(dǎo)入.png

3.配置CMakeLists.text

注意
因為Cmake中的文件位置配置,我沒搞明白,所以將CMakelists.text移動了出來放在了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.10.2)

#設(shè)置工程名字
project("jnidemo")

#add_library target_include_directories  需要成對出現(xiàn)
add_library( # Sets the name of the library.
        network
        # Sets the library as a shared library.
        SHARED
        # Provides a relative path to your source file(s).
        src/main/cpp/native-lib.cpp)
# 指向.H頭文件位置
target_include_directories(
        network
        PRIVATE
        ${CMAKE_SOURCE_DIR}/src/main/cpp/include
)

SET (LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
# 添加第三方庫
add_library(HancNetSDK
        # 設(shè)置引入的函數(shù)庫類型為靜態(tài)庫
        SHARED
        # 表示引入第三方靜態(tài)庫
        IMPORTED)
# 配置第三方庫鏈接
set_target_properties(
        HancNetSDK
        PROPERTIES
        IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libHancNetSDK.so)


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)


target_link_libraries( # Specifies the target library.
        network
        HancNetSDK
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

4 .h文件,cpp文件 Jni工具類

頭文件.png
cpp文件.png
package com.wkq.jnidemo;

/**
 * Jni 工具類
 */
public class JniUtil {
        static {
            System.loadLibrary("network");
            System.loadLibrary("HancNetSDK");
        }

    public static native String stringFromJNI();
    public static native boolean HancNetInit();
    public static native int netConnect(String ip,int port,String linkCmd,int timeout,int cmdSize,int dwUser);
}


總結(jié)

這個Demo簡單的測試了一下根據(jù)頭文件調(diào)用三方So路的Jni 調(diào)用方案,中間各種亂七八糟糟心事兒不少,現(xiàn)存方式有MK 和Cmake兩種方案這里選取了Cmake方案,后續(xù)還有很多坑大家一起挨著吧,就這

注意
Gradle 4.+和Cmake add_library 的IMPORTE 的異常

歡迎點贊!!!

資源

1.Demo源碼

2.gradle4.+和Cmake配置文檔

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

相關(guān)閱讀更多精彩內(nèi)容

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