Androidstudio 2.3.3 JNI Process Rollup (1): 1, write your own C file and use (original)

Source: Internet
Author: User

1, write Java code, specify the Lib and native methods.
Package com.taven.myapplication;

Package com.taven.myapplication;

Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;

public class Mainactivity extends Appcompatactivity {

static {
System.loadlibrary ("jni-lib-1");
}

Private native void Sayhellojni ();

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (Savedinstancestate)
Setcontentview (R.layout.activity_main);
}
}

2. Create a new JNI folder and create a new android.mk file in the Jni folder

The contents of the Mk file are as follows:
#指定编译的文件夹 Specify the current file directory
Local_path: = $ (call My-dir)
#编译器会定义很多的临时变量, intermediate variables, preferably emptying all intermediate variables.
Include $ (clear_vars)
#编译器编译出来的模块名称
local_module:= jni-lib-1
#编译器编译出来的源代码的名称
local_src_files:= jni-lib-1.c
#编译一个动态库, Static Library
#静态库 file name. A contains all of the functions and the function is dependent on the run, large size, including all the APIs
#动态库 file name. So contains functions that do not contain the dependencies of function runs, small size, and when running, go to the operating system to find the required API
Include $ (build_shared_library)

3. Add the Mk file path under the Build.gradle of the app

Externalnativebuild {
Ndkbuild {
Path file ("Src\\main\\jni\\android.mk")
}
}

4, you can add the exclusion option, specify to generate the specified platform so file.

Add the following code to the Defaultconfig in the Android node in the App->build.gradle file:
NDK {
Abifilters "Armeabi", "armeabi-v7a", "x86"
}

5, regenerate the project (Rebuild), when the mouse is placed on the native method, the generation of JNI code hints will appear. The Jni file is automatically generated by pressing Alt+enter.

6. Fill in the C code



7, compile the project again, the resulting so library directory in the app/build/intermedieates/ndkbuild/debug/obj/local/directory;

8, the operation can be.

Androidstudio 2.3.3 JNI Process Rollup (1): 1, write your own C file and use (original)

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.