Complete steps for establishing JNI and Java applications under Android SDK-call C ++ code in Android Java

Source: Internet
Author: User

There are usually two ways to develop Android applications: ndk and eclipse, or add them directly in the android SDK. This section describes how to add an application in the android SDK environment. It involves JNI and Java calls to JNI. The JNI code file is C ++.

 

The first step is to establish the JNI-Layer Code. Note that the JNI code path has a great relationship with the Java code path and must be consistent.

Create the jni c ++ file android_test.cpp in the frameworks/base/CORE/JNI directory. The content is as follows:

 

# Define log_tag "FMC"

 

# Include "JNI. H"

# Include "android_runtime/androidruntime. H"

# Include <nativehelper/jnihelp. h>

 

# Include "utils/log. H"

 

 

 

Extern "C "{

Int test (void); // this function can be from C code or C library

 

};

 

 

Namespace Android // note that the namespace is closely related to all the paths of JNI called by Java, and The namespace cannot be used at will.

{

Static jint android_test (jnienv * ENV, jobject clazz)

{

Return test ();

}

 

 

Static jninativemethod method_table [] = {

{"My_test", "() I", (void *) android_test}, // The first parameter is a string, which is the function name seen at the Java layer;

// The second parameter is the parameter description of the function,

// The Java code uses this parameter to know the parameters of the called function;

// The third parameter is the c Function actually called in the JNI code.

}

 

Int register_android_test (jnienv * env) // This is the JNI registration function. Android. test is the package path seen at the Java call layer (Step 3,

// If register_android_hardware_test is used,

// The Java call layer needs to import the Android. Hardware. Test package.

 

{

Return androidruntime: registernativemethods (ENV, "android/test", method_table, nelem (method_table ));

/* The second parameter is associated with the path of the Java file call package at the application layer. The "android/test" string parameter and

The complete path of frameworks/base/CORE/Java/Android/test. Java corresponds to the package path anroid. Test, Android. Test. Test */

}

 

 

};

 

Step 2: add the JNI code registration function to the androidruntime Runtime Library. Edit the frameworks/base/CORE/JNI/androidruntime. cpp file:

Add the following two lines according to the method on the file:

..........

Extern int register_android_test (jnienv * env); // This is the registration function in the android_test.cpp file.

..........................

Static const regjnirec gregjni [] = {

.........

.........

Reg_jni (register_android_test ),

.........

.......

};

 

 

 

Step 3: Create a Java call layer and create the file test. Java in the frameworks/base/CORE/Java/Android/test directory. The content is as follows:

 

 

Package Android. test;

 

 

/**

* Native methods for managing FM.

*

* {@ Hide}

*/

Public class test {

Private native int my_test (); // The final code that can be called in the android application code

 

Public test () // Constructor

{

// Add your code here .............

}

};

 

 

Step 4: Create your Android Application in the packages/apps/test directory (the specific method is not described here). Suppose test_app.java is the main file of the application, the content is as follows:

Package com. app;

 

Import Android. Test; // import your own package

 

Use this in the Code:

Test test;

Test. my_test (); // The final call

 

Android sdk jni implementation, Android sdk jni c ++ code, Android calls C ++, and establishes the complete steps of JNI and Java applications under Android SDK. Android Java calls C ++ code

 

 

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.