AndroidStudio2.2 CMake Way to create a JNI project

Source: Internet
Author: User

About CMake: Starting with the androidStudio2.2 version, you can create a JNI project in CMake Way.


After opening Androidstudio, follow these steps:
1) file-new-new Project, note that support C + + is selected. And then all the way next until finish.


2) I have a compilation problem here; (Skip this step if you are not experiencing this problem)

This is because the newly created project does not have a default NDK configuration; The operation is as follows:
Right-click the project name and select Open Module Setting this:

Then note the red flag. Configure your NDK path, I use this version of 14b.
Click OK, then Androidstudio will be compiled automatically.

3) Take a look at the automatically generated code;

Here's an auto-generated Mainactivity.java red flag, which is called JNI.
The following system.loadlibrary is the load of the engine file named native-lib.so (cmake mode, the file is not visible in the project, but the compiled apk is there)
The native method above is declared in the Java code, and the JNI layer has the implementation of such a method.
Draw a circle on the left, click (or mouse on the method name above, hold down CTRL, click the left mouse button), you can directly index to the CPP file, the implementation of this method. Such as.


The first time you see this file may be a little strange, the following posted important code annotations;

#include <jni.h>#include<string>extern "C" //this sentence, said, according to the C way to compile, if not add this sentence, may lead to find this methodJniexport jstring Jnicall//this sentence, indicating that the return value is a Java string, as for Jniexport, Jnicall are JNI syntaxJava_test_hank_com_jni1011_mainactivity_stringfromjni (//This means that this is a method of implementing a Java class, and the full name of the Java class is test.hank.com.//jni1011. Mainactivity, the method is named StringfromjniJNIEnv *env,//Pointer ObjectJobject/* This*/) {std::stringHello ="Hello from C + +";//Create a String object, std:: The notation, STD is the C + + standard library, after two colons, plus string, is that this class is the C + + standard Library of the string class. Use this class to create a string objectreturnEnv->newstringutf (Hello.c_str ());//The return value, since Env is a pointer object, so call the method, Newstringutf () to construct a UTF-encoded string}

Run the project and you will see a line of string "Hello from C + +" displayed in the middle of the interface, which is obviously from the JNI layer.
When Android gave us a sample project, we just told us how to invoke Jni in Java. But JNI callback to the Java process, we need to manually modify the code.

4) manually modify the code to implement the JNI layer calls the C + + layer code;
1-Create a new Java class: Javacaller.java, and creates a method that indicates that this is the method that allows the JNI layer to be called.


2-jni calls a method of an object in Java, and also follows an object-oriented approach, and must have a reference to that object. So we're going to pass an object to the JNI layer first.
Look at the code.
In the Java layer, create a new native method, Setjavaobject, with a parameter of one object of the Javacaller class.


Put the mouse on the method name, press and hold Alt+enter, you will be prompted to create a Jni method, as the implementation of this native.
Choose to create this method, (here is a pit, the automatic generation of this method, no extern "C" lead to compile pass, probably the reason is to add the extern "C", it means to compile with C, the JNI docking should still be C, so add extern "C")
Then write the code to call this Setjavaobject method.
The next step is to continue into the JNI layer, where we aim to invoke a method of the Java class from the JNI layer.
So, write code in JNI to use this Java object passed in.
The code is as follows, with detailed annotations for each step:

In this case, the call process is complete.
Execute the code and you will find that this line is in the log.
10-11 04:23:51.112 4254-4254/test.hank.com.jni1011 D/hankzhou:param: Can I put Chinese in here? -Param2: Really can put Chinese yes

This is the log we printed in the Javacaller.java test method. Note that the JNI callback method for Java succeeds.

At this point, the example of the androidstudio-cmake approach to developing JNI is complete.
However, there are two points to explain.
1) There is a regular conversion of Java types into JNI types (see the Java layer's native method, and the C method of the JNI layer, the parameter list for both). Associated, but very different in wording)


Very abstract? Well, look at the picture.
This is the Java:

This is the JNI:

JNI in front of the two is what to do temporarily do not know, but it is obvious to see 3 parameters are string, to the jni here, it becomes jstring, this is the so-called translation.

2) A method of the JNI callback Java object, also has the type conversion;

Or look at the picture:
Java methods:


The method is created with reflection in JNI:


The arguments here, test is the method name, and it remains consistent.
The formal parameter and the return value must correspond exactly, there is a type conversion.
First look at the last V, the return value of the test method void.
Two ljava/lang/string in front; Corresponds to two strings. (multiple parameters, semicolon delimited)
Here is void and string, so what if it is another type? See the table above.

These are the entry-level instructions for developing JNI in the CMake way. More complex, write later.


AndroidStudio2.2 CMake Way to create a JNI project

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.