Android Library Project 的使用小結以及指令碼打包事項

來源:互聯網
上載者:User

作者:徐建祥
日期: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  .apk file
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 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}" 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 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).

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.