Run the NDK program with Android Studio

Source: Internet
Author: User

Recently prepared to study the Android dual-process daemon, since the previous use of eclipse to write JNI habits, and now mainly use 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 rest must not be described here.

2. Example introduction 1), Project creation

Open as to create a project input package name Com.cayden.jnis
If you use the layout and activity generated by as, there will be a problem with subsequent runs.
Specific follow-up will be explained in the question section

2), creating a JNI Java class

Java calls C/+ + requires JNI Intermediate bridge
The specific code is as follows

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
Into the build/intermediates/classes/debug.

Then create the JNI directory under Src/main, cut the previously generated file com_cayden_jnis_jniutils.h to this directory, 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), modify activity and make calls

Specific 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 in the Local.properties file
Modify the 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. Operation effect

After running, it shows the content written in C I ' m jni

But go back to the project directory to find that the generated so file is not seen but we will be the APK anti-compilation can be seen, seemingly as still pretty strong.
You can look at the post-compilation

4, problem finishing 1) Error: "NDK integration is deprecated in the current plugin"

Workaround: Open the Gradle.properties file under the project directory and write the following line in the file:
Android.usedeprecatedndk=true
No accident, again compile the project, the NDK environment 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

Run the NDK program with Android Studio

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.