Android Development Environment Configuration

Source: Internet
Author: User

Detailed explanation of Android. mk specifications

 

This article from: http://blog.ednchina.com/laizibin315/1895365/message.aspx

The android. mk file is a required file for compiling C code using ndk. The Android. mk file describes which c files will be compiled and how to compile them. Master android. the preparation of the MK file mainly involves some of the keywords to be used in it. Let's look at a simple example. The example is in the D of the ndk package: /android-ndk-r3/apps/Hello-JNI/project/JNI directory:

 

The content in Android. mk is as follows:

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

Local_path: = $ (call my-DIR)

 

Include $ (clear_vars)

 

Local_module: = hello-JNI

Local_src_files: = hello-jni.c

 

Include $ (build_shared_library)

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

 

Local_path is the root directory of all c files to be compiled. The value $ (call my-DIR) indicates that the root directory is the directory where Android. mk is located.

 

Include $ (clear_vars) indicates that the global variables used in the compiling environment are cleared when the ndk compilation tool is used, such as local_module and local_src_files, because android may be called multiple times during an ndk compilation process. the global variables used in the MK file may change. I have read the following complex examples for this question and may understand it.

 

Local_module is part of the name when the library is finally generated, and the prefix lib and suffix. So is the name libhello-jni.so of the generated shared library.

 

Local_src_files indicates the name of the C file to be compiled

 

Include $ (build_shared_library) indicates that some shared libraries will be generated during ndk compilation.

 

The content in the corresponding JNI folder of Android. mk is as follows:

 

Code in its hello-jni.c is as follows:

# Include <string. h>

# Include <JNI. h>

 

/* This is a trivial JNI example where we use a native Method

* To return a new VM string. See the corresponding JAVA Source

* File located:

*

* Apps/samples/Hello-JNI/project/src/COM/example/hellojni. Java

*/

Jstring

Java_com_example_hellojni_hellojni_stringfromjni (jnienv * ENV,

Jobject thiz)

{

Return (* env)-> newstringutf (ENV, "hello from JNI! ");

}

 

For more information about how to use ndk for compilation, see my blog "how to build the underlying development environment of ndk"

For details about the code in hello-jni.c, you can refer to the detailed explanation of jni.pdf in the attachment.

 

Let's take a look at a complex example. In this example, a static library: libsum-jni.a will first be generated during the ndk compilation process, this static library will be called in further compilation to generate shared library together: libhello-jni.so

 

 

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

Local_path: = $ (call my-DIR)

 

Include $ (clear_vars)

 

Local_module: = sum-JNI

Local_src_files: = sum-jni.c

 

Include $ (build_static_library)

 

Include $ (clear_vars)

 

Local_module: = hello-JNI

Local_src_files: = hello-jni.c

 

Local_static_libraries: = sum-JNI

 

Include $ (build_shared_library)

Bytes -------------------------------------------------------------------------------------

 

In this android. mk file, we can see that include $ (clear_vars) is used twice, so that the values of global variables used before and after ndk compilation do not affect each other.

Include $ (build_static_library) indicates to compile the sum-jni.c file into a static library

Libsum-jni.a

 

Local_static_libraries: = sum-JNI indicates that the static library libsum-jni.a generated during the first step of compilation is called during the second step of compilation, and compiled into a shared library hello-jni.c with the libhello-jni.so File

 

The content in the corresponding JNI folder of Android. mk is as follows:

 

 

Code in hello-jni.c:

 

# Include "sum-jni.h"

# Include <string. h>

# Include <JNI. h>

 

/* This is a trivial JNI example where we use a native Method

* To return a new VM string. See the corresponding JAVA Source

* File located:

*

* Apps/samples/Hello-JNI/project/src/COM/example/hellojni. Java

*/

Jstring

Java_com_example_hellojni_hellojni_stringfromjni (jnienv * ENV,

Jobject this)

{

Return (* env)-> newstringutf (ENV, "My name is Zibin Lai .");

}

 

Jint

Java_com_example_hellojni_hellojni_jsum (jnienv * ENV, jobject this, jint A, jint B)

{

Return sum (A, B );

}

 

Sum-jni.c code:

Int sum (int A, int B)

{

Return A + B;

}

 

 

The last example is the Android. mk settings compiled by multiple C files:

 

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

Local_path: = $ (call my-DIR)

 

Include $ (clear_vars)

 

Local_module: = sum-JNI

Local_src_files: = sum-jni.c

 

Include $ (build_static_library)

 

Include $ (clear_vars)

 

Local_module: = hello-JNI

Local_src_files: = hello-jni.c/

Compare-jni.c/

Sort/sort. c/

 

Local_static_libraries: = sum-JNI

 

Include $ (build_shared_library)

Bytes -----------------------------------------------------------------------------------

 

We can see that the parameter value assignment of the second local_src_files has more file names than the above values, and the path can be attached to the value assignment process.

The content in the corresponding JNI folder of Android. mk is as follows:

Related Article

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.