From 8f3a8ba69afd914a563ecb522a838c08285ae06b Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Tue, 24 Nov 2026 17:39:34 +0530 Subject: [PATCH] ADFA-6110: Apply IDE sub-plugins only to Android application modules The init script applies AndroidIDEGradlePlugin to every subproject of every build, including included builds such as a build-logic java-library. Gradle decorates a plugin class before calling apply(), resolving every type in its method signatures, compiled lambdas included. LogSender, Jdwp, Profiler and QuickBuild all carry AGP types there, so on a project without AGP the build failed with "Could not generate a decorated class ... com/android/build/..." before each sub-plugin's own APP_PLUGIN guard could decline. Gate all four in AndroidIDEGradlePlugin on hasPlugin(APP_PLUGIN). Each sub-plugin already returned early without it, so app modules are unaffected. Loading the classes by name (Class.forName) does not help: decoration happens either way, and the new test fails with it. Tests: a parameterized case per sub-plugin on a java-library in an included build (fails on all four without the gate), and a Profiler case on :app (fails if the gate never opens). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01JULeV2jMSV6tkuLknMpc89 --- docs/PLUGIN_AUTHORING.md | 6 +-- .../gradle/AndroidIDEGradlePlugin.kt | 51 +++++++++++------- .../gradle/AndroidIDEInitScriptPluginTest.kt | 53 +++++++++++++++++++ quickbuild/docs/pipeline.md | 2 +- 4 files changed, 88 insertions(+), 24 deletions(-) diff --git a/docs/PLUGIN_AUTHORING.md b/docs/PLUGIN_AUTHORING.md index 7b5d6892c3..8ee938a651 100644 --- a/docs/PLUGIN_AUTHORING.md +++ b/docs/PLUGIN_AUTHORING.md @@ -94,9 +94,9 @@ plugins { } ``` -CoGo injects `LogSenderPlugin` (via its build init script) into every module; that -plugin references AGP's `ApplicationVariant` and is loaded from the root buildscript -classpath, so a root without AGP fails configuration with +CoGo injects `LogSenderPlugin` (via its build init script) into every Android application +module; that plugin references AGP's `ApplicationVariant` and is loaded from the root +buildscript classpath, so a root without AGP fails configuration with `NoClassDefFoundError: com/android/build/api/variant/ApplicationVariant`. > The in-app plugin-project detector still recognizes a project by a diff --git a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt index 0a880dfeff..c4470350ff 100644 --- a/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt +++ b/gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEGradlePlugin.kt @@ -50,26 +50,13 @@ class AndroidIDEGradlePlugin : Plugin { } target.run { - val isLogSenderEnabled = findProperty(PROPERTY_LOG_SENDER_ENABLED) == "true" - if (isLogSenderEnabled) { - pluginManager.apply(LogSenderPlugin::class.java) - } - - val isJdwpEnabled = findProperty(PROPERTY_JDWP_ENABLED) == "true" - if (isJdwpEnabled) { - pluginManager.apply(JdwpPlugin::class.java) - } - - val isProfileableEnabled = findProperty(PROPERTY_PROFILEABLE_ENABLED) == "true" - if (isProfileableEnabled) { - pluginManager.apply(ProfilerPlugin::class.java) - } - - val isQuickBuildEnabled = findProperty(PROPERTY_QUICK_BUILD_ENABLED) == "true" - if (isQuickBuildEnabled) { - // By name, not ::class: Quick Build classes load (and touch newer AGP APIs) - // only when the property enables them - see QUICK_BUILD_PLUGIN_CLASS. - pluginManager.apply(Class.forName(QUICK_BUILD_PLUGIN_CLASS)) + // Gradle decorates a plugin class before calling apply(), which resolves every type in + // its method signatures (compiled lambdas included). Each sub-plugin carries AGP types + // there, so on a project without AGP (a build-logic java-library in an included build) + // decoration throws before the sub-plugin's own APP_PLUGIN guard can decline + // (ADFA-6110). Check here, before any sub-plugin class is handed to Gradle. + if (plugins.hasPlugin(APP_PLUGIN)) { + applyApplicationPlugins() } tasks.configureEach { task -> @@ -83,4 +70,28 @@ class AndroidIDEGradlePlugin : Plugin { } } } + + private fun Project.applyApplicationPlugins() { + val isLogSenderEnabled = findProperty(PROPERTY_LOG_SENDER_ENABLED) == "true" + if (isLogSenderEnabled) { + pluginManager.apply(LogSenderPlugin::class.java) + } + + val isJdwpEnabled = findProperty(PROPERTY_JDWP_ENABLED) == "true" + if (isJdwpEnabled) { + pluginManager.apply(JdwpPlugin::class.java) + } + + val isProfileableEnabled = findProperty(PROPERTY_PROFILEABLE_ENABLED) == "true" + if (isProfileableEnabled) { + pluginManager.apply(ProfilerPlugin::class.java) + } + + val isQuickBuildEnabled = findProperty(PROPERTY_QUICK_BUILD_ENABLED) == "true" + if (isQuickBuildEnabled) { + // By name, not ::class: Quick Build classes load (and touch newer AGP APIs) + // only when the property enables them - see QUICK_BUILD_PLUGIN_CLASS. + pluginManager.apply(Class.forName(QUICK_BUILD_PLUGIN_CLASS)) + } + } } diff --git a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/AndroidIDEInitScriptPluginTest.kt b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/AndroidIDEInitScriptPluginTest.kt index e84cc36d7a..686480b954 100644 --- a/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/AndroidIDEInitScriptPluginTest.kt +++ b/gradle-plugin/src/test/java/com/itsaky/androidide/gradle/AndroidIDEInitScriptPluginTest.kt @@ -136,6 +136,59 @@ class AndroidIDEInitScriptPluginTest { assertThat(result.output).contains("Build directory of project ':' relocated to $expected") } + /** ADFA-6110: a sub-plugin must not reach a project without AGP, here a build-logic java-library. */ + @ParameterizedTest + @ValueSource( + strings = [ + GradlePluginConfig.PROPERTY_LOG_SENDER_ENABLED, + GradlePluginConfig.PROPERTY_JDWP_ENABLED, + GradlePluginConfig.PROPERTY_PROFILEABLE_ENABLED, + GradlePluginConfig.PROPERTY_QUICK_BUILD_ENABLED, + ], + ) + fun `test sub-plugin leaves a non-Android project in an included build alone`( + enabledProperty: String, + @TempDir dir: File, + ) { + val project = File(dir, "composite").apply { mkdirs() } + File(project, "settings.gradle.kts").writeText("rootProject.name = \"composite\"\nincludeBuild(\"lib\")\n") + File(project, "build.gradle.kts").writeText("") + val lib = File(project, "lib").apply { mkdirs() } + File(lib, "settings.gradle.kts").writeText("rootProject.name = \"lib\"\ninclude(\"common\")\n") + File(lib, "build.gradle.kts").writeText("") + val common = File(lib, "common").apply { mkdirs() } + File(common, "build.gradle.kts").writeText("plugins { `java-library` }\n") + val aar = File(dir, "dummy.aar").apply { writeText("aar") } + + val result = + buildProject( + task = ":lib:common:help", + projectDir = project.toPath(), + configureArgs = { + it += "-P$enabledProperty=true" + // Read only by the sub-plugin they belong to; set both so any one can apply. + it += "-P${GradlePluginConfig.PROPERTY_LOG_SENDER_AAR}=${aar.absolutePath}" + it += "-P${GradlePluginConfig.PROPERTY_QUICK_BUILD_RUNTIME_AAR}=${aar.absolutePath}" + }, + ) + + // Proves the IDE plugin reached the non-Android project, so the build passing means + // the sub-plugin was declined rather than never offered. + assertThat(result.output).contains("Applying AndroidIDEGradlePlugin to project ':common'") + } + + /** The ADFA-6110 gate must still let sub-plugins through on an Android application module. */ + @Test + fun `test sub-plugin still applies to an Android application module`() { + val result = + buildProject(configureArgs = { + it += "-P${GradlePluginConfig.PROPERTY_PROFILEABLE_ENABLED}=true" + it += "--info" + }) + + assertThat(result.output).contains("Applying ProfilerPlugin to project ':app'") + } + @Test fun `test build output stays put by default`() { val result = buildProject(configureArgs = { it += "--info" }) diff --git a/quickbuild/docs/pipeline.md b/quickbuild/docs/pipeline.md index df7c35473e..57c42d0cfe 100644 --- a/quickbuild/docs/pipeline.md +++ b/quickbuild/docs/pipeline.md @@ -172,7 +172,7 @@ flowchart TB classDef ext stroke-dasharray: 6 4,stroke-width:1.5px ``` -- Applied by `AndroidIDEGradlePlugin` when the `PROPERTY_QUICK_BUILD_ENABLED` property is set, and only to **debuggable application variants** (`variant.debuggable`, read in `onVariants`). +- Applied by `AndroidIDEGradlePlugin` when the `PROPERTY_QUICK_BUILD_ENABLED` property is set and the module applies `com.android.application`, and only to **debuggable application variants** (`variant.debuggable`, read in `onVariants`). - Fails the build immediately if `-Pcotg.quickbuild.runtimeAar` is missing or does not point at a file. The runtime AAR is added to the variant's **runtime** configuration only, never the compile classpath. - Compose is detected in `finalizeDsl`, from either `buildFeatures.compose` or the `org.jetbrains.kotlin.plugin.compose` plugin, and reported to the daemon through `setup.json`.