#include <jni.h>
#include <string>
int sum(int a, int b) {
return a + b;
}
在defaultConfig目录里面 ndk { abiFilters "armeabi","x86" }
在app.gralde中的android目录下 sourceSets.main { jniLibs.srcDirs = ['libs'] jni.srcDirs = [] }
#设置so库路径
set(my_lib_path ${CMAKE_SOURCE_DIR}/../../../libs)
#将第三方库作为动态库引用
add_library(test-lib
SHARED
IMPORTED)
#指定第三方库的绝对路径
set_target_properties(test-lib
PROPERTIES IMPORTED_LOCATION
${my_lib_path}/${ANDROID_ABI}/libtest-lib.so)
target_link_libraries( # Specifies the target library.
native-lib
test-lib
# Links the target library to the log library
# included in the NDK.
${log-lib})
cmake_minimum_required(VERSION 3.4.1)
#设置so库路径
set(my_lib_path ${CMAKE_SOURCE_DIR}/libs)
#将第三方库作为动态库引用
add_library(test-lib
SHARED
IMPORTED)
#指定第三方库的绝对路径
set_target_properties(test-lib
PROPERTIES IMPORTED_LOCATION
${my_lib_path}/${ANDROID_ABI}/libtest-lib.so)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp)
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.
native-lib
test-lib
# Links the target library to the log library
# included in the NDK.
${log-lib})
//
// Created by aruba on 2020/4/13.
//
#ifndef NDKAPPLICATION_TEST_LIB_H
#define NDKAPPLICATION_TEST_LIB_H
//申明外部 函数 外部属性
extern int sum(int a, int b);
#endif //NDKAPPLICATION_TEST_LIB_H
#include <jni.h>
#include <string>
#include <android/log.h>
#include <assert.h>
#include "test-lib.h"
#define TAG "C++"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
extern "C"
JNIEXPORT jstring JNICALL native_stringFromJNI(JNIEnv *env, jclass type) {
return env->NewStringUTF("C++");
}
JNIEXPORT jint JNICALL native_sum(JNIEnv *env, jclass type, jint a, jint b) {
return sum(a, b);
}
static const JNINativeMethod gMethods[] = {
{
"stringFromJNI", "()Ljava/lang/String;", (void *) native_stringFromJNI
},
{
"sum", "(II)I", (void *) native_sum
}
};
static int registerNatives(JNIEnv *env) {
LOGI("registerNatives begin");
jclass clazz;
//找到java的类
clazz = env->FindClass("com/aruba/ndkapplication/JniUtils");
if (clazz == NULL) {
LOGI("clazz is null");
return JNI_FALSE;
}
if (env->RegisterNatives(clazz, gMethods, NELEM(gMethods)) < 0) {
LOGI("RegisterNatives error");
return JNI_FALSE;
}
return JNI_TRUE;
}
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
LOGI("jni_OnLoad begin");
JNIEnv *env = NULL;
if (vm->GetEnv((void **) &env, JNI_VERSION_1_4) != JNI_OK) {
LOGI("ERROR: GetEnv failed\n");
return -1;
}
assert(env != NULL);
registerNatives(env);
return JNI_VERSION_1_4;
}
package com.aruba.ndkapplication;
public class JniUtils {
static {
System.loadLibrary("native-lib");
}
public static native String stringFromJNI();
public static native int sum(int a, int b);
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = findViewById(R.id.sample_text);
tv.setText(JniUtils.sum(10, 20) + "");
}
}