天天看點

在 NDK 開發中引入第三方靜态庫和動态庫生成第三方動态庫和靜态庫引入第三方庫

作者:位元組流動

來源:

https://blog.csdn.net/Kennethdroid/article/details/86418725

靜态庫

靜态庫檔案字尾一般為 .a ,利用靜态庫編譯成的可執行檔案通常比較大,編譯後的執行程式不需要外部函數庫的支援。但是,如果一個程式依賴的靜态庫發生改變,那麼這個程式就需要重新編譯。

動态庫

動态庫也稱為共享庫,動态庫檔案字尾一般為 .so (Linux) 或 .dll (Windows) ,相對于靜态庫,動态庫在編譯時, 并沒有被編譯進目标代碼中,程式執行到相關函數時才調用動态庫裡的相關函數,動态庫編譯後所産生的可執行檔案通常較小。由于動态庫沒有被整合程序式,而是程式運作時動态地申請并調用,是以程式的運作環境中必須提供相應的庫。動态庫的改變并不影響程式,便于更新。

兩種函數庫特點

靜态庫:編譯後的執行程式不需要外部的函數庫支援,編譯過程中已經被載入可執行程式,程式運作時将不再需要該靜态庫;

動态庫:動态庫的代碼在程式運作時才載入記憶體,而編譯過程中僅簡單的引用,代碼體積較小,并且動态庫與程式代碼獨立,可複用,耦合度低;

生成第三方動态庫和靜态庫

本文主要通過 CMake 工具建構,請確定 AS 版本在 2.2 以上。本節簡單生成一個動态庫和靜态庫,模仿我們要引入的第三方庫檔案。

建立一個工程,選擇 Support C++ ,檔案結構:

在 NDK 開發中引入第三方靜态庫和動态庫生成第三方動态庫和靜态庫引入第三方庫

檔案 haohao/haohao.h ,定義一個簡單的結構體和一個簡單的類。

//
// Created by haohao on 2017/12/14.
//

#ifndef NDKLIB_HAOHAO_H
#define NDKLIB_HAOHAO_H
#include <stddef.h>
#include <android/log.h>
#include <jni.h>
#include <string>

#define  LOG_E(...)  __android_log_print(ANDROID_LOG_ERROR,"HaoHao",__VA_ARGS__)

#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
    std::string (*getResponse)();
} HaoHao_FUN;

class HaoHao{
public:
    HaoHao(std::string, int);
    std::string getHaoHaoWord();

private:
    std::string name;
    int age;

};

extern __attribute__ ((visibility ("default"))) HaoHao_FUN haohao_fun;

#ifdef __cplusplus
}
#endif
#endif //NDKLIB_HAOHAO_H
      

haohao/haohao.cpp ,執行個體化一個結構體。

#include "haohao.h"

HaoHao::HaoHao(std::string name, int age) {
    this->name = name;
    this->age = age;
}

std::string HaoHao::getHaoHaoWord() {
    char tem[3];
    sprintf(tem, "%d", age);
    return "Hello, My name is " + name + " I am " + tem + " years old";
}

std::string get_response(){
    HaoHao hao = HaoHao("chary", 23);
    return hao.getHaoHaoWord();
}

__attribute__ ((visibility ("default"))) HaoHao_FUN haohao_fun = {
     get_response
};      

haohao/CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)
add_library(haohao STATIC haohao.cpp ) // 添加為靜态庫

find_library(log-lib  log )

target_link_libraries(haohao ${log-lib} )      

nannan/nannan.h ,定義一個簡單的結構體,提供給其他程式使用。

#ifndef NDKLIB_NANNAN_H
#define NDKLIB_NANNAN_H
#include <stddef.h>
#include <android/log.h>
#include <jni.h>
#include <string>

#define  LOG_E(...)  __android_log_print(ANDROID_LOG_ERROR,"NanNan",__VA_ARGS__)

#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
    std::string (*getResponse)();

    std::int32_t (*getNanNanKey)();
} NANNAN_FUN;

extern __attribute__ ((visibility ("default"))) NANNAN_FUN nannan_fun;

#ifdef __cplusplus
}
#endif
#endif //NDKLIB_NANNAN_H      

nannan/nannan.cpp

#include "nannan.h"

std::string get_nannan_response(){
    LOG_E("Nannan get response");
    return "Hello, My name is Nannan!";
}

std::int32_t get_nannan_age(){
    LOG_E("Nannan get age");
    return 23;
}

__attribute__ ((visibility ("default"))) NANNAN_FUN nannan_fun = {
        get_nannan_response,
        get_nannan_age
};
      

nannan/CmakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

add_library(nannan SHARED nannan.cpp ) // 添加為動态庫

find_library(log-lib  log )

target_link_libraries(nannan ${log-lib} )      

根目錄下的 CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp)
set(jnilibs "${CMAKE_SOURCE_DIR}/src/main/jniLibs")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${jnilibs}/${ANDROID_ABI})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -std=c++14 -pthread -DMGULK_LOG_STDERR=1 -Wall -Wextra -Wnon-virtual-dtor -g")

ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/src/main/cpp/haohao)
ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/src/main/cpp/nannan)      

app/build.gradle

在 NDK 開發中引入第三方靜态庫和動态庫生成第三方動态庫和靜态庫引入第三方庫

make 工程,在 app/src/main/jniLibs 目錄下生成了我們需要的第三方庫檔案。

在 NDK 開發中引入第三方靜态庫和動态庫生成第三方動态庫和靜态庫引入第三方庫

引入第三方庫

建立一個工程,将上節生成的第三方庫檔案拷貝到 app/src/main/jniLibs 目錄下,在 cpp 目錄下建立 include 目錄,拷貝第三方庫的頭檔案在 include 目錄下。

工程目錄

在 NDK 開發中引入第三方靜态庫和動态庫生成第三方動态庫和靜态庫引入第三方庫

MainActivity.java

package com.haohao.ndklib;

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

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
      

native/native.cpp ,調用第三方庫提供的 API 。

#include <jni.h>
#include <string>
#include <nannan.h>
#include <haohao.h>

extern "C"
{
JNIEXPORT jstring JNICALL
Java_com_haohao_ndklib_MainActivity_stringFromJNI(JNIEnv
                                                  *env, jobject instance) {

    char age[3];
    sprintf(age, "%d", nannan_fun.getNanNanKey());

    std::string value = nannan_fun.getResponse() + " I am " + age + " years old.\n";
    value += haohao_fun.getResponse();
    //std::string value = "Hello";
    return env->NewStringUTF(value.c_str());
}
}      

native/CMakeLists.txt 檔案。

add_library(native SHARED native.cpp)

# 引入 .so 檔案
add_library(nannan SHARED IMPORTED )
set_target_properties(nannan PROPERTIES IMPORTED_LOCATION "${jnilibs}/${ANDROID_ABI}/libnannan.so")

# 引入 .a 檔案
add_library(haohao STATIC IMPORTED )
set_target_properties(haohao PROPERTIES IMPORTED_LOCATION "${jnilibs}/${ANDROID_ABI}/libhaohao.a")

find_library( log-lib log )

target_link_libraries(native nannan haohao ${log-lib})      

根目錄下的 CMakeLists.txt 檔案。

cmake_minimum_required(VERSION 3.4.1)

include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include)
set(jnilibs "${CMAKE_SOURCE_DIR}/src/main/jniLibs")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${jnilibs}/${ANDROID_ABI})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -std=c++14 -pthread -DMGULK_LOG_STDERR=1 -Wall -Wextra -Wnon-virtual-dtor -g")

ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/src/main/cpp/native)      

app/build.gradle 檔案配置。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.haohao.ndklib"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                arguments '-DANDROID_PLATFORM=android-15',
                        '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=gnustl_static'
//                cppFlags "-DANDROID_STL=c++_static"
                abiFilters 'armeabi-v7a','x86_64', 'arm64-v8a','x86'
            }
        }

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

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
}
      

build 工程後,生成

libnative.so

,直接運作項目,安裝 apk 。

在 NDK 開發中引入第三方靜态庫和動态庫生成第三方動态庫和靜态庫引入第三方庫
本文示例代碼 https://github.com/githubhaohao/NDKLib

NDK 開發系列文章:

「視訊雲技術」你最值得關注的音視訊技術公衆号,每周推送來自阿裡雲一線的實踐技術文章,在這裡與音視訊領域一流工程師交流切磋。  
在 NDK 開發中引入第三方靜态庫和動态庫生成第三方動态庫和靜态庫引入第三方庫