Android NDK Development

Source: Internet
Author: User

Android NDK Development

Many of the files on the Internet use the javah command to generate header files for JNI writing. However, after the ADT is integrated with NDK, you can just click it. Lazy methods are rarely introduced on the Internet, this section focuses on the development of lazy JNI.

For how to configure NDK for ADT, google or Android developer.

1. Create an Android project named JNI_Learn. The code snippet generated with one click 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 highlighted part is added by myself.

If loadLibrary is used, I will mark the name later. Here, the name of the so library generated by the native layer is used. The lib prefix and. so suffix must be removed.

To declare the native method, add a native flag to the native layer before the access permission and other declarations.

The plus method is similar to normal use.

2. Then, the project is operated by a lazy user. Right-click the project and choose Android Tools> Add Native Support...


Check that there are two files in the project.


The first one is JNI_Learn.cpp, which will be detailed later. The second is Android. mk. During Android compilation, you will find whether Android. mk exists in the directory and compile it based on the file. The specific compilation rules are compiled. After opening the file, the content is as follows:

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_LDLIBS := -llogLOCAL_MODULE    := JNI_LearnLOCAL_SRC_FILES := JNI_Learn.cppinclude $(BUILD_SHARED_LIBRARY)
All except the red part are automatically generated. The red part is added to use the native log method.
LOCAL_PATH indicates the path of the source file to be compiled, LOCAL_LDLIBS indicates the connector option to be appended when the module is compiled, LOCAL_MODULE indicates the name of the module to be compiled, LOCAL_SRC_FILES indicates the source file to be compiled, include $ (BUILD_SHARED_LIBRARY) indicates that the file is eventually compiled into a shared library file.


3. Add the code for implementing the plus method in the native layer as follows:

#include 
 
  #include #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_1learn_JNI_plus");return x + y;}
 

If you use Android log to print logs to logcat, you can view related knowledge on your own. This does not matter much. This is why LOCAL_LDLIBS needs to be added to Android. mk.

Extern "C" must be added, because the exported function prototype after C ++ and C compilation is different, java can only call C type, therefore, you need to convert C ++ to C type and add the extern "C" declaration.

JNIEXPORT declares that it is an export function.

The method name must start with Java and be separated by underscores (_) between the package name and Class Name and method name. If your package name is accidentally underlined, so how can we handle the situation where the java package name in JNI contains underscores? Add "1" to the front ".


There are several errors in such a simple program:

The first one is that JNI Load Library fails, because in the apk, we can see whether so is compressed in (change the apk suffix to rar, decompress the rar file and you will see this so in the lib folder.) I can see that the file is compressed and the loading still fails. The reason is that the loadLibrary parameter must not contain the prefix lib without the suffix. so.

The undefined reference to was revealed because I did not declare extern "C" and java could not find the C ++ declaration method.

In addition, the package name exactly contains underscores (_ 1), such as JNI_Learn.

In addition, to use LOG, you must add LOCAL_LDLIBS to Android. mk.



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.