SoFunction
Updated on 2025-03-01

Detailed explanation of local code generator based on Android

There is something that is annoying when developing with AndroidNDK, that is, creating local code folders, generating local code files and creating local code compiled files. Especially when implementing local methods, it is also quite annoying, because the name of the local method is too long. Its naming specification is: Java_package-name_class-name_method-name(arguments). If you are not careful, there will be spelling errors, which will lead to long-term debugging. Because I didn't endure such torture and in order not to repeat the same thing (DRY-Don't Repeat Yourself), I wrote a Java program to do it.
This widget can check Java files one by one, and create files containing local methods, that is, local code files, generate compiled files, update Java files, and add them.
The specific principles are as follows:
•Each Java file containing local code generates a local file containing all local methods in the file
• The generated local method is a standard JNI, in its specific form:
Copy the codeThe code is as follows:

return-type Java_package-name_class-name_method-name(arguments){
       }

That is, all you need to do is implement this method.
•The name of the default local code sharing library is the name of the Android project
With this widget, you can just declare the local method in Java, run the tool, then implement the local method, and then compile it.
Can be fromDownload here>This gadget. After decompression, there are three files that are Java source code, one is Jar package (), and the other is Shell script (). The reason for putting the source code is that if you are interested, you can make improvements, but please
Send me one. After downloading, it is best to modify the shell script and change the path of the Jar file to the specific storage path, otherwise an error that the Jar file cannot be found will be reported. Finally put it under ~/bin for convenience when using it. When using it, just run it in the root directory of the Android project.
The following is an example to demonstrate how to use this widget:
Create an item called HelloJni, and create a HelloJniActivity, declare a local method getStringFromJni(); 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 the codeThe code is as follows:

package ;
import ;
import ;
import ;
public class HelloJniActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        setContentView();
        TextView text = (TextView) findViewById();
        (getStringFromJni());
    }

    private native String getStringFromJni();
    private native int getStatusFromJni(int type);
}

After writing the Java code, enter the project root directory from the terminal
Copy the codeThe code is as follows:

$cd HelloJni
$ls
  assets  bin    gen    res  src
$
appplication HelloJni
package name:
class name: HelloJniActivity
$ls
  assets  bin    gen  jni    res  src
$ls jni
 

Open View and
Copy the codeThe code is as follows:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := HelloJni
LOCAL_SRC_FILES :=
include $(BUILD_SHARED_LIBRARY)

Copy the codeThe code is as follows:

#include <>
jstring Java_com_hilton_hellojni_HelloJniActivity_getStringFromJni(JNIEnv* env, jobject thiz) {
}
jint Java_com_hilton_hellojni_HelloJniActivity_getStatusFromJni(JNIEnv* env, jobject thiz, jint type) {
}

After viewing again, it was also updated, and there are more statements to load the shared library:
Copy the codeThe code is as follows:

package ;
import ;
import ;
import ;
public class HelloJniActivity extends Activity {
    static {
        ("HelloJni");
    }
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        setContentView();
        TextView text = (TextView) findViewById();
        (getStringFromJni());
    }

    private native String getStringFromJni();
    private native int getStatusFromJni(int type);
}

The rest of the job is to implement the local method.
Of course, there are still many problems with this tool. Feedback or suggestions for improvement are welcome.
In addition, this tool is written in Java, and a better choice should be written in scripts, such as Perl or Python. Also, if you can integrate this tool into ADT, or create an integration tool ANDT that is completely used for NDK development, it can automatically generate local files like the ones that generate. For example, to make an ANDT tool, integrated into Eclipse, it can automatically generate local files and compile files after a local method declaration is declared in Java. What a wonderful thing this will be, and it will be of great help to the development of NDK. I think Google should make an Eclipse plug-in specially used for NDK development, or add support for NDK to ADT, because there are more and more open interfaces, more and more developers using NDK, and more and more applications developed based on NDK (in versions 2.3 and later, you can only use NDK to develop an Apk, that is, use pure C/C++ to develop applications). Hope this day will come soon.