Based on the Android local code generator detailed _android

Source: Internet
Author: User
Tags generator
One thing that is annoying when using ANDROIDNDK development is to create a local code folder, generate a local code file, and create a compiled file that creates local code. This is especially annoying when implementing local methods, because the name of the local method is too long. Its naming code is: Java_package-name_class-name_method-name (arguments), a little inattentive will have spelling mistakes, and lead to a long time debugging. In order not to endure such torture, but also to not repeat the same thing (Dry-don ' t Repeat yourself), so wrote a Java program to do this thing.
This gadget can check the Java file individually and create a local method file that contains a local code file, generate android.mk compile files and update Java files, and add System.loadlibrary.
The specific principle is this:
• Each Java file that contains local code generates a local file containing all the local methods in the file
• The generated local method is a standard JNI, in the form of:
Copy Code code as follows:

Return-type java_package-name_class-name_method-name (arguments) {
}

In other words, all you need to do is implement this method.
• Default local code shared library name is the name of the Android project
With this gadget, you can simply declare the local method in Java, run the tool, then implement the local method, and then compile it.
can be from < Download Here> This gadget. After decompression, there are three files, one is Java source, one is Jar package (Genjni.jar), and the other is Shell script (genjni.sh). The reason to put the source code is that if interested friends can improve, but after the improvement please
Send me a copy. After downloading, it is best to modify the shell script, the path of the jar file to the specific storage path, or you will not find the jar file error. Finally put the genjni.sh under the ~/bin, in order to use it conveniently. Use it to run genjni.sh under the root directory of the Android project.
here's how to use this gadget in one instance:
Create a project called Hellojni and create a hellojniactivity in which to declare a local method Getstringfromjni (), and a textview to display the information returned by Getstringfromjni (). Another local method, Getstatusfromjni (int), is for display purposes and is not used. This is Java code:
Copy Code code as follows:

Package com.hilton.hellojni;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.TextView;
public class Hellojniactivity extends activity {
/** called the activity is a. */
@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 root of the project from the terminal
Copy Code code 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 View Android.mk and HELLOJNIACTIVITY.C
Copy Code code as follows:

Local_path: = $ (call My-dir)
Include $ (clear_vars)
Local_module: = Hellojni
Local_src_files: = hellojniactivity.c
Include $ (build_shared_library)

Copy Code code 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) {
}

And then look at the Hellojniactivity.java is also updated, there are many statements to load the shared library:
Copy Code code 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 the activity is a. */
@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 of the work is to implement the local approach.
Of course, this tool still has a lot of problems, you are welcome to feedback, or give suggestions for improvement.
In addition, the tool is written in Java, and better choices should be written with scripts, such as Perl or Python. Also, if you can integrate this tool into ADT, or create an integration tool that is fully used for NDK development Andt, you can automatically generate local files like R.java. For example, a Andt tool, integrated into eclipse, automatically generates local files and compiled files automatically when there is a local method declaration in Java. What a wonderful thing it will be, it will be of great help to the development of NDK. I think Google should be doing an eclipse plug-in dedicated to NDK development, or adding support for NDK in ADT, because more and more NDK open interfaces and more NDK developers, The application based on NDK development will be more and more (2.3 and later version, can only use NDK to develop a apk, that is, the use of pure and C + + to develop applications). I hope this day comes early.
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.