作者:徐建祥
日期:2012/10/16
网址:http://www.anymobile.org


一、Android Projects

Android Projects
An Android project is the container for your application's source code, resource files, and files such as the Ant build and Android Manifest file. An application project is the main type of project and the contents are eventually built into an .apkfile that you install on a device.
Test Projects
These projects contain code to test your application projects and are built into applications that run on a device.
Library Projects

These projects containshareableAndroidsource code and resourcesthat 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.apkfile 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”。

Android Library Project 的使用小结以及脚本打包事项_第1张图片

三、Eclipse 截图

Android Library Project 的使用小结以及脚本打包事项_第2张图片

四、ANT 脚本

build.xml

    <target name="resource-src" depends="dirs">      <echo>Generating R.java / Manifest.java from the resources...</echo>          <exec executable="${aapt}" failonerror="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}" failonerror="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 atapp_nameappears inoverlaybut 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 inres/anim/and accessed from the R.anim class.
Frame animations are saved inres/drawable/and accessed from the R.drawable class.


ColorState ListResource
Define a color resources that changes based on the View state.
Saved inres/color/and accessed from the R.color class.


Drawable Resources
Define various graphics with bitmaps or XML.
Saved inres/drawable/and accessed from the R.drawable class.


Layout Resource
Define the layout for your application UI.
Saved inres/layout/and accessed from the R.layout class.


Menu Resource
Define the contents of your application menus.
Saved inres/menu/and accessed from the R.menu class.


String Resources
Define strings, string arrays, and plurals (and include string formatting and styling).
Saved inres/values/and accessed from the R.string, R.array, and R.plurals classes.


Style Resource
Define the look and format for UI elements.
Saved inres/values/and accessed from the R.style class.


More Resource Types
Define values such as booleans, integers, dimensions, colors, and other arrays.
Saved inres/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).

更多相关文章

  1. android用代码获取布局文件
  2. eclipse 导出项目到 android studio .so 库
  3. android rdp 远程桌面项目
  4. 清除Mac中Android studio的配置文件
  5. Android读取assests目录下文件
  6. 学习android无私的资源
  7. android 资源文件

随机推荐

  1. 介绍一款好用的终端工具 Screen
  2. Ansible入门之Playbook
  3. 努力无用论?我不信。。。
  4. nginx+uwsgi+django环境搭建
  5. django1.8数据迁移
  6. 学习的道与术
  7. django模型使用
  8. 闲聊僵尸进程
  9. 如何让学到的运维知识系统化?
  10. 失眠三重天