转载注明出处:http://www.cnblogs.com/aliflycoris/p/5507236.html
0x01 前言
public class MainActivity extends Activity implements OnClickListener {
static{
System.loadLibrary("JniTest");
}
private native int Add(double num1,double num2);
private native int Sub(double num1,double num2);
private native int Mul(double num1,double num2);
private native int Div(double num1,double num2);
@Override
protected void onCreate(Bundle savedInstanceState) {
}
}
ndk {
moduleName "JniTest"
abiFilters "armeabi", "armeabi-v7a", "x86"
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class com_example_caculate_MainActivity */
#ifndef _Included_com_example_caculate_MainActivity
#define _Included_com_example_caculate_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_caculate_MainActivity
* Method: Add
* Signature: (DD)I
*/
JNIEXPORT jint JNICALL Java_com_example_caculate_MainActivity_Add
(JNIEnv *, jobject, jdouble, jdouble);
/*
* Class: com_example_caculate_MainActivity
* Method: Sub
* Signature: (DD)I
*/
JNIEXPORT jint JNICALL Java_com_example_caculate_MainActivity_Sub
(JNIEnv *, jobject, jdouble, jdouble);
/*
* Class: com_example_caculate_MainActivity
* Method: Mul
* Signature: (DD)I
*/
JNIEXPORT jint JNICALL Java_com_example_caculate_MainActivity_Mul
(JNIEnv *, jobject, jdouble, jdouble);
/*
* Class: com_example_caculate_MainActivity
* Method: Div
* Signature: (DD)I
*/
JNIEXPORT jint JNICALL Java_com_example_caculate_MainActivity_Div
(JNIEnv *, jobject, jdouble, jdouble);
#ifdef __cplusplus
}
#endif
#endif
#include
#define jintJNICALL
#ifndef _Included_com_example_caculate_MainActivity
#define _Included_com_example_caculate_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jintJNICALL Java_com_example_caculate_MainActivity_Add
(JNIEnv *env, jobject obj, jdouble num1, jdouble num2)
{
return (jint)(num1 + num2+2);
}
JNIEXPORT jintJNICALL Java_com_example_caculate_MainActivity_Sub
(JNIEnv *env, jobject obj, jdouble num1, jdouble num2)
{
return (jint)(num1 - num2+2);
}
JNIEXPORT jintJNICALL Java_com_example_caculate_MainActivity_Mul
(JNIEnv *env, jobject obj, jdouble num1, jdouble num2)
{
return (jint)(num1 * num2+2);
}
JNIEXPORT jintJNICALL Java_com_example_caculate_MainActivity_Div
(JNIEnv *env, jobject obj, jdouble num1, jdouble num2)
{
if (num2 == 0) return 0;
return (jint)(num1 / num2+2);
}
#ifdef __cplusplus
}
#endif
#endif
#include
#include
//#include
#include
#include
#include
#include
#include
JNIEXPORT jint JNICALL native_Add
(JNIEnv *env, jobject obj, jdouble num1, jdouble num2)
{
return (jint)(num1 + num2 +1);
}
JNIEXPORT jint JNICALL native_Sub
(JNIEnv *env, jobject obj, jdouble num1, jdouble num2)
{
return (jint)(num1 - num2 +1);
}
JNIEXPORT jint JNICALL native_Mul
(JNIEnv *env, jobject obj, jdouble num1, jdouble num2)
{
return (jint)(num1 * num2 +1);
}
JNIEXPORT jint JNICALL native_Div
(JNIEnv *env, jobject obj, jdouble num1, jdouble num2)
{
if (num2 == 0) return 0;
return (jint)(num1 / num2 +1);
}
//Java和JNI函数的绑定表
static JNINativeMethod gMethods[] = {
{"Add", "(DD)I", (void *)native_Add},
{"Sub", "(DD)I", (void *)native_Sub},
{"Mul", "(DD)I", (void *)native_Mul},
{"Div", "(DD)I", (void *)native_Div},
};
//注册native方法到java中
static int registerNativeMethods(JNIEnv* env, const char* className,
JNINativeMethod* gMethods, int numMethods)
{
jclass clazz;
clazz = (*env)->FindClass(env, className);
if (clazz == NULL) {
return JNI_FALSE;
}
if ((*env)->RegisterNatives(env, clazz, gMethods,numMethods) < 0){
return JNI_FALSE;
}
return JNI_TRUE;
}
int register_ndk_load(JNIEnv *env)
{
return registerNativeMethods(env, "com/example/caculate/MainActivity",
gMethods,sizeof(gMethods) / sizeof(gMethods[0]));
//NELEM(gMethods));
}
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
jint result = -1;
if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
return result;
}
register_ndk_load(env);
// 返回jni的版本
return JNI_VERSION_1_4;
}
public class MainActivity extends Activity implements OnClickListener {
static{
System.loadLibrary("JniTest");
}
private native int Add(double num1,double num2);
private native int Sub(double num1,double num2);
private native int Mul(double num1,double num2);
private native int Div(double num1,double num2);
@Override
protected void onCreate(Bundle savedInstanceState) {
}
}
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE := JniTest
LOCAL_SRC_FILES := MyJniCalc.c
LOCAL_SHARED_LIBRARIES := libandroid_runtime
include $(BUILD_SHARED_LIBRARY)
apply plugin: 'com.android.application'
android {
compileSdkVersion 16
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.example.caculate"
minSdkVersion 8
targetSdkVersion 16
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir 'src/main/libs'
}
task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {
destinationDir file("$projectDir/libs")
baseName "libJniTest"
extension "jar"
from fileTree(dir: "libs", include: "**/*.so")
into "lib"
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:18.0.0'
compile files('libs/libJniTest.jar')
}
public class MainActivity extends Activity implements OnClickListener {
static{
System.loadLibrary("JniTest");
}
private native int Add(double num1,double num2);
private native int Sub(double num1,double num2);
private native int Mul(double num1,double num2);
private native int Div(double num1,double num2);
@Override
protected void onCreate(Bundle savedInstanceState) {
}
}