Android Studio uses JNI

Source: Internet
Author: User
Tags mul

1. Add Native Interface

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


2.build->make Project
Verify that there are no other errors in the project and compile the project to generate a. class file
Inside the Build/intermediates/classes/debug.

3.javah generate the. h file

Under the App/src/main/java directory of the Project
CMD executes the Javah com.example.caculate.MainActivity command, the. h file is generated
Create the JNI directory and copy the. h

4.jni directory implementation. C
Create a new. C implementation. Functions in the. h


5.build->make Project

Will error
Add the NDK path to the Local.properties file
Sdk.dir=c\:\\users\\alif\\appdata\\local\\android\\sdk
ndk.dir=i\:\\android\\android-ndk-r11c

Add the name of so in the build.gradle below the app
\app\build.gradle
NDK {
ModuleName "Jnitest"
Abifilters "Armeabi", "armeabi-v7a", "x86"
}


6.build->make Project can compile the so file.
The resulting so is below app/build/intermediates/ndk/debug/lib


We add a android.mk file under the JNI directory
Local_path: = $ (call My-dir)
Include $ (clear_vars)
Local_module: = Jnitest
Local_src_files: = main.c
Include $ (build_shared_library)


Then ndk-build generate the Libs directory

Changing the Libs directory name to Jnilibs will be automatically packaged into the APK.

. h and. c Files

/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class com_example_caculate_mainactivity*/#ifndef _included_com_example_caculate_mainactivity#define_included_com_example_caculate_mainactivity#ifdef __cplusplusextern "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<jni.h>#defineJintjnicall#ifndef _included_com_example_caculate_mainactivity#define_included_com_example_caculate_mainactivity#ifdef __cplusplusextern "C" {#endifjniexport jintjnicall java_com_example_caculate_mainactivity_add (jnienv*env, Jobject obj, jdouble num1, jdouble num2) {    return(Jint) (NUM1 +num2);} Jniexport jintjnicall java_com_example_caculate_mainactivity_sub (jnienv*env, Jobject obj, jdouble num1, jdouble num2) {    return(Jint) (NUM1-num2);} Jniexport jintjnicall Java_com_example_caculate_mainactivity_mul (jnienv*env, Jobject obj, jdouble num1, jdouble num2) {    return(Jint) (NUM1 *num2);} 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);} #ifdef __cplusplus}#endif#endif

Using dynamic Enrollment JNI

1. The function name is too long because it is a Javah generated header file, the name is too long
2. Every class has to be javah once, very troublesome

Generate write in the JNI directory. C

#include <jni.h>#include<stdio.h>//#include <assert.h>#include <stdlib.h>#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>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);}//binding Tables for Java and JNI functionsStaticJninativemethod 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},};//registering the native method in JavaStatic intRegisternativemethods (jnienv* env,Const Char*ClassName, Jninativemethod* Gmethods,intnummethods)    {Jclass clazz; Clazz= (*env)Findclass (env, className); if(Clazz = =NULL) {        returnJni_false; }    if(*env)->registernatives (env, Clazz, Gmethods,nummethods) <0){        returnJni_false; }    returnjni_true;}intRegister_ndk_load (JNIENV *env) {    returnRegisternativemethods (ENV,"com/example/caculate/mainactivity", Gmethods,sizeof(gmethods)/sizeof(gmethods[0])); //Nelem (Gmethods));}jniexport jint jni_onload (JAVAVM* VMS,void*reserved) {jnienv* env =NULL; Jint result= -1; if((*VM)->getenv (VM, (void* *) &env, jni_version_1_4)! =JNI_OK) {        returnresult;    } register_ndk_load (env); //returns the version of JNI    returnjni_version_1_4;}

Writing android.mk files


Local_path: = $ (call My-dir)
Include $ (clear_vars)
Local_prelink_module: = False
Local_module: = Jnitest_jni
Local_src_files: = myjnicalc.c
Local_shared_libraries: = Libandroid_runtime
Include $ (build_shared_library)

Generating so files using Ndk-build

or move it to the Jnilibs directory.


Last Successful modification


78+11 +1 = 90

Source: http://pan.baidu.com/s/1kULyvDd Password M8ns

Android Studio uses JNI

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.