0

在我的项目中,我想生成一个包含有关我的动态功能信息的类。动态功能以这种方式添加:

// 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 也是空的。

4

1 回答 1

0

它是空的,因为您试图在 gradle 生命周期配置阶段获取扩展值,所有 gradle 属性尚未配置。

使用afterEvaluate闭包。在此块dynamicFeatures中已配置且不为空。

project.afterEvaluate {
    val extension = project.extensions.getByType(BaseAppModuleExtension::class.java)
        ?: throw Exception("Android extension cannot be null")
    extension.dynamicFeatures
}
于 2020-04-23T23:21:50.173 回答