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 projectBuild 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/
相关文章推荐
- android 加载图片oom的好的解决方法
- Android 中使用自定义字体的方法
- mono for Android 问题集合
- android 4.4 强制所有应用横屏显示
- [Android Studio] 取消引用库打包出现异常-- provided dependencies can only be jars
- android调用,暂停或停止系统自带音乐播放器
- android dialog
- 将Eclipse代码导入到AndroidStudio的两种方式
- Android 编程下 Touch 事件的分发和消费机制
- android activity切换效果实现
- [Flash 3D] 又是一个难题解决了。(Flash3D在android中运行)
- Android项目结构
- Android RecyclerView的基本使用
- Android SDK 国内镜像
- Android在包名称更改项目
- 8 个最优秀的 Android Studio 插件
- android:scaleType="fitXY" 详细说明
- Android ActionBar 返回上一个Activity
- Android 开源框架Universal-Image-Loader完全解析
- android只让某个activity运行一次-SharedPreferences