新建工程NDKTest,新建一个空Activity
新建java类MyNDK.java:
package com.zj.ndktest; /** * Created by root on 15-11-25. */ public class MyNDK { static { System.loadLibrary("hello-jni"); } public static native String Hello(); }
local.properties上加入:
sdk.dir=/opt/android-sdk-linux ndk.dir=/opt/android-ndk-r10e
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.zj.ndktest" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" ndk { moduleName "hello-jni" } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.0' }
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java:
package com.zj.ndktest; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.widget.TextView; public class MainActivity extends Activity { private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mTextView = (TextView)findViewById(R.id.text_view); mTextView.setText(MyNDK.Hello()); } }
点击MyNDK.java文件,右键点击External Tools->javah(没有的请查看android ndk 入门 - 一个简单的ndk工程)
生成app/src/main/jni/com_zj_ndktest_MyNDK.h
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_zj_ndktest_MyNDK */ #ifndef _Included_com_zj_ndktest_MyNDK #define _Included_com_zj_ndktest_MyNDK #ifdef __cplusplus extern "C" { #endif /* * Class: com_zj_ndktest_MyNDK * Method: Hello * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_zj_ndktest_MyNDK_Hello (JNIEnv *, jclass); #ifdef __cplusplus } #endif #endif
LOCAL_PATH := ${call my-dir} include $(CLEAR_VARS) LOCAL_MODULE := hello-jni LOCAL_SRC_FILES := main.cpp include ${BUILD_SHARED_LIBRARY}
#include <jni.h> #include "com_zj_ndktest_MyNDK.h" JNIEXPORT jstring JNICALL Java_com_zj_ndktest_MyNDK_Hello (JNIEnv *env, jclass cla) { return env->NewStringUTF("Hello JNI"); }
####################################################################
实现int输入,int输出
在MyNDK.java上加入:
public static native int HelloIntegerAdd(int a, int b); public static native int HelloIntegerSubtract(int a, int b); public static native int HelloIntegerMultiply(int a, int b); public static native int HelloIntegerDivide(int a, int b);
<TextView android:id="@+id/text_integer_add" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_integer_subtarct" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_integer_multiply" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_integer_divide" android:layout_width="match_parent" android:layout_height="wrap_content" />
在MainActivity上加入:
package com.zj.ndktest; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.widget.TextView; public class MainActivity extends Activity { private TextView mTextView; private TextView mTextIntegerAdd; private TextView mTextIntegerSubtract; private TextView mTextIntegerMultiply; private TextView mTextIntegerDivide; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mTextView = (TextView)findViewById(R.id.text_view); mTextView.setText(MyNDK.Hello()); StringBuffer text_integer_add = new StringBuffer("3 + 5 ="); text_integer_add.append(MyNDK.HelloIntegerAdd(3, 5)); mTextIntegerAdd = (TextView)findViewById(R.id.text_integer_add); mTextIntegerAdd.setText(text_integer_add.toString()); StringBuffer text_integer_subtract = new StringBuffer("33 - 100 = "); text_integer_subtract.append(MyNDK.HelloIntegerSubtract(33, 100)); mTextIntegerSubtract = (TextView)findViewById(R.id.text_integer_subtarct); mTextIntegerSubtract.setText(text_integer_subtract.toString()); StringBuffer text_integer_multiply = new StringBuffer("6 * 23 = "); text_integer_multiply.append(MyNDK.HelloIntegerMultiply(6, 23)); mTextIntegerMultiply = (TextView)findViewById(R.id.text_integer_multiply); mTextIntegerMultiply.setText(text_integer_multiply.toString()); StringBuffer text_integer_divide = new StringBuffer("90 / 32 = "); text_integer_divide.append(MyNDK.HelloIntegerDivide(90, 32)); mTextIntegerDivide = (TextView)findViewById(R.id.text_integer_divide); mTextIntegerDivide.setText(text_integer_divide.toString()); } }
生成新的com_zj_ndktest_MyNDK.h:
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_zj_ndktest_MyNDK */ #ifndef _Included_com_zj_ndktest_MyNDK #define _Included_com_zj_ndktest_MyNDK #ifdef __cplusplus extern "C" { #endif /* * Class: com_zj_ndktest_MyNDK * Method: Hello * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_zj_ndktest_MyNDK_Hello (JNIEnv *, jclass); /* * Class: com_zj_ndktest_MyNDK * Method: HelloIntegerAdd * Signature: (II)I */ JNIEXPORT jint JNICALL Java_com_zj_ndktest_MyNDK_HelloIntegerAdd (JNIEnv *, jclass, jint, jint); /* * Class: com_zj_ndktest_MyNDK * Method: HelloIntegerSubtract * Signature: (II)I */ JNIEXPORT jint JNICALL Java_com_zj_ndktest_MyNDK_HelloIntegerSubtract (JNIEnv *, jclass, jint, jint); /* * Class: com_zj_ndktest_MyNDK * Method: HelloIntegerMultiply * Signature: (II)I */ JNIEXPORT jint JNICALL Java_com_zj_ndktest_MyNDK_HelloIntegerMultiply (JNIEnv *, jclass, jint, jint); /* * Class: com_zj_ndktest_MyNDK * Method: HelloIntegerDivide * Signature: (II)I */ JNIEXPORT jint JNICALL Java_com_zj_ndktest_MyNDK_HelloIntegerDivide (JNIEnv *, jclass, jint, jint); #ifdef __cplusplus } #endif #endif
#include <jni.h> #include "com_zj_ndktest_MyNDK.h" JNIEXPORT jstring JNICALL Java_com_zj_ndktest_MyNDK_Hello (JNIEnv *env, jclass cla) { return env->NewStringUTF("Hello JNI"); } /* * Class: com_zj_ndktest_MyNDK * Method: HelloIntegerAdd * Signature: (II)I */ JNIEXPORT jint JNICALL Java_com_zj_ndktest_MyNDK_HelloIntegerAdd (JNIEnv *env, jclass cla, jint a, jint b) { jint c; c = a + b; return c; } /* * Class: com_zj_ndktest_MyNDK * Method: HelloIntegerSubtract * Signature: (II)I */ JNIEXPORT jint JNICALL Java_com_zj_ndktest_MyNDK_HelloIntegerSubtract (JNIEnv *env, jclass cla, jint a, jint b) { jint c; c = a - b; return c; } /* * Class: com_zj_ndktest_MyNDK * Method: HelloIntegerMultiply * Signature: (II)I */ JNIEXPORT jint JNICALL Java_com_zj_ndktest_MyNDK_HelloIntegerMultiply (JNIEnv *env, jclass cla, jint a, jint b) { jint c; c = a * b; return c; } /* * Class: com_zj_ndktest_MyNDK * Method: HelloIntegerDivide * Signature: (II)I */ JNIEXPORT jint JNICALL Java_com_zj_ndktest_MyNDK_HelloIntegerDivide (JNIEnv *env, jclass cla, jint a, jint b) { jint c; c = a / b; return c; }
############################################################################################3
实现double输入,double输出
修改MyNDK.java如下:
package com.zj.ndktest; /** * Created by root on 15-11-25. */ public class MyNDK { static { System.loadLibrary("hello-jni"); } public static native String Hello(); public static native double HelloDoubleAdd(double a, double b); public static native double HelloDoubleSubtract(double a, double b); public static native double HelloDoubleMultiply(double a, double b); public static native double HelloDoubleDivide(double a, double b); }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_double_add" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_double_subtarct" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_double_multiply" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_double_divide" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
package com.zj.ndktest; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.widget.TextView; public class MainActivity extends Activity { private TextView mTextView; private TextView mTextDoubleAdd; private TextView mTextDoubleSubtract; private TextView mTextDoubleMultiply; private TextView mTextDoubleDivide; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mTextView = (TextView)findViewById(R.id.text_view); mTextView.setText(MyNDK.Hello()); StringBuffer text_double_add = new StringBuffer("3.0 + 5.0 ="); text_double_add.append(MyNDK.HelloDoubleAdd(3.0, 5.0)); mTextDoubleAdd = (TextView)findViewById(R.id.text_double_add); mTextDoubleAdd.setText(text_double_add.toString()); StringBuffer text_double_subtract = new StringBuffer("33.0 - 100.0 = "); text_double_subtract.append(MyNDK.HelloDoubleSubtract(33.0, 100.0)); mTextDoubleSubtract = (TextView)findViewById(R.id.text_double_subtarct); mTextDoubleSubtract.setText(text_double_subtract.toString()); StringBuffer text_double_multiply = new StringBuffer("6.0 * 23.0 = "); text_double_multiply.append(MyNDK.HelloDoubleMultiply(6.0, 23.0)); mTextDoubleMultiply = (TextView)findViewById(R.id.text_double_multiply); mTextDoubleMultiply.setText(text_double_multiply.toString()); StringBuffer text_double_divide = new StringBuffer("90.0 / 32.0 = "); text_double_divide.append(MyNDK.HelloDoubleDivide(90.0, 32.0)); mTextDoubleDivide = (TextView)findViewById(R.id.text_double_divide); mTextDoubleDivide.setText(text_double_divide.toString()); } }
点击MyNDK.java,右键->External Tools->javah,生成新的com_zj_ndktest_MyNDK.h:
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_zj_ndktest_MyNDK */ #ifndef _Included_com_zj_ndktest_MyNDK #define _Included_com_zj_ndktest_MyNDK #ifdef __cplusplus extern "C" { #endif /* * Class: com_zj_ndktest_MyNDK * Method: Hello * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_zj_ndktest_MyNDK_Hello (JNIEnv *, jclass); /* * Class: com_zj_ndktest_MyNDK * Method: HelloDoubleAdd * Signature: (DD)D */ JNIEXPORT jdouble JNICALL Java_com_zj_ndktest_MyNDK_HelloDoubleAdd (JNIEnv *, jclass, jdouble, jdouble); /* * Class: com_zj_ndktest_MyNDK * Method: HelloDoubleSubtract * Signature: (DD)D */ JNIEXPORT jdouble JNICALL Java_com_zj_ndktest_MyNDK_HelloDoubleSubtract (JNIEnv *, jclass, jdouble, jdouble); /* * Class: com_zj_ndktest_MyNDK * Method: HelloDoubleMultiply * Signature: (DD)D */ JNIEXPORT jdouble JNICALL Java_com_zj_ndktest_MyNDK_HelloDoubleMultiply (JNIEnv *, jclass, jdouble, jdouble); /* * Class: com_zj_ndktest_MyNDK * Method: HelloDoubleDivide * Signature: (DD)D */ JNIEXPORT jdouble JNICALL Java_com_zj_ndktest_MyNDK_HelloDoubleDivide (JNIEnv *, jclass, jdouble, jdouble); #ifdef __cplusplus } #endif #endif
#include <jni.h> #include "com_zj_ndktest_MyNDK.h" JNIEXPORT jstring JNICALL Java_com_zj_ndktest_MyNDK_Hello (JNIEnv *env, jclass cla) { return env->NewStringUTF("Hello JNI"); } /* * Class: com_zj_ndktest_MyNDK * Method: HelloDoubleAdd * Signature: (DD)D */ JNIEXPORT jdouble JNICALL Java_com_zj_ndktest_MyNDK_HelloDoubleAdd (JNIEnv *env, jclass cla, jdouble a, jdouble b) { jdouble c; c = a + b; return c; } /* * Class: com_zj_ndktest_MyNDK * Method: HelloDoubleSubtract * Signature: (DD)D */ JNIEXPORT jdouble JNICALL Java_com_zj_ndktest_MyNDK_HelloDoubleSubtract (JNIEnv *env, jclass cla, jdouble a, jdouble b) { jdouble c; c = a - b; return c; } /* * Class: com_zj_ndktest_MyNDK * Method: HelloDoubleMultiply * Signature: (DD)D */ JNIEXPORT jdouble JNICALL Java_com_zj_ndktest_MyNDK_HelloDoubleMultiply (JNIEnv *env, jclass cla, jdouble a, jdouble b) { jdouble c; c = a * b; return c; } /* * Class: com_zj_ndktest_MyNDK * Method: HelloDoubleDivide * Signature: (DD)D */ JNIEXPORT jdouble JNICALL Java_com_zj_ndktest_MyNDK_HelloDoubleDivide (JNIEnv *env, jclass cla, jdouble a, jdouble b) { jdouble c; c = a / b; return c; }
###################################################################
输入boolean,输出boolean
修改MyNDK.java如下:
package com.zj.ndktest; /** * Created by root on 15-11-25. */ public class MyNDK { static { System.loadLibrary("hello-jni"); } public static native String Hello(); public static native boolean HelloBoolean(boolean b); }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_boolean_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
package com.zj.ndktest; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.widget.TextView; public class MainActivity extends Activity { private TextView mTextView; private TextView mTextBoolean; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mTextView = (TextView)findViewById(R.id.text_view); mTextView.setText(MyNDK.Hello()); StringBuffer text_boolean = new StringBuffer("True = "); text_boolean.append(MyNDK.HelloBoolean(true)); mTextBoolean = (TextView)findViewById(R.id.text_view); mTextBoolean.setText(text_boolean.toString()); } }
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_zj_ndktest_MyNDK */ #ifndef _Included_com_zj_ndktest_MyNDK #define _Included_com_zj_ndktest_MyNDK #ifdef __cplusplus extern "C" { #endif /* * Class: com_zj_ndktest_MyNDK * Method: Hello * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_zj_ndktest_MyNDK_Hello (JNIEnv *, jclass); /* * Class: com_zj_ndktest_MyNDK * Method: HelloBoolean * Signature: (Z)Z */ JNIEXPORT jboolean JNICALL Java_com_zj_ndktest_MyNDK_HelloBoolean (JNIEnv *, jclass, jboolean); #ifdef __cplusplus } #endif #endif
#include <jni.h> #include "com_zj_ndktest_MyNDK.h" JNIEXPORT jstring JNICALL Java_com_zj_ndktest_MyNDK_Hello (JNIEnv *env, jclass cla) { return env->NewStringUTF("Hello JNI"); } JNIEXPORT jboolean JNICALL Java_com_zj_ndktest_MyNDK_HelloBoolean (JNIEnv *env, jclass cla, jboolean b) { jboolean res = JNI_FALSE; if (b == JNI_TRUE) { res = JNI_TRUE; } else { res = JNI_FALSE; } return res; }
##################################################################################
输入jstring,double,输出jstring
修改MyNDK.java:
package com.zj.ndktest; /** * Created by root on 15-11-25. */ public class MyNDK { static { System.loadLibrary("hello-jni"); } public static native String Hello(); public static native String sprint(String format, double x); }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_string_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
package com.zj.ndktest; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.widget.TextView; public class MainActivity extends Activity { private TextView mTextView; private TextView mTextString; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mTextView = (TextView)findViewById(R.id.text_view); mTextView.setText(MyNDK.Hello()); mTextString = (TextView)findViewById(R.id.text_string_view); mTextString.setText(MyNDK.sprint("Amount due = %8.2f", 33.4556)); } }
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_zj_ndktest_MyNDK */ #ifndef _Included_com_zj_ndktest_MyNDK #define _Included_com_zj_ndktest_MyNDK #ifdef __cplusplus extern "C" { #endif /* * Class: com_zj_ndktest_MyNDK * Method: Hello * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_zj_ndktest_MyNDK_Hello (JNIEnv *, jclass); /* * Class: com_zj_ndktest_MyNDK * Method: sprint * Signature: (Ljava/lang/String;D)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_zj_ndktest_MyNDK_sprint (JNIEnv *, jclass, jstring, jdouble); #ifdef __cplusplus } #endif #endif
#include <jni.h> #include <stdlib.h> #include <stdio.h> #include "com_zj_ndktest_MyNDK.h" JNIEXPORT jstring JNICALL Java_com_zj_ndktest_MyNDK_Hello (JNIEnv *env, jclass cla) { return env->NewStringUTF("Hello JNI"); } JNIEXPORT jstring JNICALL Java_com_zj_ndktest_MyNDK_sprint (JNIEnv *env, jclass cla, jstring format, jdouble x) { const char* cformat; jstring ret; cformat = env->GetStringUTFChars(format, NULL); char *cret; cret = (char*)malloc(strlen(cformat)+8); sprintf(cret, cformat, x); ret = env->NewStringUTF(cret); free(cret); env->ReleaseStringUTFChars(format, cformat); return ret; }
#############################################################
访问实例域
删除MyNDK.java文件,新建Employee.java文件:
package com.zj.ndktest; /** * Created by root on 15-11-25. */ public class Employee { static { System.loadLibrary("hello-employee"); } private String name; private double salary; public Employee(String n, double s) { name = n; salary = s; } public native void raiseSalary(double byPercent); public String print() { StringBuffer sb = new StringBuffer(); sb.append(name); sb.append(" "); sb.append(salary); return sb.toString(); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/text_instance_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
package com.zj.ndktest; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.widget.TextView; public class MainActivity extends Activity { private TextView mTextInstance;; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); Employee em = new Employee("hello world", 32000); em.raiseSalary(5); mTextInstance = (TextView)findViewById(R.id.text_instance_view); mTextInstance.setText(em.print()); } }
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.zj.ndktest" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" ndk { // moduleName "hello-jni" moduleName "hello-employee" } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.0' }
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_zj_ndktest_Employee */ #ifndef _Included_com_zj_ndktest_Employee #define _Included_com_zj_ndktest_Employee #ifdef __cplusplus extern "C" { #endif /* * Class: com_zj_ndktest_Employee * Method: raiseSalary * Signature: (D)V */ JNIEXPORT void JNICALL Java_com_zj_ndktest_Employee_raiseSalary (JNIEnv *, jobject, jdouble); #ifdef __cplusplus } #endif #endif
修改Android.mk:
LOCAL_PATH := ${call my-dir} include $(CLEAR_VARS) LOCAL_MODULE := hello-employee LOCAL_SRC_FILES := main.cpp include ${BUILD_SHARED_LIBRARY}
#include <jni.h> #include <stdlib.h> #include <stdio.h> #include "com_zj_ndktest_Employee.h" JNIEXPORT void JNICALL Java_com_zj_ndktest_Employee_raiseSalary (JNIEnv *env, jobject obj, jdouble byPercent) { /* get the class */ jclass class_Employee = env->GetObjectClass(obj); /* get the field ID */ jfieldID id_salary = env->GetFieldID(class_Employee, "salary", "D"); /* get the field value */ jdouble salary = env->GetDoubleField(obj, id_salary); salary *= 1 + byPercent / 100; /* set the field value */ env->SetDoubleField(obj, id_salary, salary); }
######################################################################################
输入double数组,输出double数组
删除Employee.java,新建MyNDK.java:
package com.zj.ndktest; /** * Created by root on 15-11-25. */ public class MyNDK { static { System.loadLibrary("hello-jni"); } public static native String Hello(); public native double[] HelloDoubleArray(double[] arr); public String print(double[] arr) { StringBuffer sb = new StringBuffer(); for(double dou : arr) { sb.append(dou); sb.append(" "); } return sb.toString(); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_array_view1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_array_view2" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
package com.zj.ndktest; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.widget.TextView; public class MainActivity extends Activity { private TextView mTextView; private TextView mTextArray1; private TextView mTextArray2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mTextView = (TextView)findViewById(R.id.text_view); mTextView.setText(MyNDK.Hello()); double[] arr = new double[]{1,2,3,4}; MyNDK my = new MyNDK(); mTextArray1 = (TextView)findViewById(R.id.text_array_view1); mTextArray1.setText(my.print(arr)); double[] arr2 = my.HelloDoubleArray(arr); mTextArray2 = (TextView)findViewById(R.id.text_array_view2); mTextArray2.setText(my.print(arr2)); } }
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.zj.ndktest" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" ndk { moduleName "hello-jni" //moduleName "hello-employee" } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.0' }
LOCAL_PATH := ${call my-dir} include $(CLEAR_VARS) LOCAL_MODULE := hello-jni LOCAL_SRC_FILES := main.cpp include ${BUILD_SHARED_LIBRARY}
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_zj_ndktest_MyNDK */ #ifndef _Included_com_zj_ndktest_MyNDK #define _Included_com_zj_ndktest_MyNDK #ifdef __cplusplus extern "C" { #endif /* * Class: com_zj_ndktest_MyNDK * Method: Hello * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_zj_ndktest_MyNDK_Hello (JNIEnv *, jclass); /* * Class: com_zj_ndktest_MyNDK * Method: HelloDoubleArray * Signature: ([D)[D */ JNIEXPORT jdoubleArray JNICALL Java_com_zj_ndktest_MyNDK_HelloDoubleArray (JNIEnv *, jobject, jdoubleArray); #ifdef __cplusplus } #endif #endif
#include <jni.h> #include <stdlib.h> #include <stdio.h> #include "com_zj_ndktest_Employee.h" extern "C" { /* * Class: com_zj_ndktest_MyNDK * Method: Hello * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_zj_ndktest_MyNDK_Hello (JNIEnv *env, jclass cla) { return env->NewStringUTF("hello world"); } /* * Class: com_zj_ndktest_MyNDK * Method: HelloDoubleArray * Signature: ([D)[D */ JNIEXPORT jdoubleArray JNICALL Java_com_zj_ndktest_MyNDK_HelloDoubleArray (JNIEnv *env, jobject obj, jdoubleArray dou) { jsize leng = env->GetArrayLength(dou); jdoubleArray arr = env->NewDoubleArray(leng); double *a = env->GetDoubleArrayElements(arr, NULL); double *d = env->GetDoubleArrayElements(dou, NULL); for (int i = 0; i < leng; i++) { a[i] = d[i] * 2; } env->ReleaseDoubleArrayElements(dou, d, NULL); env->ReleaseDoubleArrayElements(arr, a, NULL); return arr; } }