Projects表现为一个容器保存着代码和资源文件。SDK工具希望工程们遵从着特定的结构这样它就可以正确的对你的工程进行编译和打包,所以强烈推荐你使用Eclipse的ADT或者android工具来创建工程。有三种类别的工程,它们共享统一的通用结构,但是在功能上各不同:
Android Projects
android工程包含工程源文件,资源文件,ant文件,AndroidManifest文件等,这是应用工程的主要类型,而且它的内容最总被打包到.apk来装在应用终端上。
Test Projects
这类工程包含代码来测试你的应用程序,这些可以被built进入应用程序,并且运行在终端上。
Library Projects
这些工程包含可共享的android代码和资源,你可以在Android project进行引用。这对公共代码重用的时候很有用。库工程不能被安装在终端设备上,但是,它们会被拉进.apk在build的时候。
详解每个工程
Android Projects
src/
src/your/package/namespace/ActivityName.java
. All other source code files (
such as .java
or .aidl
files) go here as well.
bin
.apk
file and other compiled resources.自动编译默认的目录
jni
gen/
R.java
file and interfaces created from AIDL files.资源索引和aidl接口类
assets/
.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/
anim/
color/
drawable/
layout/
menu/
raw/
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/
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/
PreferenceScreen
, AppWidgetProviderInfo
, orSearchability Metadata. See Application Resources for more information about configuring these application components.这个功能多一些。
libs/
AndroidManifest.xml
build.properties
build.xml
default.properties
<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