您的位置:首页 > 移动开发 > Android开发

Android的Library工程

2015-06-25 15:11 309 查看


Android的Library工程

JAN 4TH, 2013 | COMMENTS


为什么要使用Android的Library工程?

简单的说就是减少代码拷贝。

An Android library project is a development project that holds shared Android source code and resources. Other Android application projects can reference the library project and, at build time, include its compiled sources in their .apk files. Multiple application
projects can reference the same library project and any single application project can reference multiple library projects.


Android Library工程开发中需要考虑的问题


能把Android的Library工程打成一个单独的jar包发布吗?

如果工程是纯粹的Java文件,那么这和一个普通的java library工程没有区别, 可以打成jar包来发布。 在
custom_rules.xml
里添加如下两个target即可使用
ant
来编译jar包了:
<target name="jar" depends="-compile" description="Build binary jar">
<jar destfile="${ant.project.name}.jar" basedir="${out.classes.absolute.dir}" >
<include name="com/example/android/**" />
</jar>
</target>

<target name="src-jar" description="bundle sources in a jar">
<jar destfile="${ant.project.name}-src.jar" basedir="src"/>
</target>


但是如果Library工程有资源(string, layout等),针对 ADT r21 的答案是不行的。

You cannot export a library project to a JAR file. A library cannot be distributed as a binary file (such as a JAR file). This will be added in a future version of the SDK Tools.

针对Library工程有资源但又不想发布源代码的一种workaround的方法是把src目录进行编译后成jar包,然后发布的时候发布资源和jar包。在主工程中把Library工程的jar包放在libs目录下即可正常编译。 当然如果工程文件的
AndroidManifest.xml
里的内容需要合并到主工程的
AndroidManifest.xml
里。
合并
AndroidManifest.xml
的需求已经在Android
Tools的Roadmap里,但何时能实现还未知。


资源冲突

如果在Library的
strings.xml
和主工程的
strings.xml
都定义了
hello_world

那么编译后的apk会优先使用主工程的
hello_world

为了避免冲突,建议在Library的工程中对所有资源添加前缀。

Since the tools merge the resources of a library project with those of a dependent application project, a given resource ID might be defined in both projects. In this case, the tools select the resource from the application, or the library with highest priority,
and discard the other resource. As you develop your applications, 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.
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).


Library工程文件不能包含assets

Library工程不能在assets目录下存放raw资源,必须存放在主工程的assets目录下。

但是支持使用res目录来存放资源。


每个Library工程都有自己的R文件

每个Library工程在编译后都会在gen目录生成自己的R.java文件。


为Library工程创建测试工程

要的测试工程本身可以直接引用Library工程,测试工程本身也是APK。

通常的做法是在Library工程下创建一个tests目录用来存放测试工程。

需要在测试工程的
AndroidManifest.xml
<application>
中加入让它成为一个测试工程。
<uses-library android:name="android.test.runner" />


同时在
project.properties
中加入对Library工程的引用:
# Project target.
target=android-8
android.library.reference.1=..


参考:

Android SDK document - library project

Build changes in revision 14

Changes to Library Projects in Android SDK Tools, r14

Android Tools Roadmap

原文地址:http://blog.chenming.info/blog/2013/01/04/android-library-jar/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: