When using Android NDK, there are three ways to produce android builds: ndk-build, cmake or standalone toolchains.
The following (very short) description will show you, how it works with (the command line tool) ndk-build. I'm using ndk-build, because i'm not delivering a software (app) for different environments, i'm only interested in compiling a source code (c or c++) to android. No more, no less.
But before we can start, we need the Android NDK. It can be downloaded from https://developer.android.com/ndk/downloads. Choose the right version for you. Unzip the zip archive to a directory of your choice and put the root directory of your android-ndk directory to your search path (the command line tool ndk-build should start from the command line in every directory). In Windows you enter the path to the system or user path, in Linux you may add the following lines at the end of the file „.bashrc“ in your home directory (assuming you unzip the content of the zip archiv to usr/lib/android-ndk), so the path is active, when you start the terminal.
Code: Select all
export PATH=$PATH:/usr/lib/android-ndk
Let us begin with an current project, which is easy to compile: Demolito.
1. Download the sources from https://github.com/lucasart/Demolito. If you donwload the sources as zip archive, unzip the files to a directory of your choice (e. g. Demolito).
2. When you go in to the root directory of Demolito, you will see a src directory. Create a directory named jni in the root directory of Demolito and put the following file in to that directory.
Android.mk:
Code: Select all
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Demolito
LOCAL_SRC_FILES := $(wildcard ../src/*.c)
LOCAL_CFLAGS += -DVERSION=\"2020-10-10\"
include $(BUILD_EXECUTABLE)
For us, line 3, 4 and 5 are necessary.
Line 3: Here you define a name for your builds (executables). In our case we choose the name Demolito.
Line 4: Here are all source files listed, which have to be compiled. In our case we want to compile all *.c files in the src directory.
Line 5: Here you can define additional compiler flags for the compiling process. In your case we only need the parameter „VERSION“, which is used in the source code of Demolito for showing the correct version number. No more parameter needed. Android NDK, i mean ndk-build, will do the rest (in most cases, ndk-build choose the right parameter for you.)
3. Start the command line and change to the jni directory and call ndk-built. The compiling process is starting and when there is no error, the executables will be found in the libs directory.
4. Ready.
OK, this is a very basic example, which works with the default settings of Android NDK.
Normally, we have to add a few more compiler settings. Let us describe some of them with the Demolito example.
When we take a look at the makefile in the src directory, we can see, that the compiler language of the project is gnu11. Normally ndk-build find this out automatically for you, but in some cases, this won‘t work. So we add this information to the Android.mk file, so the compiler knows, in what compiler language it should compile the project.
Android.mk:
Code: Select all
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Demolito
LOCAL_SRC_FILES := $(wildcard ../src/*.c)
LOCAL_CFLAGS += -std=gnu11 -DVERSION=\"2020-10-10\"
include $(BUILD_EXECUTABLE)
Android.mk:
Code: Select all
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Demolito
LOCAL_SRC_FILES := $(wildcard ../src/*.c)
LOCAL_CFLAGS += -std=gnu11 -O3 -flto -DVERSION=\"2020-10-10\"
LOCAL_LDFLAGS += -flto
include $(BUILD_EXECUTABLE)
For completeness, i‘m using the following Android.mk file for compiling Demolito.
Code: Select all
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Demolito
LOCAL_SRC_FILES := $(wildcard ../src/*.c)
LOCAL_CFLAGS += -std=gnu11 -DNDEBUG -O3 -flto -Wfatal-errors -Wall -Wextra -Wshadow -DVERSION=\"2020-10-10\"
LOCAL_LDFLAGS += -flto
include $(BUILD_EXECUTABLE)
With ndk-build you have not only the file Android.mk for setting compiling options, there is also a file named Application.mk which can be used to tell the compiler additional instructions.
For example, if you want, that your builds are compatible with the Android API level 16, you have to put the following line in to Application.mk (which should be also in the jni directory).
Application.mk:
Code: Select all
APP_PLATFORM := android-16
This example shows, all what you need for compiling to Android is, an installed Android NDK (root directory should be in the search path), two files, named Android.mk and Application.mk, with the correct settings, in the jni directory. The only thing you have to do is, calling ndk-build from the command line within the jni directory.
Keep in mind, this is only a very short and by far not complete description, but it shows the idea behind of cross compiling with ndk-build from Android NDK.