在我的项目中,我想生成一个包含有关动态功能信息的类。通过以下方式添加动态功能:
// 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"]
}
资料来源:https://developer.android.com/guide/app-bundle/at-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
}
}
不幸的是,即使我的插件应用于具有动态功能的build.gradle文件,extension.dynamicFeatures也为空。
最佳答案
它是空的,因为您正在尝试在gradle生命周期配置阶段获取扩展值,因此尚未配置所有gradle属性。
使用afterEvaluate
闭包。在此块中,dynamicFeatures
已经配置并且不为空。
project.afterEvaluate {
val extension = project.extensions.getByType(BaseAppModuleExtension::class.java)
?: throw Exception("Android extension cannot be null")
extension.dynamicFeatures
}
关于android - 如何从buildSrc中的自定义Gradle插件访问Android的 “dynamicFeatures”属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61387764/