[Go to] Create Hello-JNI with Android Studio, hello-jniandroid
[Go to] Create Hello-JNI with Android Studio
From: https://codelabs.developers.google.com/codelabs/android-studio-jni/index.html? Index = .. % 2F... % 2 Findex #4
FQ is required for access.
The reason why the image is not translated into Chinese is that the image is very detailed and cannot be understood in English. It can be completed step by step based on the image. In addition, developers should be able to read the English Technical blog.
1. Overview
In this codelab, you'll learn how to use Android Studio to start Android NDK project development.
2. Create Java Sample App
3. AddJNI Build Capability to HelloAndroidJni Project
Android Studio supports nativedevelopment via experimental plugin developed by Google, let's add it into ourproject.
classpath 'com.android.tools.build:gradle:2.1.0'
With your latest version [it does not have to be 0.7.2]:
classpath 'com.android.tools.build:gradle-experimental:0.7.2'
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { applicationId "com.google.sample.helloandroidjni" minSdkVersion 22 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}// others below this line: no change
With:
apply plugin: 'com.android.model.application'model { android { compileSdkVersion 23 buildToolsVersion "23.0.3" defaultConfig { applicationId "com.google.sample.helloandroidjni" minSdkVersion.apiLevel 22 targetSdkVersion.apiLevel 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles.add(file('proguard-android.txt')) } } }}// others below this line: no change
NOTE: the version numbers may be different on your system, and you do not need to change the version number -- just use them as is. Only changing the highlighted part wocould be fine!
4. Add JNI Code Into Project
buildTypes {...}// New codendk { moduleName "hello-android-jni"}// New code finished
... // new code static { System.loadLibrary("hello-android-jni"); } publicnativeString getMsgFromJni(); // new code done} // class MainActivity
Note:
- Make sure library name is the same as moduleName inside build. gradle
- The "Build" step is just to build, do not load the built apk yet; if you load it, it will crash since there is no native implementation for getMsgFromJni () yet
- Select function "getMsgFromJni ()".
- Wait for context aware menu prompt to appear.
- Click on to bring up the popup
- Select "Create Function Java_com_google_example_helloandroidjni_MainActivity_getMsgFromJni ".
- Android Studio creates a prototype function for getMsgFromJNI () in hello-android-jni.c file under the "jni" folder. Both got created at once!
#include<jni.h> JNIEXPORT jstring JNICALLJava_com_google_sample_helloandroidjni_MainActivity_getMsgFromJni(JNIEnv *env, jobject instance) { // TODO return (*env)->NewStringUTF(env, returnValue);}
- Replace "returnValue" in the above code with our own message:
// TODOreturn (*env)->NewStringUTF(env, "Hello From Jni");
- Add an ID to the existing TextView.
Open "Android Studio" pane, "res"> "layout"> "content_main.xml" [if you have chosen template "Empty Activity" in step "Create Java Sample App ", you file might be "activity_main.xml" instead], select "design" view, and click or "Hello World", inside "Properties" pane, put "@ + id/Jni_msgView""ID"Field:
[The other way is to directly add into "text" view, and put id inAndroid: id = "@ + id/jni_msgView ".]
- Display our jni message in the TextView.
In MainActivity: onCreate () function, append following code to the end of the function:
((TextView) findViewById(R.id.jni_msgView)).setText(getMsgFromJni());
5. Debugging JNI Code
[Note: if you are using Android StudioRC 1.5 or better, you can set a breakpoint on getMsgFromJni () in Java code and "trace into" JNI code]
Project source https://github.com/leon-HM/HelloAndroidJni