android - 将大型 Android 项目分割成 Android 库会缩短构建时间吗?

标签 android android-library

我有一个相当大的 Android 项目,sdk 需要花费大量时间来进行资源解析/dexing 等。我想以某种方式改进它。

我了解到 Android 库项目现在也可以包含资源。所以我们也可以在里面放Activity、Fragments等。

这是否意味着,如果我将我的大型项目的一部分导出到我从主项目引用的库项目中,那么当我重建主要项目?所以我只需为修改后的库和可能的主项目做资源解析/索引等,在大多数情况下减少了总体构建时间。

最佳答案

Does this mean, that if I export parts of my large project into library projects which I reference from the main project, then I don't have to rebuild the already built (and not modified) libraries again, when I rebuild the main project?

部分没有,Android Library Project不是直接构建的,它总是和依赖的Main Project一起构建,当SDK编译/构建Main Project时,SDK工具将Library Project编译成一个临时的JAR文件并在主要项目。每当您重新构建主项目时,引用的库项目都会作为主项目构建生命周期的一部分重新构建,即使库项目中没有任何更改。检查在您的 app-lib/bin 文件夹下生成的临时 JAR 的时间戳以获取证据,每次构建主项目时它总是会更改。

引自official dev guide :

However, a library project differs from an standard Android application project in that you cannot compile it directly to its own .apk and run it on an Android device. Similarly, you cannot export the library project to a self-contained JAR file, as you would do for a true library. Instead, you must compile the library indirectly, by referencing the library in the dependent application and building that application.

When you build an application that depends on a library project, the SDK tools compile the library into a temporary JAR file and uses it in the main project, then uses the result to generate the .apk. In cases where a resource ID is defined in both the application and the library, the tools ensure that the resource declared in the application gets priority and that the resource in the library project is not compiled into the application .apk. This gives your application the flexibility to either use or redefine any resource behaviors or values that are defined in any library.

Android 库项目不同于常规的 java 库项目。您可以在其中一次将所有内容编译并构建到 jar 库中,然后开始从主项目中的引用 jar 依赖项导入/使用该类。目前 Android Library Project 是基于源代码机制设计的,而不是基于编译代码的库机制,如 this Android blog 中所述。 , 虽然在未来的版本中 promise 了独立的 jar 分发(不幸的是,r15、r16、r17 和 r18 都没有)。

关于android - 将大型 Android 项目分割成 Android 库会缩短构建时间吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10153425/

相关文章:

Android - 通过wifi将数据从应用程序发送到电脑

Android - 图层列表椭圆形有额外的内线

java - 从/向路径读取和写入 CSV

Android 反调试失败与 android 库(绝对路径)

android - Kotlin 多平台 : Plugin [id: 'com.android.library' ] was not found

android - 在android中添加有和没有资源的库

android - 在 Robolectric 测试中覆盖 Dagger 模块

android - 自定义线性布局,在构造函数上获取所有 subview

java - 无法解析项目 : Android library and Java library module dependency

机器人 : how to start activity defined in library project