[Google Android] understands NDK (1)--compiling Android. so files

Source: Internet
Author: User

Original address: http://www.cnblogs.com/tamer/archive/2011/01/18/1938307.html

To be blunt, C code is made into a dynamic library, called by Java Code (JNI). The executable program that can run directly on Android is not written in C.

NDK development requires a cross-compilation environment that requires the installation of the Cygwin, NDK.

These preparations do not say a lot of online, of course, can also ask me.

Tell me about the. so file generation

--------------------------------------------------------------------------------------------------------------- -----------------------------------

compiling Android. so file

1. Defining the Native Interface

Create an Android project "Ndk_plus" and add a JNI class.

Figure 1:android's SDK Project

Jni.java Code:

Package kun.ndk.plus;
public class JNI {
public native int plus (int a, int b);
}

2. Generate the corresponding. h file according to the Jni.java file

CMD to the bin directory of the Ndk_plus project, and then execute javah–classpath. –jni Kun.ndk.plus.JNI

This will generate the Kun_ndk_plus_jni.h header file. The contents are as follows:

/* Don't EDIT this file-it are machine generated */
#include <jni.h>
/* Header for class KUN_NDK_PLUS_JNI */

#ifndef _included_kun_ndk_plus_jni
#define _included_kun_ndk_plus_jni
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:kun_ndk_plus_jni
* Method:plus
* Signature: (II) I
*/
Jniexport Jint Jnicall Java_kun_ndk_plus_jni_plus
(JNIEnv *, Jobject, Jint, Jint);

#ifdef __cplusplus
}
#endif
#endif

If prompted without the Javah command, you will need to locate the directory where Javah.exe is located (C:\Program files\java\jdk1.5.0\bin) and add this directory to the environment variable path.

3. Implement the corresponding. c file according to the. h file

Create a kun_ndk_plus_jni.c file in eclipse (all other editing tools are available)

//-------------------------------------------------------------------------------------------------------------

#include <jni.h>

#include "kun_ndk_plus_jni.h"

Jniexport Jint Jnicall Java_kun_ndk_plus_jni_plus

(JNIEnv *e, Jobject J, Jint A, jint b)

{

return a + B;

}

//-------------------------------------------------------------------------------------------------------------

Note the red part. Because the function declaration in the generated. h does not have a formal parameter, you need to add the parameter variable yourself when you implement it.

Also note that you want to add a carriage return (the characteristics of the GCC compiler) after the blue curly braces.

4. Making the. mk File

Create a TXT file under this folder by creating a new plus folder under the Apps directory in the NDK directory.

Enter the content:

App_project_path: = $ (call My-dir)

App_modules: = Plus

Rename to Application.mk

In the Ndk/sources/samples directory, create a new plus folder under this folder, creating a TXT file, and enter the following:

Local_path: = $ (call My-dir)

Include $ (clear_vars)

Local_module: = Plus

Local_src_files: = Kun_ndk_plus_jni.c

Include $ (build_shared_library)

Rename the name Android.mk. Copy the. c and. h files to this folder.

Keywords specific meaning see Doc/overview. Txt

5. Compiling

Launch Cygwin, CD to NDK directory

Make App=plus

After success, there will be a libplus.so file under Apps/plus/libs/armeabi.

6. The following is the use of the. so file in the Java code to add to Android engineering.

[Google Android] understands NDK (1)--compiling Android. So file

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.