在我的项目中,我想生成一个包含动态特征信息的类。动态特征是这样添加的。
// In the base module’s build.gradle file.
android {
...
// Specifies dynamic feature modules that have a dependency on
// this base module.
dynamicFeatures = [":dynamic_feature", ":dynamic_feature2"]
}
Source: https:/developer.android.comguideapp-bundleat-install-delivery#base_feature_relationship。
我从几天前就开始寻找解决方案,但没找到什么。目前,我的插件是这样的。
class MyPlugin : Plugin<Project> {
override fun apply(project: Project) {
if (project == rootProject) {
throw Exception("This plugin cannot be applied to root project")
}
val parent = project.parent ?: throw Exception("Parent of project cannot be null")
val extension = project.extensions.getByName("android") as BaseAppModuleExtension?
?: throw Exception("Android extension cannot be null")
extension.dynamicFeatures
}
}
不幸的是,extension.dynamicFeatures是空的,即使我的插件被应用到有动态特性的build.gradle文件中。
它是空的,因为你试图在gradle生命周期配置阶段获得扩展值,所有gradle属性还没有配置。
使用 afterEvaluate
关闭。在本块中 dynamicFeatures
已经配置好了,而且不是空的。
project.afterEvaluate {
val extension = project.extensions.getByType(BaseAppModuleExtension::class.java)
?: throw Exception("Android extension cannot be null")
extension.dynamicFeatures
}