android ndk 入门2 - 基本方法实现

2024-06-21 15:48

本文主要是介绍android ndk 入门2 - 基本方法实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 新建工程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

app/build.gradle上加入:

apply plugin: 'com.android.application'android {compileSdkVersion 23buildToolsVersion "23.0.2"defaultConfig {applicationId "com.zj.ndktest"minSdkVersion 15targetSdkVersion 23versionCode 1versionName "1.0"ndk { moduleName "hello-jni"}}buildTypes {release {minifyEnabled falseproguardFiles 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"><TextViewandroid: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;@Overrideprotected 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

新建jni/Android.mk:

LOCAL_PATH := ${call my-dir}
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := main.cpp
include ${BUILD_SHARED_LIBRARY}

新建main.cpp:

#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");
}

运行,即可完成简单的ndk程序


####################################################################


实现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);

在activity_main.xml上加入:

    <TextViewandroid:id="@+id/text_integer_add"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/text_integer_subtarct"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/text_integer_multiply"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid: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;@Overrideprotected 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());}
}

点击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:    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

main.cpp修改为:

#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);
}

修改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"><TextViewandroid:id="@+id/text_view"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/text_double_add"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/text_double_subtarct"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/text_double_multiply"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/text_double_divide"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;private TextView mTextDoubleAdd;private TextView mTextDoubleSubtract;private TextView mTextDoubleMultiply;private TextView mTextDoubleDivide;@Overrideprotected 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

修改main.cpp如下:

#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);
}

修改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"><TextViewandroid:id="@+id/text_view"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/text_boolean_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;private TextView mTextBoolean;@Overrideprotected 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());}
}

点击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:    HelloBoolean* Signature: (Z)Z*/
JNIEXPORT jboolean JNICALL Java_com_zj_ndktest_MyNDK_HelloBoolean(JNIEnv *, jclass, jboolean);#ifdef __cplusplus
}
#endif
#endif

修改main.cpp如下:

#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);
}

修改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"><TextViewandroid:id="@+id/text_view"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/text_string_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;private TextView mTextString;@Overrideprotected 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));}
}

点击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:    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

修改main.cpp如下:

#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();}
}

修改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"><TextViewandroid:id="@+id/text_instance_view"android:layout_width="match_parent"android:layout_height="wrap_content" />
</LinearLayout>

修改MainAcitvity.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 mTextInstance;;@Overrideprotected 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());}
}

修改app/build.gradle:

apply plugin: 'com.android.application'android {compileSdkVersion 23buildToolsVersion "23.0.2"defaultConfig {applicationId "com.zj.ndktest"minSdkVersion 15targetSdkVersion 23versionCode 1versionName "1.0"ndk {// moduleName "hello-jni"moduleName "hello-employee"}}buildTypes {release {minifyEnabled falseproguardFiles 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'
}

点击Employee.java,右键->Extrenal Tools->javah,生成com_zj_ndktest_Employee.h:
/* 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}

修改main.cpp:

#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();}
}

修改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"><TextViewandroid:id="@+id/text_view"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/text_array_view1"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/text_array_view2"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;private TextView mTextArray1;private TextView mTextArray2;@Overrideprotected 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));}
}

修改app/build.gradle:

apply plugin: 'com.android.application'android {compileSdkVersion 23buildToolsVersion "23.0.2"defaultConfig {applicationId "com.zj.ndktest"minSdkVersion 15targetSdkVersion 23versionCode 1versionName "1.0"ndk {moduleName "hello-jni"//moduleName "hello-employee"}}buildTypes {release {minifyEnabled falseproguardFiles 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'
}

修改Android.mk:

LOCAL_PATH := ${call my-dir}
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := main.cpp
include ${BUILD_SHARED_LIBRARY}

点击MyNDK,右键->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:    HelloDoubleArray* Signature: ([D)[D*/
JNIEXPORT jdoubleArray JNICALL Java_com_zj_ndktest_MyNDK_HelloDoubleArray(JNIEnv *, jobject, jdoubleArray);#ifdef __cplusplus
}
#endif
#endif

修改main.cpp:

#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;}}

运行:



这篇关于android ndk 入门2 - 基本方法实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1081651

相关文章

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同