在Android源码树中增加Android Library项目(静态链接方式的库)。
需要为Library项目添加Android.mk文件,以便于使用mm/mmm命令进行编译。
参考资料:
跟我一起写 Makefile - ChinaUnix.net
如何学习Android mk的原理?
答案:尝试模仿Android Build System的架构来写一套类似的Makefile。创建一个build目录下写类似的一系列mk文件,然后创建多个不同的子项目。
疑问:
2. Makefile中的target和其中各行命令的执行的次序是怎样的?如果控制?
其实Makefile中的每一行,都是按顺序执行的,比如一些变量的定义和赋值等。
./core/package.mk:376:PACKAGES.$(LOCAL_PACKAGE_NAME).PRIVATE_KEY := $(private_key)
./core/package.mk:377:PACKAGES.$(LOCAL_PACKAGE_NAME).CERTIFICATE := $(certificate)
./core/package.mk:427:PACKAGES.$(LOCAL_PACKAGE_NAME).OVERRIDES := $(strip $(LOCAL_OVERRIDES_PACKAGES))
./core/package.mk:428:PACKAGES.$(LOCAL_PACKAGE_NAME).RESOURCE_FILES := $(all_resources)
./core/package.mk:430:PACKAGES.$(LOCAL_PACKAGE_NAME).RESOURCE_OVERLAYS := $(package_resource_overlays)
通过以上RESOURCE_OVERLAYS是否能增加对Lib资源的引用?
ant编译示例:
要让Android源码树中的App项目引用Android Library项目,则需要:
1. 指定Lib项目的中间文件在Android代码树编译输出目录下(out目录)。
比如framework-res.apk的中间目录是:target Package: framework-res (out/target/product/tf4/obj/APPS/framework-res_intermediates/)
2. 让指定的App项目通过指定引用该Lib项目则可自动引用到Lib的jar和资源。
通过在Android mk编译脚本系统中(./build/目录子目录中的脚本文件)添加编译过程和变量定义,使其能够编译特殊的Lib项目和让App项目通过特定的属性能够引用到Lib项目。
示例:创建静态库项目MyLib, 及引用该项目的应用程序项目App01。
ant debug编译App01的过程:
-set-mode-check:
-set-debug-files:
-check-env:
[checkenv] Android SDK Tools Revision 22.0.1
[checkenv] Installed at /tools/android-sdk-linux_86
-setup:
[echo] Project Name: App01
[gettype] Project Type: Application
[echo] Creating output directories if needed...
[mkdir] Created dir: /App01/bin
[mkdir] Created dir: /App01/bin/res
[mkdir] Created dir: /App01/gen
[mkdir] Created dir: /App01/bin/classes
[mkdir] Created dir: /App01/bin/dexedLibs
[echo] ----------
[echo] Resolving Dependencies for App01...
[dependency] Library dependencies:
[dependency]
[dependency] ------------------
[dependency] Ordered libraries:
(由于App01引用了MyLib静态库项目,所以到此处则先编译MyLib库,然后才断续编译App01)
MyLib库编译过程:
1. 初始化project:
-setup:
[echo] Project Name: MyLib
[gettype] Project Type: Android Library
[dependency] Library dependencies:
[dependency] No Libraries
2. 处理资源res:
-code-gen:
[echo] Handling Resources...
[aapt] Found new input file
[aapt] Generating resource IDs...
(这一步生成了 R.java 文件)
3. 编译java:
-compile:
[javac] Compiling 1 source file to /MyLib/bin/classes
[echo] Creating library output jar file...
(这一步生成了jar文件)
-dex:
[echo] Library project: do not convert bytecode...
-crunch:
[crunch] Crunching PNG Files in source dir: /MyLib/res
[crunch] To destination dir: /MyLib/bin/res
[crunch] Crunched 0 PNG files to update cache
-package-resources:
[echo] Library project: do not package resources...
-package:
[echo] Library project: do not package apk...
-post-package:
-do-debug:
[echo] Library project: do not create apk...
至此,MyLib库编译完毕,断续编译App01项目:
-code-gen:
[mergemanifest] Merging AndroidManifest files into one.
[mergemanifest] Manifest merger disabled. Using project manifest only.
[echo] Handling aidl files...
[aidl] No AIDL files to compile.
[echo] ----------
[echo] Handling RenderScript files...
[renderscript] No RenderScript files to compile.
[echo] ----------
[echo] Handling Resources...
[aapt] Generating resource IDs...
[aapt] nothing matches overlay file ic_foo.png, for flavor ,,,,,,,,,,,,hdpi,,,,,,,
[aapt] nothing matches overlay file ic_foo.png, for flavor ,,,,,,,,,,,,mdpi,,,,,,,
[aapt] nothing matches overlay file ic_foo.png, for flavor ,,,,,,,,,,,,xxhdpi,,,,,,,
(当App项目与Lib项目有同名资源,而分别在不同的dpi目录中的话,则会出现以上overlay输出提示。这表明Lib中的资源是以overlay方式集成于App项目中来的。)
[echo] ----------
[echo] Handling BuildConfig class...
[buildconfig] Generating BuildConfig class.
-pre-compile:
-compile:
[javac] Compiling 4 source files to /App01/bin/classes
-post-compile:
-obfuscate:
-dex:
[dex] input: /App01/bin/classes
[dex] input: /MyLib/bin/classes.jar
[dex] input: /MyLib/libs/android-support-v4.jar
[dex] Pre-Dexing /MyLib/bin/classes.jar -> classes-753acc10e624775a3e02bc661a597b2a.jar
[dex] Pre-Dexing /MyLib/libs/android-support-v4.jar -> android-support-v4-e45eedfe9b2b84e819a265a649d9761c.jar
[dex] Converting compiled files and external libraries into /App01/bin/classes.dex...
[dx] Merged dex A (22 defs/11.3KiB) with dex B (12 defs/30.1KiB). Result is 34 defs/48.6KiB. Took 0.0s
[dx] Merged dex A (34 defs/48.6KiB) with dex B (316 defs/355.2KiB). Result is 350 defs/495.2KiB. Took 0.1s
-crunch:
[crunch] Crunching PNG Files in source dir: /App01/res
[crunch] To destination dir: /App01/bin/res
[crunch] Processing image to cache: /meizu/App01/res/drawable-hdpi/ic_foo.png => /App01/bin/res/drawable-hdpi/ic_foo.png
[crunch] (processed image to cache entry /App01/bin/res/drawable-hdpi/ic_foo.png: 53% size of source)
[crunch] Processing image to cache: /App01/res/drawable-mdpi/ic_foo.png => /App01/bin/res/drawable-mdpi/ic_foo.png
[crunch] (processed image to cache entry /App01/bin/res/drawable-mdpi/ic_foo.png: 0% size of source)
[crunch] Processing image to cache: /App01/res/drawable-xhdpi/ic_foo.png => /App01/bin/res/drawable-xhdpi/ic_foo.png
[crunch] (processed image to cache entry /App01/bin/res/drawable-xhdpi/ic_foo.png: 65% size of source)
[crunch] Processing image to cache: /App01/res/drawable-xxhdpi/ic_foo.png => /App01/bin/res/drawable-xxhdpi/ic_foo.png
[crunch] (processed image to cache entry /App01/bin/res/drawable-xxhdpi/ic_foo.png: 66% size of source)
[crunch] Crunched 4 PNG files to update cache
-package-resources:
[aapt] Creating full resource package...
[aapt] nothing matches overlay file ic_foo.png, for flavor ,,,,,,,,,,,,hdpi,,,,,,,
[aapt] nothing matches overlay file ic_foo.png, for flavor ,,,,,,,,,,,,mdpi,,,,,,,
[aapt] nothing matches overlay file ic_foo.png, for flavor ,,,,,,,,,,,,xxhdpi,,,,,,,
-package:
[apkbuilder] Current build type is different than previous build: forced apkbuilder run.
[apkbuilder] Creating App01-debug-unaligned.apk and signing it with a debug key...
-post-package:
-do-debug:
[zipalign] Running zip align on final apk...
[echo] Debug Package: /App01/bin/App01-debug.apk
[propertyfile] Creating new property file: /App01/bin/build.prop
[propertyfile] Updating property file: /App01/bin/build.prop
[propertyfile] Updating property file: /App01/bin/build.prop
[propertyfile] Updating property file: /App01/bin/build.prop
-post-build:
debug:
BUILD SUCCESSFUL