#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(
C
M
A
K
E
S
O
U
R
C
E
D
I
R
/
s
r
c
/
m
a
i
n
/
c
p
p
)
s
e
t
(
j
n
i
l
i
b
s
"
{CMAKE_SOURCE_DIR}/src/main/cpp) set(jnilibs "
CMAKESOURCEDIR/src/main/cpp)set(jnilibs"{CMAKE_SOURCE_DIR}/src/main/jniLibs")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
j
n
i
l
i
b
s
/
{jnilibs}/
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( C M A K E S O U R C E D I R / s r c / m a i n / c p p / h a o h a o ) A D D S U B D I R E C T O R Y ( {CMAKE_SOURCE_DIR}/src/main/cpp/haohao) ADD_SUBDIRECTORY( CMAKESOURCEDIR/src/main/cpp/haohao)ADDSUBDIRECTORY({CMAKE_SOURCE_DIR}/src/main/cpp/nannan)
app/build.gradle
![image](//upload-images.jianshu.io/upload_images/3239933-6b04eb22ad241cf1.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/820/format/webp)
make 工程,在 app/src/main/jniLibs 目录下生成了我们需要的第三方库文件。
![image](//upload-images.jianshu.io/upload_images/3239933-0a0060da770eda3f.png?imageMogr2/auto-orient/strip|imageView2/2/w/524/format/webp)
# 引入第三方库
新建一个工程,将上节生成的第三方库文件拷贝到 app/src/main/jniLibs 目录下,在 cpp 目录下新建 include 目录,拷贝第三方库的头文件在 include 目录下。
工程目录
![image](//upload-images.jianshu.io/upload_images/3239933-94353d219e381439.jpg?imageMogr2/auto-orient/strip|imageView2/2/w/614/format/webp)
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
#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 “
j
n
i
l
i
b
s
/
{jnilibs}/
jnilibs/{ANDROID_ABI}/libnannan.so”)
引入 .a 文件
add_library(haohao STATIC IMPORTED )
set_target_properties(haohao PROPERTIES IMPORTED_LOCATION “
j
n
i
l
i
b
s
/
{jnilibs}/
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(
C
M
A
K
E
S
O
U
R
C
E
D
I
R
/
s
r
c
/
m
a
i
n
/
c
p
p
/
i
n
c
l
u
d
e
)
s
e
t
(
j
n
i
l
i
b
s
"
{CMAKE_SOURCE_DIR}/src/main/cpp/include) set(jnilibs "
CMAKESOURCEDIR/src/main/cpp/include)set(jnilibs"{CMAKE_SOURCE_DIR}/src/main/jniLibs")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
j
n
i
l
i
b
s
/
{jnilibs}/
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’