[Android NDK ] Create Android mk File
Before the NDK build tools will compile your code they need a 'Makefile' file. That's the purpose of the file 'Android.mk'. Because our project only uses a single source file for the JNI library, our 'Android.mk' file will be simple to write. Information on writing the 'Android.mk' file can be found in the 'docs' folder inside the 'android-ndk-r4b' folder. The most relevant document is the 'ANDROID-MK.TXT', and this document does a thorough job of explaining the parts of the file that are required.
An example 'Android.mk' file is shown below. This file is taken from the Awesomeguy project.
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := awesomeguy LOCAL_CFLAGS := -Werror LOCAL_SRC_FILES := awesomeguy.c LOCAL_LDLIBS := -llog include $(BUILD_SHARED_LIBRARY)
For the purposes of this tutorial we'll leave most of the file as it is.
- LOCAL_PATH - this line should be left as it is since your source file ('example.c') is in the same directory as the 'Android.mk' file.
- include $(CLEAR_VARS) - this line should be left as it is. It is required.
- LOCAL_MODULE - this line should be changed to match your module name. For this tutorial we'll change it to 'example'. This name should not have any spaces in it as it will be made into the actual library's name ('libexample.so' for us).
- LOCAL_CFLAGS - This line can be left as it is. It is for compiler flags.
- LOCAL_SRC_FILES - this line should be changed to 'example.c' since that's our source file.
- LOCAL_LDLIBS - leave this the same.
- include $(BUILD_SHARED_LIBRARY) - leave this the same.
So our new 'Android.mk' file should look like this.
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := example LOCAL_CFLAGS := -Werror LOCAL_SRC_FILES := example.c LOCAL_LDLIBS := -llog include $(BUILD_SHARED_LIBRARY)
The NDK build tools will compile the code, and then if there are no errors it will name the object code 'libexample.so' and place it in the Android project in a new folder that it creates especially for this purpose. The name of the folder is 'libs/armeabi/'. The build tools also manage the creation of some other files that are necessary.
The name, 'libexample.so' comes from the 'LOCAL_MODULE' line in the 'Android.mk' file, and it is the library name, but when referring to the module in the java code you refer to 'example' not 'libexample.so'.
No comments: