«

»

Dec 04

Creating the android and application make files

Creating the android and application make files

 

 

Theoretical background:

 
 

Application.mk:

The Application.mk is a Make File that defines general variables about how to compile your applications.

For example:

Which ABIs the application will build for, which toolchain and which compiler to use, what is the min android version to build to and cpp flags for the compiler.

An Application can have more than one Application.mk.

 

Android.mk:

The Android.mk file set which c++ sources to build, which header files to link, it defines how to group the source files to shared libraries and static libraries, and defines these libraries relations. It is used to define specific variables and build operations that the Application.mk did not define.

An Application can have more than one Android.mk.

 

Now let us create our Application.mk and Android.mk file.

We will place them in the app/jni folder:

 

1. Create a custom Android.mk file and a custom Application.mk file in the app/jni folder of our project:

 

Application.mk:

 

# We will use the clang compiler
NDK_TOOLCHAIN_VERSION := clang3.4

# The ABIs we will build for
APP_ABI := x86 armeabi-v7a

# The Android default C++ runtime support library is libstdc++, however, this library provides a very minimal support and does not include
# for example, the c++ Exceptions and RTTI support.
# Since we will need these implementations, we set here that we will use a different c++ runtime support library - gnustl_shared
# We use the shared version of gnustl and not the gnustl_static version because if we have many modules it is recommended to used the shared
# Otherwise the c++ runtime implementation gnustl_static will be copied to each module and that could cause bugs.
# See https://developer.android.com/ndk/guides/cpp-support.html#runtimes for more details
APP_STL := gnustl_shared

# The platform on which we will build against - its the same as the minSdkVersion defined in the build.gradle file 
APP_PLATFORM := android-19

# Adding the CPP flags 
APP_CPPFLAGS := -std=c++11 -frtti -fexceptions

 

Android.mk:

 

# A fix for 'abspath' function on Windows platform
# See: http://gnu-make.2324884.n4.nabble.com/using-realpath-abspath-in-3-81-tp13798p13801.html
myabspath = $(join $(filter %:,$(subst :,: ,$1)),$(abspath $(filter-out %:,$(subst :,: ,$1))))

#-----------------------------------------------------------------------------------------------
# Common definitions
#-----------------------------------------------------------------------------------------------

LOCAL_PATH := $(call my-dir)

# Define a variable for the root of our repository
CROSS_PLATFORM_PROJECT_EXAMPLE_ROOT := $(call myabspath,$(LOCAL_PATH)/../../../..)

# Define other useful path variables
CROSS_PLATFORM_PROJECT_EXAMPLE_XPLAT_ROOT := $(CROSS_PLATFORM_PROJECT_EXAMPLE_ROOT)/XPlat
CROSS_PLATFORM_PROJECT_EXAMPLE_XPLAT_INC := $(CROSS_PLATFORM_PROJECT_EXAMPLE_XPLAT_ROOT)/Code/inc
CROSS_PLATFORM_PROJECT_EXAMPLE_XPLAT_SRC := $(CROSS_PLATFORM_PROJECT_EXAMPLE_XPLAT_ROOT)/Code/src

# Note: This path can not be too long, this is already the max length for the path, it won't compile if it is too long
CROSS_PLATFORM_PROJECT_EXAMPLE_SWIG_SRC := $(CROSS_PLATFORM_PROJECT_EXAMPLE_XPLAT_ROOT)/BuildSupport/Android/SwigOut

#-----------------------------------------------------------------------------------------------
# Our shared library
#-----------------------------------------------------------------------------------------------

include $(CLEAR_VARS)

# The name of our shared library module (without the lib preface that is automatically added)
LOCAL_MODULE    := CrossPlatformProjectExample

# Our PCH file
PCH_FILE := $(CROSS_PLATFORM_PROJECT_EXAMPLE_XPLAT_INC)/pch.h

# Flag to indicate that we use a PCH file
LOCAL_CPPFLAGS += -include $(PCH_FILE)

# The path to all the header files we use in the project
LOCAL_C_INCLUDES := $(CROSS_PLATFORM_PROJECT_EXAMPLE_XPLAT_INC)

# All the c++ src files we want to compile
LOCAL_SRC_FILES := $(wildcard $(CROSS_PLATFORM_PROJECT_EXAMPLE_XPLAT_SRC)/*.cpp)
LOCAL_SRC_FILES += $(wildcard $(CROSS_PLATFORM_PROJECT_EXAMPLE_XPLAT_SRC)/Resolver/*.cpp)

# This is important ! we also need to compile the SWIG generated c++ JNI classes
LOCAL_SRC_FILES += $(wildcard $(CROSS_PLATFORM_PROJECT_EXAMPLE_SWIG_SRC)/*.cpp)


# Adding required standard libs
# A Documentation about the list of standard libs can be found here: https://developer.android.com/ndk/guides/stable_apis.html
# The list of standard libs are found in <ndk_path>\platforms\<android_version>\<platform_abi>\usr\lib

# The Android Logger
LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

 
 

In the next article we will see how to integrate these make files to our Android gradle build system.

 

Good luck !

Leave a Reply

Your email address will not be published.

אתם יכולים להשתמש באפשרויות ותגי ה-HTMLהבאים: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>