2011年11月6日 星期日

[Android] Android Developer Tool (ADT) - Resources compilation

[Definition]
  • A plugin for Eclipse that provides a suite of tools that are integrated with the Eclipse IDE.
  • It can integrated Android project creation, building, packaging, installation, and debugging.
    • Add components based on the Android Framework API.
      • Java programming language and XML editors
      • Create an application UI.
    • SDK Tools integration
      • Debug your applications using the Android SDK tools.
    • Export signed (or unsigned) .apk files in order to distribute your application.

[ADT 14.0.0 (October 2011)]
  • Added webcam support to Android 4.0 or later platforms to emulate rear-facing cameras
  • Resource compilation
    • Changed how library projects are built in Eclipse.
      • You need SDK Tools r14 or newer to use the new library project feature that generates each library project into its own JAR file.
    • Improved incremental builds so that resource compilation runs less frequently.
    • Builds no longer run when you edit strings or layouts (unless you add a new id)
    • No longer run once for each library project.
    • No longer happens for normal save operations. It only happens when running or debugging (the build option that lets you disable the packaging step, which was introduced in ADT 12, is now on by default.)
  • Added the "Go to Matching" (Ctrl-Shift-P) feature, which lets you jump between opening and closing tags in XML files.

[Library project]
  • Source code in the library project can access its own resources through its R class.
  • Can reference other library projects and can import an external library (JAR) in the normal way.
  • Differs from an standard Android application project:
    • Cannot compile it directly to its own .apk and run it on an Android device.
    • Can not include raw assets.
    • Cannot export the library project to a self-contained JAR file, as you would do for a true library. (Changed in r14?)
      • Instead, you must compile the library indirectly, by referencing the library in the dependent application and building that application.
Usage
  • When you build an application that depends on a library project, the SDK tools compile the library into a temporary JAR file and uses it in the main project, then uses the result to generate the .apk. 
    • ADT creates virtual source folders linking to the library source folders.
    • Because library dependencies are controlled by the content of project.properties (formerly default.properties), ADT must dynamically add and remove these virtual source folders whenever a project is opened and closed.
    • Source-based library prevents distribution of re-usable components(?) unless one is willing to include the source code.
  • In cases...
    • A resource ID is defined in both the application and the library
      • The tools ensure that the resource declared in the application gets priority and that the resource in the library project is not compiled into the application .apk. (or the library with highest priority, and discard the other resource?)
      • This gives your application the flexibility to either use or redefine any resource behaviors or values that are defined in any library.
      • Be aware that common resource IDs are likely to be defined in more than one project and will be merged, with the resource from the application or highest-priority library taking precedence.
    • When two libraries referenced from an application define the same resource ID
      • The tools select the resource from the library with higher priority and discard the other.
  • At build time, the libraries are merged with the application one at a time, starting from the lowest priority to the highest.

Previously (< r14)
Library projects were handled as extra resource and source code folders to be used when compiling the resources and the application’s source respectively. While this worked fine for most cases, there were two issues.
  1. Developers asked us for the ability to distribute a library as a single jar file that included both compiled code and resources. The nature of Android resources, with their compiled IDs prevented this.
  2. The implementation of the library projects was extremely fragile in Eclipse. Adding extra source folders outside of the project folders is non-trivial when it needs to be handled automatically, in a way that doesn’t expose a user’s local installation path (which is required for people working in teams through a source control system such as SVN or git).
To fix all of these issues, we have decided to move to library projects generating a jar file that is directly used by other projects.

The first impact of this change
  • Had to change the way resource IDs are generated.
  • The main projects and all required libraries
    • The resource IDs will still be generated as final static int in the final R class generated in the main project with the resources.
  • The new library project
    • The resource IDs generated by libraries to be non final.
  • Note
    • In a regular projects
      • Resource IDs are generated as final static int. 
      • These constants then get inlined into the classes that use them. 
      • This means the compiled classes do not access the values stored in, say, R.layout.myui, but instead directly embed the value 0x000042.
    • To make the compiled classes not embed this value and instead access it from the R classes, library project will generate the resources IDs as static int only.
    • This prevents the Java compiler from inlining the values in the library code
  • Therefore prevents usage of the switch statement in the library code.
    • Eclipse provides a refactoring action to convert from switch statements to if/else.

Summary
Library projects containing the jar file directly instead of the source code.(Support in r15)

Related
[Android] export android project 為 jar 檔


By the way
  • /res/xml
    • 存儲xml格式的文件。
    • 會被編譯成二進制格式放到最終的apk裡。
    • 可以通過R類來訪問這裡的文件,並且解析裡面的內容。
  • /res/raw
    • 這裡的文件會原封不動的存儲到設備上。
    • 不會被編譯為二進制形式,訪問的方式也是通過R類。
  • /assets
    • 不會被編譯成二進制形式之外。
    • 訪問方式是通過文件名,而不是資源ID。
    • 可以在這裡任意的建立子目錄,而/res目錄中的資源文件是不能自行建立子目錄的。

Pending Study Issues
  • The detail about eclipse export java/android jar.


* Reference
- Android Application, Android Libraries and Jar Libraries **** Although it describes the structure before r14 but it is still very worth to read.
- ADT Plugin for Eclipse | Android Developers
- Android Developer Tools
- Build changes in revision 14 - Android Tools Project Site ***
- SDK Tools
- Library Projects
Android Developers Blog: Changes to Library Projects in Android SDK Tools, r14 ***
Managing Projects | Android Developers **
- Building and Running *
- Developing In Eclipse, with ADT
- I am Happy Android,3C: android /res/xml /res/raw /assets

沒有留言:

張貼留言