Using the internet There are many Javah commands to generate a header file to complete the JNI write, but in fact ADT integrates the NDK. Click on the mouse to be able, online introduction is very small lazy method, here, we mainly talk about the lazy JNI development.
Configure the NDK for ADT. Please personal Google or view Android developer, not much to do description here.
1. Create a new androidproject, my side named Jni_learn, a key generated after the code fragment such as the following:
public class JNI extends Actionbaractivity {static{system.loadlibrary ("Jni_learn");} public native int plus (int x, int y); @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_JNI); if (savedinstancestate = = null) { Getsupportfragmentmanager (). BeginTransaction () . Add (R.id.container, new Placeholderfragment ()) . commit (); } LOG.D ("Jnitest", "3+5=" + Plus (3, 5)); }
Highlighting is part of my own participation. Explain briefly.
LoadLibrary words, the inside name later I will mark again, this side is mainly the native layer generated so library name, need to remove the Lib prefix and. so suffix.
Declaring the native method after access to the other declarations, add a native to mark this method as native layer.
Using the Plus method is no different than normal use.
2. Then the lazy person is working, right-clicking the mouse on project. Choose android Tools->add Native support ...
Then look at project with two files
The first one is Jni_learn.cpp, which is detailed later. The second one is android.mk. In Android compilation. Will look for android.mk under the folder and compile it according to this file. A detailed compilation rule is written, after opening the file. The contents are as follows:
Local_path: = $ (call My-dir) include $ (clear_vars) Local_ldlibs: =-lloglocal_module : = jni_learnlocal_src_files: = Jni_learn.cppinclude $ (build_shared_library)
In addition to the red part, the others are actively generated for themselves, plus red Part I joined in order to use the native log method
Where Local_path represents the path to the compiled source file, Local_ldlibs represents the connector option that needs to be appended when compiling the module, Local_module indicates the name of the module that was finally compiled. Local_src_files represents the source file that needs to be compiled, and the include $ (build_shared_library) indicates that it is finally compiled into a shared library file.
3. The following detailed implementation code to add the native layer to the plus method
#include <jni.h> #include <android/log.h> #define LOG_TAG "Jnitest"//log func//int __android_log_print (int Prio, const char *tag, const char *FMT, ...) #define LOGD (...) __android_log_print (Android_log_debug, Log_tag, __va_args__) extern "C" Jniexport int java_com_ Example_jni_1</span>learn_jni_plus "); return x + y;}
Use Android log to print the log to Logcat. Want to understand the ability to consult the relevant knowledge, this side is not very big relationship. This is also why you need to add local_ldlibs reasons in Android.mk.
extern "C" is required, because the C + + and C compiled export function prototype is different, Java can only call C type, so you need to convert C + + to C type. Add the extern "C" declaration.
Jniexport declares that he is an export function.
The method name must begin with Java, and the package name and the class name and the method name are separated by an underscore. Suppose it happens that you have an underscore in your package name. So how do you deal with the Java package name in JNI with an underscore? Add a "1" to the front.
This simple program encountered several errors:
The first is that the JNI Load library failed, because the APK can see that so is not compressed into the (change the apk suffix to rar, decompression RAR can see there is a Lib directory has this so), I saw the compression in, the result or load failed.
The reason is that the LoadLibrary will need not prefix lib without suffix. So talented enough.
Burst undefined reference to. The reason is because I did not declare extern "C". Java could not find a way to declare C + +.
And there's just an underscore in the package name. For example, Jni_learn needs to be changed to _1 ability to evade.
There is also the use of log, you must add Local_ldlibs in Android.mk.
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Android NDK goes into development