When developing with AndroidNDK, it is annoying to create a local code folder, generate a local code file, and create a compilation file for the local code. Especially when implementing local methods, it is also annoying because the local method name is too long. The naming convention for it is: Java_package-name_class-name_method-name (arguments), a little bit of a piece of attention will be spelling errors, leading to long debugging. Because we Don't have to deal with this kind of torture, and to avoid repeating the same thing (DRY-Don't Repeat Yourself), we wrote a Java program to do it.
This tool can check the Java files one by one, create a local method file, that is, a local code file, generate the Android. mk compilation file, update the Java file, and add System. loadLibrary.
The specific principle is as follows:
• Each Java file containing local code generates a local file containing all the local methods in the file
• The generated local method is JNI compliant with the standard format:
Copy codeThe Code is as follows: return-type Java_package-name_class-name_method-name (arguments ){
}
That is to say, all you need to do is implement this method.
• By default, the name of the local code sharing library is the name of the Android project.
With this tool, you can declare the local method in Java, run the tool, implement the local method, and then compile it.
From <Download here> This small tool. After decompression, there are three files: the Java source code, the Jar package (genjni. jar), and the Shell script (genjni. sh ). The reason for putting the source code on is that if you are interested, you can make improvements.
Send me a copy. After downloading the file, you 'd better modify the Shell script and change the path of the Jar file to the specific storage path. Otherwise, an error will be reported that the Jar file cannot be found. Finally, place genjni. sh IN ~ /Bin. Run genjni. sh in the root directory of the Android project.
The following example shows how to use this tool:
Create a project named HelloJni, create a HelloJniActivity, declare a local method getStringFromJni () in it, and use a TextView to display the information returned by getStringFromJni. Another local method, getStatusFromJni (int), is used for display and is not used. This is the Java code:Copy codeThe Code is as follows: package com. hilton. hellojni;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class HelloJniActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TextView text = (TextView) findViewById (R. id. text );
Text. setText (getStringFromJni ());
}
Private native String getStringFromJni ();
Private native int getStatusFromJni (int type );
}
After writing the Java code, enter the project root directory from the terminalCopy codeThe Code is as follows: $ cd HelloJni
$ Ls
AndroidManifest. xml assets bin default. properties gen proguard. cfg res src
$ Genjni. sh
Appplication HelloJni
Package name: com. hilton. hellojni
Class name: HelloJniActivity
$ Ls
AndroidManifest. xml assets bin default. properties gen jni proguard. cfg res src
$ Ls jni
Android. mk HelloJniActivity. c
Open Android. mk and HelloJniActivity. c.Copy codeThe Code is as follows: LOCAL_PATH :=$ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_MODULE: = HelloJni
LOCAL_SRC_FILES: = HelloJniActivity. c
Include $ (BUILD_SHARED_LIBRARY)
Copy codeThe Code is as follows: # include <jni. h>
Jstring Java_com_hilton_hellojni_HelloJniActivity_getStringFromJni (JNIEnv * env, jobject thiz ){
}
Jint Java_com_hilton_hellojni_HelloJniActivity_getStatusFromJni (JNIEnv * env, jobject thiz, jint type ){
}
Check that HelloJniActivity. java has also been updated. The statement for loading the shared library is added:Copy codeThe Code is as follows: package com. hilton. hellojni;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class HelloJniActivity extends Activity {
Static {
System. loadLibrary ("HelloJni ");
}
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TextView text = (TextView) findViewById (R. id. text );
Text. setText (getStringFromJni ());
}
Private native String getStringFromJni ();
Private native int getStatusFromJni (int type );
}
The rest is to implement the local method.
Of course, this tool still has many problems. You are welcome to give feedback or suggestions for improvement.
In addition, this tool is written in Java, and a better choice should be written in scripts, such as Perl or Python. In addition, if you can integrate this tool into ADT or create an integrated tool ANDT that is fully used for NDK development, You can automatically generate local files like R. java. For example, when an ANDT tool is integrated into Eclipse, It can automatically generate local files and compile files after local method declarations exist in Java. This will be a wonderful thing and will be of great help to the Development of NDK. I think Google should develop an Eclipse plug-in specifically for NDK development, or add support for NDK in ADT, because there are more and more interfaces open to NDK, and more developers will use NDK, more and more applications will be developed based on NDK (in versions 2.3 and later, only NDK can be used to develop an Apk, that is, developing applications using pure C/C ++ ). We hope this day will arrive early.