Recently, we are ready to study the Android dual-process daemon, because it was used to write the JNI habit with eclipse. Today it is mainly used as tools. Try to write a demo here and then study the two-process daemon.
1. Required Tools
Android Studio 1.4
ndk:android-ndk-r9d
.... The other must not be described here
2, Demo sample introduction 1), Project creation
Open as to create a project input package name Com.cayden.jnis
It is assumed that the layout and activity generated with as will fail in execution.
Details may be explained in the question section
2), creating a JNI Java class
Java calls to C + + requires JNI Intermediate bridge
Detailed code such as the following
package com.cayden.jnis;/** * Created by cuiran on 16/2/27. */publicclass JniUtils { publicnativegetCLanguageString(); static { System.loadLibrary("JniUtils"); }}
3), generate h create C file
Need to compile the class file to generate the corresponding H file
See the commands in the picture in detail
Into the build/intermediates/classes/debug.
Then create the Jni folder under Src/main, cut the previously generated file Com_cayden_jnis_jniutils.h to this folder, and new file jnitest.c
//// Created by 崔冉 on 16/2/27.//"com_cayden_jnis_JniUtils.h"/* * Class: com_cayden_jnis_JniUtils * Method: getCLanguageString * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_cayden_jnis_JniUtils_getCLanguageString (JNIEnv *env, jobject obj){ return (*env)->NewStringUTF(env,"I‘m jni");}
4), change activity and make calls
Detailed reference Code
PackageCom.cayden.jnis;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.TextView; Public class mainactivity extends Activity { @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Jniutils jniutils=NewJniutils (); TextView tv= (TextView) Findviewbyid (R.id.textview); Tv.settext ("JNI Result:"+jniutils.getclanguagestring ()); }}
5), configure the NDK and its generated so file name
Add ndk.dir=/users/cuiran/tool/android-ndk-r9d to local.properties file
Change Build.gradle file
Apply plugin:' Com.android.application 'Android {Compilesdkversion +Buildtoolsversion' 19.1.0 'Defaultconfig {ApplicationID"Com.cayden.jnis"Minsdkversion -Targetsdkversion atVersioncode1Versionname"1.0"} buildtypes {release {minifyenabledfalseProguardfiles Getdefaultproguardfile (' Proguard-android.txt '),' Proguard-rules.pro 'ndk{ModuleName"Jniutils" //generated so nameAbifilters"Armeabi","armeabi-v7a","x86" //output specifies the so library under three ABI architectures. }} debug{ndk{modulename"Jniutils" //generated so nameAbifilters"Armeabi","armeabi-v7a","x86" //output specifies the so library under three ABI architectures. }}}}dependencies {compile Filetree (dir:' Libs ', include: [' *.jar '])}
3. Execution effect
After execution, shows the content written in C I ' m jni
But go back to the project folder to find that the generated so file is not seen but we will be able to see the APK anti-compilation. Seems to be quite powerful.
Can look at the anti-compiled
4, problem finishing 1) Error: "NDK integration is deprecated in the current plugin"
Workaround: Open the Gradle.properties file under the project folder. and write the following line in the file:
Android.usedeprecatedndk=true
No accident, compile the PROJECT,NDK environment again this piece is OK.
2) Error: Error:jni.h:no such file or directory
Workaround: Change compilesdkversion and Targetsdkversion to 19 to compile and pass the
Performing NDK programs with Android Studio