Android的Library工程

为什么要使用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 SDKTools.

针对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=.. 

参考:

原文:http://blog.chenming.info/blog/2013/01/04/android-library-jar/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值