美文网首页Android深入
Android 引用三方So库

Android 引用三方So库

作者: 没有了遇见 | 来源:发表于2022-07-06 10:26 被阅读0次
效果.gif

简介

日益奔放的Android生存环境下,工作中出现了各种各样稀奇古怪的需求,项目中对接各部门各种各样的三方库也成了现在的常见需求,这里就三方So库对接做个简单的例子

需求

添加三方So 库 根据.h(头文件)调用三方So库c/c++代码

环境

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

实现

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"
        //设置NDK版本
        ndkVersion "21.1.6352462"
        // gradle 执行的任务名字
        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.导入so和.h头文件

头文件so导入.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)

#设置工程名字
project("jnidemo")

#add_library target_include_directories  需要成对出现
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
        # 设置引入的函数库类型为静态库
        SHARED
        # 表示引入第三方静态库
        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);
}


总结

这个Demo简单的测试了一下根据头文件调用三方So路的Jni 调用方案,中间各种乱七八糟糟心事儿不少,现存方式有MK 和Cmake两种方案这里选取了Cmake方案,后续还有很多坑大家一起挨着吧,就这

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

欢迎点赞!!!

资源

1.Demo源码

2.gradle4.+和Cmake配置文档

相关文章

网友评论

    本文标题:Android 引用三方So库

    本文链接:https://www.haomeiwen.com/subject/vvaebrtx.html