Skip to content

[兼容性反馈] AGP 9.0+ / Gradle 9.1 环境下插件无法应用,提示 StringFog plugin must be used with android plugin #178

@Sohoad

Description

@Sohoad

[兼容性反馈] AGP 9.0+ / Gradle 9.1 环境下插件无法应用,提示 StringFog plugin must be used with android plugin

问题描述

在将项目升级至 Android Gradle Plugin 9.0.0 (Alpha/Beta) 以及 Gradle 9.1 环境后,StringFog 插件(v5.2.0)无法正常工作。即使在 app/build.gradle 中严格按照顺序应用了 com.android.application,StringFog 依然会报错提示找不到 Android 插件。

这似乎是因为从 Gradle 9.x 开始,插件加载逻辑和隔离机制发生了变化,导致 StringFog 内部的插件存在性检查(project.plugins.hasPlugin)无法在正确的生命周期内获取到 Android 插件状态。

环境信息

  • StringFog 版本: 5.2.0
  • AGP (Android Gradle Plugin) 版本: 9.x
  • Gradle 版本: 9.1
  • JDK 版本: 17 或 21

复现步骤

  1. settings.gradle 中通过 pluginManagement 引入 StringFog 插件。
  2. app/build.gradleplugins 块中应用插件:
plugins {
    id 'com.android.application'
    id 'stringfog'
}
  1. 执行 Gradle Sync。

错误日志

Caused by: org.gradle.api.GradleException: StringFog plugin must be used with android plugin
	at com.github.megatronking.stringfog.plugin.StringFogPlugin.apply(StringFogPlugin.kt:42)
	at com.github.megatronking.stringfog.plugin.StringFogPlugin.apply(StringFogPlugin.kt:18)
	at org.gradle.api.internal.plugins.ImperativeOnlyPluginTarget.applyImperative(ImperativeOnlyPluginTarget.java:55)
    ...

已尝试的解决办法

为了绕过该问题,我尝试了以下几种方案,但均以失败告终:

  1. 使用 project.pluginManager.withPlugin('com.android.application') 延迟应用 StringFog。
  2. 将配置放入 afterEvaluate { ... } 闭包中。
  3. 使用传统的 buildscript 加载 classpath 并通过 apply plugin: 'stringfog' 应用。

以上尝试依然触发相同的 GradleException,说明该检查在插件 apply 的最早期阶段就拦截了后续流程。


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions