Use Android Studio to run the ndk Program

Source: Internet
Author: User

Use Android Studio to run the ndk Program

Recently, I want to study the android dual-process daemon. Because I used to write jni using eclipse, I am mainly using the as tool. Try to write a demo here and study the dual-process daemon.

1. required tools

Android studio 1.4
Ndk: android-ndk-r9d
.... Other requirements are not described here

2. Example 1) create a project

Open as to create a project input package nameCom. cayden. jnis
If the layout and activity generated by as are used, problems may occur in subsequent operations.
The problem will be explained later.

2) create a jni java class

Java calls c/c ++ requires a jni intermediate Bridge
The Code is as follows:

package com.cayden.jnis;/** * Created by cuiran on 16/2/27. */public class JniUtils {    public native String getCLanguageString();    static {        System.loadLibrary("JniUtils");    }}
3) generate h to create a c file

Compile the class file to generate the corresponding H file
View the commands in the image
Go to build/intermediates/classes/debug

Create the jni directory under src/main, cut the previously generated file com_cayden_jnis_JniUtils.h to this directory, and create a new file jnitest. c.

/// Created by Cui ran on 16/2/27. // # include "inline"/** Class: com_cayden_jnis_JniUtils * Method: getCLanguageString * Signature: () Ljava/lang/String; */JNIEXPORT jstring JNICALL inline (JNIEnv * env, jobject obj) {return (* env)-> NewStringUTF (env, "I'm jni ");}
4) modify the activity and call it.

Reference Code

package com.cayden.jnis;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        JniUtils jniUtils=new JniUtils();        TextView tv=(TextView)findViewById(R.id.textView);        tv.setText("jni result:"+jniUtils.getCLanguageString());    }}
5) configure the ndk and the so file name generated

AddNdk. dir =/Users/cuiran/tool/android-ndk-r9d
Modify the build. gradle File

Apply plugin: 'com. android. application 'android {compileSdkVersion 19 buildtoolsversion' 19. 1.0 'defaultconfig {applicationId "com. cayden. jnis "minSdkVersion 14 targetSdkVersion 23 versionCode 1 versionName" 1.0 "} buildTypes {release {minifyEnabled false proguardFiles getdefadefaproguardfile('proguard-android.txt '), 'proguard-rules. pro 'ndk {moduleName "JniUtils" // generated so name abiFilters "armeabi ", "Armeabi-v7a", "x86" // outputs so libraries under the specified three abi architectures. } Debug {ndk {moduleName "JniUtils" // generated so name abiFilters "armeabi", "armeabi-v7a ", "x86" // output the so library under three abi architectures. }}} Dependencies {compile fileTree (dir: 'libs', include: ['*. jar'])}
3. Running Effect

After running, the content written in c is displayed.I'm jni

However, if you go back to the project directory and find that you have not seen the generated so file, you can see it by decompiling the apk. It seems that the as is quite powerful.
Let's take a look at the decompiled

4. troubleshooting 1) error: "NDK integration is deprecated in the current plugin"

Solution: Open the gradle. properties file under the project directory, and write the following line into the file:
Android. useDeprecatedNdk = true
If nothing happens, compile the project again. The NDK environment is OK.

2) error: jni. h: No such file or directory

Solution: Change compileSdkVersion and targetSdkVersion to 19 and the compilation is successful.

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.