Introduction to Android NDK development

Source: Internet
Author: User
Tags rar

A lot of online is to use the Javah command to generate a header file to complete the JNI writing, but in fact, ADT Integration NDK, click the mouse can be, lazy method on-line introduction very little, here mainly talk lazy people jni development.

For ADT Configuration NDK, please personal Google or view Android developer, this side does not do more introduction.

1. Create a new Android project, my side named Jni_learn, a key generated after the code snippet is as follows:

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));            }
The highlight is the part I add myself to explain briefly.

LoadLibrary words, the name of the inside 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.

Declare the native method to add a native tag to the native layer before the access rights are declared before the other declarations.

Using the Plus method is no different than normal use.

2. After the lazy operation, on the project right click on the mouse, select Android Tools->add Native support ...


Then look at the project with two files.


The first one is Jni_learn.cpp, which is detailed later. The second is Android.mk, in Android compilation, will look for the directory under the existence of ANDROID.MK, and then compiled according to this file. The specific compilation rules are written, and 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 automatically generated, plus the red part is the log method I added in order to use native
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 represents the name of the module that is ultimately compiled, Local_src_files represents the source file that needs to be compiled, The include $ (build_shared_library) means that it is eventually compiled into a shared library file.


3. The following specific 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;}

Using the Android log to print the log to the Logcat, want to understand the relevant knowledge can be consulted, there is not much relationship, this is also in the android.mk why need to increase the local_ldlibs reason.

extern "C" is a must, because 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 extern "C" declaration.

Jniexport declares that he is an export function.

The method name must start with Java, and then the package name and the class name and the method name are underlined, if it happens that your package name is underlined, then how to handle the Java package name in JNI with an underscore case? 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, unzip the RAR can see there is a Lib folder has this so), I see the compression in the side, the result or load failed. The reason is that the LoadLibrary parameter must not be prefixed with the Lib suffix. So.

Burst undefined reference to, because I did not declare extern "C", Java can not find the way to declare C + +.

There is the package name just appear underline, such as Jni_learn, need to change to _1 to avoid.

There is also the use of log, you have to add Local_ldlibs in Android.mk.



Introduction to Android NDK development

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.