Managing Projects

Projects表现为一个容器保存着代码和资源文件。SDK工具希望工程们遵从着特定的结构这样它就可以正确的对你的工程进行编译和打包,所以强烈推荐你使用Eclipse的ADT或者android工具来创建工程。有三种类别的工程,它们共享统一的通用结构,但是在功能上各不同:

Android Projects

android工程包含工程源文件,资源文件,ant文件,AndroidManifest文件等,这是应用工程的主要类型,而且它的内容最总被打包到.apk来装在应用终端上。


Test Projects

这类工程包含代码来测试你的应用程序,这些可以被built进入应用程序,并且运行在终端上。


Library Projects

这些工程包含可共享的android代码和资源,你可以在Android project进行引用。这对公共代码重用的时候很有用。库工程不能被安装在终端设备上,但是,它们会被拉进.apk在build的时候。


详解每个工程

Android Projects

src/
Contains your stub Activity file, which is stored at  src/your/package/namespace/ActivityName.java. All other source code files ( such as .java or .aidl files) go here as well.
bin
Output directory of the build. This is where you can find the final  .apk file and other compiled resources.自动编译默认的目录
jni
Contains native code sources developed using the Android NDK. For more information, see the  Android NDK documentation.这个待研究
gen/
Contains the Java files generated by ADT, such as your  R.java file and interfaces created from AIDL files.资源索引和aidl接口类
assets/
This is empty. You can use it to store raw asset files. Files that you save here are compiled into an  .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the the  AssetManager. For example, this is a good location for textures and game data.
res/
Contains application resources, such as drawable files, layout files, and string values. See  Application Resources for more information.
anim/
For XML files that are compiled into animation objects. See the  Animation resource type.
color/
For XML files that describe colors. See the  Color Values resource type.
drawable/
For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML files that describe Drawable shapes or a Drawable objects that contain multiple states (normal, pressed, or focused). See the  Drawable resource type.
layout/
XML files that are compiled into screen layouts (or part of a screen). See the  Layout resource type.
menu/
For XML files that define application menus. See the  Menus resource type.
raw/
For arbitrary raw asset files. Saving asset files here instead of in the  assets/ directory only differs in the way that you access them. These files are processed by aapt and must be referenced from the application using a resource identifier in the  R class. For example, this is a good place for media, such as MP3 or Ogg files.
values/
For XML files that are compiled into many kinds of resource. Unlike other resources in the  res/ directory, resources written to XML files in this folder are not referenced by the file name. Instead, the XML element type controls how the resources is defined within them are placed into the  R class.
xml/
For miscellaneous XML files that configure application components. For example, an XML file that defines a PreferenceScreenAppWidgetProviderInfo, orSearchability Metadata. See Application Resources for more information about configuring these application components.这个功能多一些。
libs/
Contains private libraries.私有库,自己的jar等
AndroidManifest.xml
The control file that describes the nature of the application and each of its components. For instance, it describes: certain qualities about the activities, services, intent receivers, and content providers; what permissions are requested; what external libraries are needed; what device features are required, what API Levels are supported or required; and others. See the  AndroidManifest.xml documentation for more information
build.properties
Customizable properties for the build system. You can edit this file to override default build settings used by Ant and provide a pointer to your keystore and key alias so that the build tools can sign your application when built in release mode. If you use Eclipse, this file is not used.如果在eclipse中,这个文件是无用的。
build.xml
The Ant build file for your project. This is only applicable for projects that you create on the command line.
default.properties
This file contains project settings, such as the build target. This files is integral to the project, as such, it should be maintained in a Source Revision Control system. Do not edit the file manually.尽量不要手动修改这个文件。可以在Project的“ Properties”进行修改。


Test Projects
测试工程包含Android应用程序你写来用于 Testing and Instrumentation framework.这个框架是扩展了JUnit测试框架,并且添加了Android系统对象。文件结构和Android project是一致的。需要注明的AndroidManifest.xml中一定要有<instrumentation>。


Library Projects

一个android的库工程是一个保有共享的资源代码。其他的应用程序可以引用这个库工程,并且会在编译生成apk的时候包含库工程的资源。多个应用程序可以引用同一个库工程,并且一个应用程序可以同时引用多个库工程。

要注意的是,需要SDK Tools r8 or newer才能完全支持库工程对于所有的android平台版本。具体的例子可以参加应用程序中的sample的TicTacToeMain。

如果你有公用代码和资源对于多个android应用工程,你可以把他们搬到一个库工程中,这样就可以更简单来维护应用程序和版本。下面是一些通用的情况对于用库工程:

1,如果你开发了多个相关的应用程序都用了一些相同的组件,你可以把这些redundant components搬出来,然后创建一个单独,可以复用的库工程。

2,如果一个开发一个程序有付费和免费两个版本,你可以把相同的部分搬到库工程中。两个不同的独立的工程,是不同的package name,都引用着这个库工程,提供者一些不同的服务。


从结构上来讲,一个库工程的结构和普通的android工程没有任何分别。

但是一个库工程和标准工程的分别是它不能被单独build成为apk,来被安装在android设备上,也不能被输出为自包含的jar文件,它只能被引用工程一期build成为一个apk。要注意的是,在build的时候,两个工程(或者多个工程,同时引用了多个库工程)的资源要为合并,这样来共同生成一个apk。但是资源的id有可能会重复。这里就需要设置优先级,重复时优先引用谁,一般的设计应该避免出现这种情况,可以以包名为前缀等方法。最后,一个库工程是不能引用另外一个库工程的,但是它可以引用jar包。

下面的是注意事项,有些重复,算小结吧,


Using prefixes to avoid resource conflicts 请使用前缀来避免资源冲突,这个好理解,就是多用长名称,比如:geek_icon.png 代替 icon.png

To avoid resource conflicts for common resource IDs, consider using a prefix or other consistent naming scheme that is unique to the project (or is unique across all projects).

No export of library project to JAR 库工程是无法打包成 JAR 使用的,因为资源ID会冲突。

A library cannot be distributed as a binary file (such as a jar file). This is because the library project is compiled by the main project to use the correct resource IDs.

A library project can include a JAR library 库工程自身仍然可以 include 一个(或多个) JAR 库文件,但你得人为地在主工程中也 include 这些 JAR 库。

You can develop a library project that itself includes a JAR library, however you need to manually edit the dependent application project’s build path and add a path to the JAR file.

A library project can depend on an external JAR library 库工程可以依赖一个外部 JAR 库

You can develop a library project that depends on an external library (for example, the Maps external library). In this case, the dependent application must build against a target that includes the external library (for example, the Google APIs Add-On). Note also that both the library project and the dependent application must declare the external library their manifest files, in a element.

Library project can not include raw assets 库工程不能使用 include /assets 目录,若仍要使用,则必须把 东东 都复制到 主工程的 /assets 目录中。

The tools do not support the use of raw asset files in a library project. Any asset resources used by an application must be stored in the assets/ directory of the application project itself.

Targeting different Android platform versions in library project and application project 库工程使用的 SDK 版本不得高于 主工程的 SDK 版本

A library is compiled as part of the dependent application project, so the API used in the library project must be compatible with the version of the Android library used to compile the application project. In general, the library project should use an API level that is the same as — or lower than — that used by the application. If the library project uses an API level that is higher than that of the application, the application project will fail to compile. It is perfectly acceptable to have a library that uses the Android 1.5 API (API level 3) and that is used in an Android 1.6 (API level 4) or Android 2.1 (API level 7) project, for instance.

No restriction on library package name 库工程的包名,不必改变,主工程的包名,才是生成的 apk 的包名,跟库工程没关系。

There is no requirement for the package name of a library to be the same as that of applications that use it.

Multiple R classes in gen/ folder of application project 会生成多个 R 文件,有几个工程(含库工程 & 主工程)就会生成几个 R 文件,只要编译通过,你不必担心 R 文件的个数。

When you build the dependent application project, the code of any libraries is compiled and merged to the application project. Each library has its own R class, named according to the library’s package name. The R class generated from the resources of the main project and of the library is created in all the packages that are needed including the main project’s package and the libraries’ packages.

设置成为库工程非常简单,只要在工程属性中的android标签下设置is Library打上勾就可以了,而引用的时候直接在下面进行一个add操作,选择你要引用的库工程project即可,和纯java的工程依赖(java build path,project)差不多,都是直接声明一个引用即可。至于不用eclipse,直接命令行来操作的,一般用不到的吧,

http://developer.android.com/guide/developing/projects/projects-cmdline.html,

示范图:


参考:http://developer.android.com/guide/developing/projects/index.html

http://lichsword.net/blog/archives/396






你可能感兴趣的:(android,jar,application,Build,library,resources)