Tool: Android Local Code Generator

Source: Internet
Author: User

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. Its naming rules are: Java _Package-name_Class-name_Method-name(Arguments), a spelling error occurs when you are not careful, resulting in a long time of debugging. Because we don't have to deal with this kind of torture, we also want to avoid repeating the same thing (Dry-Don't
Repeat yourself), so I wrote a Java program to do this.
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 conforms to the standard JNI. The specific form is:

Return-typeJava _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.
You can download this tool from here. 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:

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 terminal

$cd HelloJni$lsAndroidManifest.xml  assets  bin  default.properties  gen  proguard.cfg  res  src$genjni.shappplication HelloJnipackage name: com.hilton.hellojniclass name: HelloJniActivity$lsAndroidManifest.xml  assets  bin  default.properties  gen  jni  proguard.cfg  res  src$ls jniAndroid.mk  HelloJniActivity.c

Open Android. mk and hellojniactivity. C.

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := HelloJniLOCAL_SRC_FILES := HelloJniActivity.cinclude $(BUILD_SHARED_LIBRARY)

#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:

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.

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.