Build Cookbook
来源:互联网 发布:经济数据点评 编辑:程序博客网 时间:2024/05/21 05:55
The Android Build Cookbook offers code snippets to help you quickly implement some common build tasks. For additional instruction, please see the other build documents in this section.
Building a simple APK
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Build all java files in the java subdirectory LOCAL_SRC_FILES := $(call all-subdir-java-files) # Name of the APK to build LOCAL_PACKAGE_NAME := LocalPackage # Tell it to build an APK include $(BUILD_PACKAGE)
Building a APK that depends on a static .jar file
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # List of static libraries to include in the package LOCAL_STATIC_JAVA_LIBRARIES := static-library # Build all java files in the java subdirectory LOCAL_SRC_FILES := $(call all-subdir-java-files) # Name of the APK to build LOCAL_PACKAGE_NAME := LocalPackage # Tell it to build an APK include $(BUILD_PACKAGE)
Building a APK that should be signed with the platform key
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Build all java files in the java subdirectory LOCAL_SRC_FILES := $(call all-subdir-java-files) # Name of the APK to build LOCAL_PACKAGE_NAME := LocalPackage LOCAL_CERTIFICATE := platform # Tell it to build an APK include $(BUILD_PACKAGE)
Building a APK that should be signed with a specific vendor key
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Build all java files in the java subdirectory LOCAL_SRC_FILES := $(call all-subdir-java-files) # Name of the APK to build LOCAL_PACKAGE_NAME := LocalPackage LOCAL_CERTIFICATE := vendor/example/certs/app # Tell it to build an APK include $(BUILD_PACKAGE)
Adding a prebuilt APK
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Module name should match apk name to be installed. LOCAL_MODULE := LocalModuleName LOCAL_SRC_FILES := $(LOCAL_MODULE).apk LOCAL_MODULE_CLASS := APPS LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX) include $(BUILD_PREBUILT)
Adding a Static Java Library
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Build all java files in the java subdirectory LOCAL_SRC_FILES := $(call all-subdir-java-files) # Any libraries that this library depends on LOCAL_JAVA_LIBRARIES := android.test.runner # The name of the jar file to create LOCAL_MODULE := sample # Build a static jar file. include $(BUILD_STATIC_JAVA_LIBRARY)
Android.mk Variables
These are the variables that you'll commonly see in Android.mk files, listedalphabetically. First, a note on the variable naming:
- LOCAL_ - These variables are set per-module. They are cleared by the
include $(CLEAR_VARS)
line, so you can rely on them being empty after including that file. Most of the variables you'll use in most modules are LOCAL_ variables. - PRIVATE_ - These variables are make-target-specific variables. That means they're only usable within the commands for that module. It also means that they're unlikely to change behind your back from modules that are included after yours. This link to the make documentation describes more about target-specific variables.
- HOST_ and TARGET_ - These contain the directories and definitions that are specific to either the host or the target builds. Do not set variables that start with HOST_ or TARGET_ in your makefiles.
- BUILD_ and CLEAR_VARS - These contain the names of well-defined template makefiles to include. Some examples are CLEAR_VARS and BUILD_HOST_PACKAGE.
- Any other name is fair-game for you to use in your Android.mk. However, remember that this is a non-recursive build system, so it is possible that your variable will be changed by another Android.mk included later, and be different when the commands for your rule / module are executed.
include $(BUILD_PACKAGE)
set thisto the set of files you want built into your app. Usually:LOCAL_ASSET_FILES += $(call find-subdir-assets)
Additional directories to instruct the C/C++ compilers to look for headerfiles in. These paths are rooted at the top of the tree. UseLOCAL_PATH
if you have subdirectories of your own that youwant in the include paths. For example:
LOCAL_C_INCLUDES += extlibs/zlib-1.2.3
LOCAL_C_INCLUDES += $(LOCAL_PATH)/src
You should not add subdirectories of include toLOCAL_C_INCLUDES
, instead you should reference those filesin the#include
statement with their subdirectories. Forexample:
#include <utils/KeyedVector.h>
not #include <KeyedVector.h>
LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1
The set of files to copy to the install include tree. You must alsosupply LOCAL_COPY_HEADERS_TO
.
This is going away because copying headers messes up the error messages, andmay lead to people editing those headers instead of the correct ones. It alsomakes it easier to do bad layering in the system, which we want to avoid. Wealso aren't doing a C/C++ SDK, so there is no ultimate requirement to copy anyheaders.
LOCAL_COPY_HEADERS_TOThe directory within "include" to copy the headers listed inLOCAL_COPY_HEADERS
to.
This is going away because copying headers messes up the error messages, andmay lead to people editing those headers instead of the correct ones. It alsomakes it easier to do bad layering in the system, which we want to avoid. Wealso aren't doing a C/C++ SDK, so there is no ultimate requirement to copy anyheaders.
LOCAL_CPP_EXTENSIONIf your C++ files end in something other than ".cpp
",you can specify the custom extension here. For example:LOCAL_CPP_EXTENSION := .cc
LOCAL_CPPFLAGS += -ffriend-injection
LOCAL_CPPFLAGS
is guaranteed to be after LOCAL_CFLAGS
on the compile line, so you can use it to override flags listed inLOCAL_CFLAGS
LOCAL_CXXIf you want to use a different C++ compiler for this module, set LOCAL_CXXto the path to the compiler. If LOCAL_CXX is blank, the appropriate defaultcompiler is used.LOCAL_DX_FLAGS LOCAL_EXPORT_PACKAGE_RESOURCES LOCAL_FORCE_STATIC_EXECUTABLEIf your executable should be linked statically, set LOCAL_FORCE_STATIC_EXECUTABLE:=true
. There is a very shortlist of libraries that we have in static form (currently only libc). This isreally only used for executables in /sbin on the root filesystem.
Files that you add to LOCAL_GENERATED_SOURCES
will beautomatically generated and then linked in when your module is built.See theCustom Tools template makefile for anexample.
When linking Java apps and libraries, LOCAL_JAVA_LIBRARIES
specifies which sets of java classes to include. Currently there aretwo of these:core
and framework
.In most cases, it will look like this:
LOCAL_JAVA_LIBRARIES := core framework
Note that setting LOCAL_JAVA_LIBRARIES
is not necessary(and is not allowed) when building an APK with"include $(BUILD_PACKAGE)
". The appropriate librarieswill be included automatically.
You can pass additional flags to the linker by settingLOCAL_LDFLAGS
. Keep in mind that the order of parameters isvery important to ld, so test whatever you do on all platforms.
LOCAL_LDLIBS
allows you to specify additional librariesthat are not part of the build for your executable or library. Specifythe libraries you want in -lxxx format; they're passed directly to the link line. However, keep in mind that there will be no dependency generatedfor these libraries. It's most useful in simulator builds where you wantto use a library preinstalled on the host. The linker (ld) is a particularlyfussy beast, so it's sometimes necessary to pass other flags here if you'redoing something sneaky. Some examples:
LOCAL_LDLIBS += -lcurses -lpthread
LOCAL_LDLIBS += -Wl,-z,origin
LOCAL_MODULE
is the name of what's supposed to be generatedfrom your Android.mk. For exmample, for libkjs, theLOCAL_MODULE
is "libkjs" (the build system adds the appropriate suffix -- .so .dylib .dll).For app modules, useLOCAL_PACKAGE_NAME
instead of LOCAL_MODULE
.LOCAL_MODULE_PATHInstructs the build system to put the module somewhere other than what'snormal for its type. If you override this, make sure you also setLOCAL_UNSTRIPPED_PATH
if it's an executable or a shared libraryso the unstripped binary has somewhere to go. An error will occur if you forgetto.See Putting modules elsewhere for more.
LOCAL_MODULE_STEM LOCAL_MODULE_TAGSSet LOCAL_MODULE_TAGS
to any number of whitespace-separatedtags.
This variable controls what build flavors the package gets included in. For example:
user
: include this in user/userdebug buildseng
: include this in eng buildstests
: the target is a testing target and makes it available for testsoptional
: don't include this
LOCAL_PACKAGE_NAME
is the name of an app. For example,Dialer, Contacts, etc.LOCAL_POST_PROCESS_COMMANDFor host executables, you can specify a command to run on the moduleafter it's been linked. You might have to go through some contortionsto get variables right because of early or late variable evaluation:
module := $(HOST_OUT_EXECUTABLES)/$(LOCAL_MODULE)
LOCAL_POST_PROCESS_COMMAND := /Developer/Tools/Rez -d __DARWIN__ -t APPL\
-d __WXMAC__ -o $(module) Carbon.r
Set LOCAL_REQUIRED_MODULES
to any number of whitespace-separatedmodule names, like "libblah" or "Email". If this module is installed, allof the modules that it requires will be installed as well. This can beused to, e.g., ensure that necessary shared libraries or providers areinstalled when a given app is installed.
LOCAL_SHARED_LIBRARIES := \
libutils \
libui \
libaudio \
libexpat \
libsgl
LOCAL_SRC_FILES
to know what sourcefiles to compile -- .cpp .c .y .l .java. For lex and yacc files, it knowshow to correctly do the intermediate .h and .c/.cpp files automatically. Ifthe files are in a subdirectory of the one containing the Android.mk, prefixthem with the directory name:LOCAL_SRC_FILES := \
file1.cpp \
dir/file2.cpp
LOCAL_STATIC_LIBRARIES := \
libutils \
libtinyxml
LOCAL_MODULE_PATH
for an executable or ashared library. If you overrodeLOCAL_MODULE_PATH
, but not LOCAL_UNSTRIPPED_PATH
, an error will occur.See Putting modules elsewhere for more.
LOCAL_WHOLE_STATIC_LIBRARIESThese are the static libraries that you want to include in your module without allowingthe linker to remove dead code from them. This is mostly useful if you want to add a static libraryto a shared library and have the static library's content exposed from the shared library.LOCAL_WHOLE_STATIC_LIBRARIES := \
libsqlite3_android
LOCAL_YACCFLAGS := -p kjsyy
from:http://www.kandroid.org/online-pdk/guide/build_cookbook.html
- Build Cookbook
- Build Cookbook
- Build Cookbook
- Build Cookbook
- Build Cookbook
- Build Cookbook
- Android Build Cookbook
- ANDROID Porting系列三、Build Cookbook
- Cookbook
- cookbook
- JUnit Cookbook
- Snort Cookbook
- C++ Cookbook
- XSLT Cookbook
- SQL Cookbook
- C# Cookbook
- Eclipse Cookbook
- Java Cookbook
- c++ 虚析构函数
- 系统HANG住了,怎么办?
- 混响音效
- IT职场人生系列之二十三:知识体系(专家与杂家)
- Git教程【译】
- Build Cookbook
- Treeview绑定数据库
- display:table实现分页功能的例子
- MySQL索引背后的数据结构及算法原理
- Windows 下单机最大TCP连接数
- iOS开发之Objective-C语法之词典对象
- 这个世界从来没有任何一件工作叫“钱多、事少、离家近”
- 编程珠玑番外篇 -M. 软件工具的设计哲学1
- 11G Flashback Data Archive新特性的研究