Dealing with dependencies in Android(安卓)projects
In revision 17 of the Android SDK Tools and of the Eclipse ADT plug-in, we have made a lot of improvements to the dependency management of Android projects.
The first thing we changed was to align both the Ant-based build system and the Eclipse plug-in so that they behave the same.
Projects have source folders, as well as Library Project and jar file dependencies. With no other setup needed than adding Library Projects as a dependency inproject.properties, a project’s classpath is automatically populated with:
- The content of the project’slibs/*.jar
- The output of the Library Projects.
- The Library Projects’libs/*.jar
These items, plus the output of the compilation of the project’s own source code, are sent to dex for bytecode conversion and inclusion in the final APK.
Because a project could depend on several libraries using the same jar files, the build system now looks at all the required jar files, detects duplicates coming from different libraries and removes them. This will prevent the dreaded “already added” error from dx.
See below for how duplicates are found.
Important change:We have changed the way Library Projects generate and package R classes:
- The R class is not packaged in the jar output of Library Projects anymore.
- Library Project do not generate the R class for Library Projects they depend on. Only main application projects generates the Library R classes alongside their own.
This means that library projects cannot import the R class from another library project they depend on. This is not necessary anyway, as their own R class includes all the necessary resources.
Note that app projects can still import the R classes from referenced Library Projects, but again, this is not needed as their own R classes include all the resources.
Eclipse specific changes
The dynamic classpath container called “Library Projects” has been renamed to “Android Dependencies” as it now contains more than just Library Projects.
The container will now also be populated with Java-only projects that are referenced by Library Projects. If those Java projects also reference other Java projects and/or jar files they will be added automatically (jar files referenced through user libraries are supported as well).
Important:this only happens if the references are set to be exported in the referencing project. Note that this is not the default when adding a project or jar file to a project build path.
Library Projects (and the content of theirlibs/*.jarfiles) is always exported.
- If the project is a Library project, these jar libraries will not be automatically visible to application projects. You should really move these tolibs/
- If the project is an application, this can work but you must make sure to mark the jar files as exported.
Dependency resolution
When a project references two Library projects that both require the same jar file, the build system has to detect and resolve the duplication.A full dependency system would associate each jar file with a fully qualified name and a version number to figure out which version to use.
Unfortunately the Android build system does not have a full dependency resolution system (yet). In the meantime we have implemented a very basic system that follows these rules:
Jar file are identified strictly by their file names.
This meansmylib.jaris different thanmylib-v2.jarand both will be packaged, possibly resulting in “already added” dx error if they are actually the same library in a different revision.
For jars with the same file name, “same version” means same exact file.
Currently our detection is very basic, checking only that the files are identical in size and sha1. If two libraries each include in their libs folder a file calledmylib.jarbut these two files are different, then the build system will fail indicating a dependency error. The solution is to make sure the two jar files are actually the same one if they are the same library or to rename them if they are different libraries.
Special case for android-support-v4.jar and android-support-v13.jar.
We make a special case for these two libraries because-v13contains a full version of-v4inside.If both are found, then only-v13will be used. Note that we can’t guarantee that the version of-v4inside-v13is the same as version used by the other libraries. We recommend that when you update your project with a newer version of the support library, you update all of your projects at the same time, whether they use-v4or-v13. 原文地址为:http://tools.android.com/recent/dealingwithdependenciesinandroidprojects
更多相关文章
- android通过读取系统属性设置字体缩放的默认值
- Android获取设备型号和系统版本号、mac地址、IP、屏幕的分辨率
- Android(安卓)SDK Android(安卓)NDK Android(安卓)Studio 官方下
- android 读取mac地址
- 使用 Android(安卓)和 XML 构建动态用户界面
- Android_linux下android platforms下载地址
- Android(安卓)Studio 3.6.3 中遇到无法创建FileHasher类型的服务
- 从android 里面读取配置文件
- How to Create QuickAction Dialog in Android