Not long ago, we introducedBuild an Android Development Environment on MyEclipse 8.6In this article, we will introduce how to install Android NDK and start using it. After this tutorial is complete, you will create your own project and simply call the native C language code from the Java code.
BKJIA recommended topics: Android development and application details
Tutorial details
Technology: Android SDK, NDK, C Language
Difficulty: Advanced
Expected completion time: 60-90 minutes
Prerequisites
Before we start, we need to take some time to understand the difficulty of this tutorial. It is marked as "advanced ". The reason we mark it as "advanced" is that our authors want to ensure that you meet the following requirements:
You have experience in Java and C.
You can adapt to command line operations.
You know how to understand your Cygwin, awk, and other tool versions.
You can adapt to Android Development.
You have an effective Android development environment. When writing this article, I used Android 2.2)
You can use Eclipse or easily apply the Eclipse instructions to your IDE.
Even if you do not meet these conditions, we certainly welcome you to read this tutorial. However, you may encounter difficulties in some steps. If you meet these conditions, these difficulties will be easily lifted. That is to say, even if you think you are a mobile development veteran, using NDK is still easy to encounter difficulties and troubles. Please note that you may need to troubleshoot the fault on your own so that everything can run properly in your development system.
This tutorial provides a complete example of open source code download for the project.
Description of when to use NDK
Well, if you are reading this tutorial, you may already be considering using NDK in your Android project. However, we want to spend some time discussing why NDK is so important, when it should be used, and -- equally important, when it should not be used.
In general, you only need to use NDK when your application is really a processor killer. That is to say, the algorithm you designed should use all the processor resources of DalvikVM, and the Native operation is more advantageous. Also, don't forget that in Android 2.2, the JIT compiler will improve the efficiency of similar code.
Another reason for using NDK is convenient transplantation. If you have a large amount of C-language code in your existing applications, using NDK not only accelerates the development process of your project, you can also synchronize modifications in your Android and non-Android projects. This is especially true for OpenGL ES applications written for other platforms.
Don't think that using native code can improve the efficiency of your application. Conversion between Java and native C languages increases resource overhead. Therefore, this is really necessary only when you have tasks that consume CPU resources in a centralized manner.
Step 2: Download the tool
Okay. Let's get started. You need to download the NDK. Let's start the download first, because during the download process, you can check to make sure that the versions of the other tools you need are correct.
Download the NDK suitable for your operating system from the Android website.
Check your tool Version against the following:
If Cygwin 1.7 or later is in Windows
Upgrade awk to the latest version. We use 20070501)
We use GNU Make 3.81 or later)
If any of the versions is too old, upgrade them before continuing.
Step 2: Install NDK
Since NDK has been downloaded, right ?), You need to decompress it. Decompress the package and put it in a suitable directory. We put it in the same directory as the Android SDK. Remember where you put it.
Now, you may want to add the NDK tool in path settings. If you are on Mac or Linux, you can use your native path settings. If you are running Cygwin in Windows, you need to set the Cygwin path.
Step 2: Create a project
Create a regular Android project. To avoid future problems, the path of your project must not contain spaces. Our project has a package named "com. mamlambo. sample. ndk1" with a default Activity called "AndroidNDK1SampleActivity"-you will see them later.
Create a directory named "jni" on the top of the project-this is where you place native code. If you are familiar with JNI, you will know that Android NDK is based on the JNI concept to a large extent-it is essentially a JNI with only a limited header file compiled in C language.
Step 2: add some C-language code
Now, create a file named native. c In the jni folder. Write the following C language code to this file at the beginning, and we will add another function later:
- #include
-
- #include
-
- #include
-
- #define DEBUG_TAG "NDK_AndroidNDK1SampleActivity"
-
- void Java_com_mamlambo_sample_ndk1_AndroidNDK1SampleActivity_helloLog(JNIEnv * env, jobject this, jstring logThis)
-
- {
-
- jboolean isCopy;
-
- const char * szLogThis = (*env)->GetStringUTFChars(env, logThis, &isCopy);
-
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szLogThis);
-
- (*env)->ReleaseStringUTFChars(env, logThis, szLogThis);
-
- }
-
This function is actually very simple. It obtains the string parameter of a Java object, converts it to a C-string, and writes it to LogCat.
However, the function name is very important. It starts with a specific word "Java", followed by the package name, class name, and method name, which is the same as that defined in Java. Each part is separated by an underscore instead of a dot.
The first two parameters of the function are also important. The first parameter is the JNI environment, which is frequently called with the helper function. The second parameter is the Java object to which the function belongs.
Step 2: Call native code from Java
Now that you have written the native code, let's look back at the Java side. In the default Activity, create a button according to your preferences and add a button processor. Call helloLog from the button processor:
- helloLog("This will log to LogCat via the native call.");
Then you must add a function declaration in Java. Add the following declaration to your Activity class:
- private native void helloLog(String logThis);
It tells the compilation and linking system that this method will be implemented in the native code.
Finally, you need to load the library to which the native code is finally compiled. You can add the following static initialization program to the Activity class to load the library name based on the name, which will be used in the next step ):
-
-
- static {
-
- System.loadLibrary("ndk1");
-
- }
-
Step 2: add the Make file of native code
In the jni folder, now you need to add the makefile to be used in compilation. This file must be named "Android. mk". If the file you named earlier is native. c and the library is ndk1, the content of Android. mk should be like this:
-
-
- LOCAL_PATH := $(call my-dir)
-
-
-
- include $(CLEAR_VARS)
-
-
-
- LOCAL_LDLIBS := -llog
-
-
-
- LOCAL_MODULE := ndk1
-
- LOCAL_SRC_FILES := native.c
-
-
-
- include $(BUILD_SHARED_LIBRARY)
-