Android Library Project 的使用小结以及脚本打包事项
作者:徐建祥日期:2012/10/16网址:http://www.anymobile.org一、Android ProjectsAndroid ProjectsAn Android project is the container for your application's source code, resource files, and files such
作者:徐建祥
日期:2012/10/16
网址:http://www.anymobile.org
一、Android Projects
Android Projects.apk
file that you install on a device.
These projects contain shareable Android source code and resources that you can reference in Android projects. This is useful when you have common code that you want to reuse. Library projects cannot be installed onto a device, however, they are pulled into the .apk
file at build time.
二、项目结构
Platform Project = Android SDK Project + Library Projects(N)
其中:Android SDK Project的“AndroidManifest.xml”需整合各个Libary Project的“AndroidManifest.xml”,并在jar中添加各个Libary Project的“bin/**.jar”。
三、Eclipse 截图
四、ANT 脚本
build.xml
<target name="resource-src" depends="dirs">
<echo>Generating R.java / Manifest.java from the resources...</echo>
<exec executable="${aapt}" failοnerrοr="true">
<arg value="package" />
<arg value="-f" />
<arg value="-m" />
<arg value="--auto-add-overlay" />
<arg value="-J" />
<arg value="${gendir}" />
<arg value="-M" />
<arg value="${tfb-sdk-dir}/AndroidManifest.xml" />
<arg value="-S" />
<arg value="${tfb-sdk-dir}/${resource-dir}" />
<arg value="-S" />
<arg value="${resource-dir}" />
<arg value="-A" />
<arg value="${tfb-sdk-dir}/${asset-dir}" />
<arg value="-I" />
<arg value="${android-jar}" />
</exec>
<exec executable="${aapt}" failοnerrοr="true">
<arg value="package" />
<arg value="-f" />
<arg value="-m" />
<arg value="--auto-add-overlay" />
<arg value="-J" />
<arg value="${gendir}" />
<arg value="-M" />
<arg value="AndroidManifest.xml" />
<arg value="-S" />
<arg value="${tfb-sdk-dir}/${resource-dir}" />
<arg value="-S" />
<arg value="${resource-dir}" />
<arg value="-A" />
<arg value="${asset-dir}" />
<arg value="-I" />
<arg value="${android-jar}" />
</exec>
</target>
注意事项: a. 通过aapt创建所有library project的R.java均生成到Android Project的gen目录下,不会发生资源冲突;
b. 通过-S添加所有项目(library projects+Android Project)的资源目录 ,这样每个项目对于的包路径下的R文件内容一样 ,并且也不需要拷贝所有库项目的资源文件到android项目的资源目录中了。
--(参考Eclipse的编译后效果)
五、资源文件
资源文件中,有个目录比较特殊(values),编译的时候会将该资源目录下的内容编译到class中,比如:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyAppName</string>
</resources>
如果不加处理的编译,会报异常:
[exec] ../tfb_sdk/res/values/sdk_strings.xml:6: error: Resource at app_name appears in overlay but not in the base package; use <add-resource> to add.
有两种处理方法:
1. 讲各个项目中的values打头的资源目录下的xml文件均拷贝到当前编译的Android Project的对于目录下;
2. 在values对于的资源中使用<add-resource>重写。
第一种方法显然是我们不想操作的,资源可能冲突,也需要额外的工作,安全加上程序猿的惰性,只能选择第二种了,也有两种添加方法:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<add-resource type="string" name="app_name">MyAppName</add-resource>
</resources>
//这种写法会造成res里面的layout等资源无法调用,造成开发困扰,Eclipse错误提示如下:
[2012-10-16 13:23:46 - ftb_main] F:\workfolder\ftb_sdk\res\layout\sdk_title_bar_layout.xml:8: error: Error:No resource found that matches the given name (at 'text' with value '@string/sdk_back').
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<add-resource type="string" name="app_name" />
<string name="app_name">MyAppName</string>
</resources>
//这种写法是正解,虽然稍微麻烦了点。
如果是color,调用方法需修改:TextView.setTextColor(R.color.gray)=> TextView.setTextColor(getResources().getColor(R.color.gray))
最后再提一点:library project中调用资源的地方,如有switch(比如重载onClick())资源ID的,需改成if...else...;至此,整个平台项目就算搭建完成了。
好久不写技术文档了,写的有点零散,
附 Resource Types
Each of the documents in this section describe the usage, format and syntax for a certain type of application resource that you can provide in your resources directory (res/).
Here's a brief summary of each resource type:
Animation Resources
Define pre-determined animations.
Tween animations are saved in res/anim/ and accessed from the R.anim class.
Frame animations are saved in res/drawable/ and accessed from the R.drawable class.
Color State List Resource
Define a color resources that changes based on the View state.
Saved in res/color/ and accessed from the R.color class.
Drawable Resources
Define various graphics with bitmaps or XML.
Saved in res/drawable/ and accessed from the R.drawable class.
Layout Resource
Define the layout for your application UI.
Saved in res/layout/ and accessed from the R.layout class.
Menu Resource
Define the contents of your application menus.
Saved in res/menu/ and accessed from the R.menu class.
String Resources
Define strings, string arrays, and plurals (and include string formatting and styling).
Saved in res/values/ and accessed from the R.string, R.array, and R.plurals classes.
Style Resource
Define the look and format for UI elements.
Saved in res/values/ and accessed from the R.style class.
More Resource Types
Define values such as booleans, integers, dimensions, colors, and other arrays.
Saved in res/values/ but each accessed from unique R sub-classes (such as R.bool, R.integer, R.dimen, etc.).
More Resource Types
defines more types of resources you can externalize, including:
Bool
XML resource that carries a boolean value.
Color
XML resource that carries a color value (a hexadecimal color).
Dimension
XML resource that carries a dimension value (with a unit of measure).
ID
XML resource that provides a unique identifier for application resources and components.
Integer
XML resource that carries an integer value.
Integer Array
XML resource that provides an array of integers.
Typed Array
XML resource that provides a TypedArray (which you can use for an array of drawables).
更多推荐
所有评论(0)