本博客 NDK 開發(fā)系列文章:
- NDK 編譯的三種方式
- NDK 開發(fā)中引入第三方靜態(tài)庫和動態(tài)庫
- NDK 開發(fā)中 Native 與 Java 交互
- NDK POSIX 多線程編程
- NDK Android OpenSL ES 音頻采集與播放
- NDK FFmpeg 編譯
- NDK FFmpeg 音視頻解碼
- NDK 直播流媒體服務器搭建
- NDK 直播推流與引流
- NDK 開發(fā)中快速定位 Crash 問題
靜態(tài)庫和動態(tài)庫
靜態(tài)庫
靜態(tài)庫文件后綴一般為 .a ,利用靜態(tài)庫編譯成的可執(zhí)行文件通常比較大,編譯后的執(zhí)行程序不需要外部函數(shù)庫的支持。但是,如果一個程序依賴的靜態(tài)庫發(fā)生改變,那么這個程序就需要重新編譯。
動態(tài)庫
動態(tài)庫也稱為共享庫,動態(tài)庫文件后綴一般為 .so (Linux) 或 .dll (Windows) ,相對于靜態(tài)庫,動態(tài)庫在編譯時, 并沒有被編譯進目標代碼中,程序執(zhí)行到相關函數(shù)時才調(diào)用動態(tài)庫里的相關函數(shù),動態(tài)庫編譯后所產(chǎn)生的可執(zhí)行文件通常較小。由于動態(tài)庫沒有被整合進程序,而是程序運行時動態(tài)地申請并調(diào)用,因此程序的運行環(huán)境中必須提供相應的庫。動態(tài)庫的改變并不影響程序,便于升級。
兩種函數(shù)庫特點
靜態(tài)庫:編譯后的執(zhí)行程序不需要外部的函數(shù)庫支持,編譯過程中已經(jīng)被載入可執(zhí)行程序,程序運行時將不再需要該靜態(tài)庫;
動態(tài)庫:動態(tài)庫的代碼在程序運行時才載入內(nèi)存,而編譯過程中僅簡單的引用,代碼體積較小,并且動態(tài)庫與程序代碼獨立,可復用,耦合度低;
生成第三方動態(tài)庫和靜態(tài)庫
本文主要通過 CMake 工具構建,請確保 AS 版本在 2.2 以上。本節(jié)簡單生成一個動態(tài)庫和靜態(tài)庫,模仿我們要引入的第三方庫文件。
新建一個工程,選擇 Support C++ ,文件結構:
文件 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 ) // 添加為靜態(tài)庫
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 ) // 添加為動態(tài)庫
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
make 工程,在 app/src/main/jniLibs 目錄下生成了我們需要的第三方庫文件。
引入第三方庫
新建一個工程,將上節(jié)生成的第三方庫文件拷貝到 app/src/main/jniLibs 目錄下,在 cpp 目錄下新建 include 目錄,拷貝第三方庫的頭文件在 include 目錄下。
工程目錄
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 ,調(diào)用第三方庫提供的 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 。