Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 2775702

Browse files
committed
fix(gradle-plugin): fix build failure when 'apply plugin: ..' syntax is used in module projects (#1274, #1620)
1 parent 0367c63 commit 2775702

File tree

7 files changed

+127
-80
lines changed

7 files changed

+127
-80
lines changed

gradle-plugin/src/main/java/com/itsaky/androidide/gradle/AndroidIDEInitScriptPlugin.kt

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package com.itsaky.androidide.gradle
2020
import com.itsaky.androidide.buildinfo.BuildInfo
2121
import com.itsaky.androidide.tooling.api.LogSenderConfig._PROPERTY_IS_TEST_ENV
2222
import com.itsaky.androidide.tooling.api.LogSenderConfig._PROPERTY_MAVEN_LOCAL_REPOSITORY
23+
import org.gradle.StartParameter
2324
import org.gradle.api.Plugin
2425
import org.gradle.api.artifacts.dsl.RepositoryHandler
2526
import org.gradle.api.initialization.Settings
@@ -43,14 +44,17 @@ class AndroidIDEInitScriptPlugin : Plugin<Gradle> {
4344

4445
override fun apply(target: Gradle) {
4546
target.settingsEvaluated { settings ->
46-
val (isTestEnv, mavenLocalRepos) = settings.startParameter.run {
47-
val isTestEnv = projectProperties.containsKey(_PROPERTY_IS_TEST_ENV)
48-
&& projectProperties[_PROPERTY_IS_TEST_ENV].toString().toBoolean()
49-
val mavenLocalRepos = projectProperties.getOrDefault(_PROPERTY_MAVEN_LOCAL_REPOSITORY, "")
50-
isTestEnv to mavenLocalRepos
51-
}
47+
settings.addDependencyRepositories()
48+
}
5249

53-
settings.addDependencyRepositories(isTestEnv, mavenLocalRepos)
50+
target.rootProject { rootProject ->
51+
rootProject.buildscript.apply {
52+
dependencies.apply {
53+
add("classpath", rootProject.ideDependency("gradle-plugin"))
54+
}
55+
56+
repositories.addDependencyRepositories(rootProject.gradle.startParameter)
57+
}
5458
}
5559

5660
target.projectsLoaded { gradle ->
@@ -62,10 +66,6 @@ class AndroidIDEInitScriptPlugin : Plugin<Gradle> {
6266
return@subprojects
6367
}
6468

65-
sub.buildscript.dependencies.apply {
66-
add("classpath", sub.ideDependency("gradle-plugin"))
67-
}
68-
6969
sub.afterEvaluate {
7070
logger.info("Trying to apply plugin '${BuildInfo.PACKAGE_NAME}' to project '${sub.path}'")
7171
sub.pluginManager.apply(BuildInfo.PACKAGE_NAME)
@@ -74,9 +74,16 @@ class AndroidIDEInitScriptPlugin : Plugin<Gradle> {
7474
}
7575
}
7676

77+
private fun Settings.addDependencyRepositories() {
78+
val (isTestEnv, mavenLocalRepos) = getTestEnvProps(startParameter)
79+
addDependencyRepositories(isTestEnv, mavenLocalRepos)
80+
}
81+
7782
@Suppress("UnstableApiUsage")
78-
private fun Settings.addDependencyRepositories(isMavenLocalEnabled: Boolean,
79-
mavenLocalRepo: String) {
83+
private fun Settings.addDependencyRepositories(
84+
isMavenLocalEnabled: Boolean,
85+
mavenLocalRepo: String
86+
) {
8087
dependencyResolutionManagement.run {
8188
repositories.configureRepositories(isMavenLocalEnabled, mavenLocalRepo)
8289
}
@@ -86,6 +93,21 @@ class AndroidIDEInitScriptPlugin : Plugin<Gradle> {
8693
}
8794
}
8895

96+
private fun RepositoryHandler.addDependencyRepositories(startParams: StartParameter) {
97+
val (isTestEnv, mavenLocalRepos) = getTestEnvProps(startParams)
98+
configureRepositories(isTestEnv, mavenLocalRepos)
99+
}
100+
101+
private fun getTestEnvProps(startParameter: StartParameter): Pair<Boolean, String> {
102+
return startParameter.run {
103+
val isTestEnv = projectProperties.containsKey(_PROPERTY_IS_TEST_ENV)
104+
&& projectProperties[_PROPERTY_IS_TEST_ENV].toString().toBoolean()
105+
val mavenLocalRepos = projectProperties.getOrDefault(_PROPERTY_MAVEN_LOCAL_REPOSITORY, "")
106+
107+
isTestEnv to mavenLocalRepos
108+
}
109+
}
110+
89111
private fun RepositoryHandler.configureRepositories(
90112
isMavenLocalEnabled: Boolean,
91113
mavenLocalRepos: String

gradle-plugin/src/main/java/com/itsaky/androidide/gradle/LogSenderPlugin.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.itsaky.androidide.gradle
1919

20+
import com.android.build.api.component.analytics.AnalyticsEnabledApplicationVariant
2021
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
2122
import com.android.build.api.variant.ApplicationVariant
2223
import com.android.build.api.variant.impl.ApplicationVariantImpl
@@ -107,7 +108,10 @@ class LogSenderPlugin : Plugin<Project> {
107108
private fun ApplicationVariant.withRuntimeConfiguration(
108109
action: Configuration.() -> Unit
109110
) {
110-
this as ApplicationVariantImpl
111-
variantDependencies.runtimeClasspath.action()
111+
if (this is ApplicationVariantImpl) {
112+
variantDependencies.runtimeClasspath.action()
113+
} else if (this is AnalyticsEnabledApplicationVariant) {
114+
delegate.withRuntimeConfiguration(action)
115+
}
112116
}
113117
}

gradle-plugin/src/test/java/com/itsaky/androidide/gradle/AndroidIDEInitScriptPluginTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ class AndroidIDEInitScriptPluginTest {
3939
assertBasics(result)
4040
}
4141

42+
@Test
43+
fun `test behavior with apply plugin syntax`() {
44+
val result = buildProject(
45+
agpVersion = BuildInfo.AGP_VERSION_MININUM,
46+
gradleVersion = "7.5.1",
47+
useApplyPluginGroovySyntax = true
48+
)
49+
assertBasics(result)
50+
}
51+
4252
private fun assertBasics(result: BuildResult) {
4353
// These plugins must be applied to the
4454
for ((project, plugins) in mapOf(

gradle-plugin/src/test/java/com/itsaky/androidide/gradle/utils.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ import kotlin.io.path.pathString
3030
internal fun buildProject(
3131
agpVersion: String = BuildInfo.AGP_VERSION_LATEST,
3232
gradleVersion: String = "8.2",
33+
useApplyPluginGroovySyntax: Boolean = false,
3334
configureArgs: (MutableList<String>) -> Unit = {},
3435
vararg plugins: String
3536
): BuildResult {
36-
val projectRoot = openProject(agpVersion, *plugins)
37+
val projectRoot = openProject(agpVersion, useApplyPluginGroovySyntax, *plugins)
3738
val initScript = FileProvider.testHomeDir().resolve(".androidide/init/androidide.init.gradle")
3839
val mavenLocal = FileProvider.projectRoot().resolve("gradle-plugin/build/maven-local/repos.txt").toFile()
3940

@@ -99,6 +100,7 @@ internal fun writeInitScript(file: File, deps: List<File>) {
99100

100101
internal fun openProject(
101102
agpVersion: String = BuildInfo.AGP_VERSION_LATEST,
103+
useApplyPluginGroovySyntax: Boolean = false,
102104
vararg plugins: String
103105
): Path {
104106
val projectRoot = FileProvider.projectRoot()
@@ -110,8 +112,20 @@ internal fun openProject(
110112
}
111113

112114
run {
113-
val pluginsText = plugins.joinToString(separator = "\n") { "id(\"$it\")" }
114-
projectRoot.resolve("app/build.gradle.kts").toFile()
115+
// remove existing build scripts
116+
projectRoot.resolve("app")
117+
.toFile()
118+
.listFiles()!!
119+
.filter { it.name.startsWith("build.gradle") && !it.name.endsWith(".in") }
120+
.forEach { it.delete() }
121+
122+
val pluginsText = if (!useApplyPluginGroovySyntax) {
123+
plugins.joinToString(separator = "\n") { "id(\"$it\")" }
124+
} else {
125+
plugins.joinToString(separator = "\n") { "apply plugin: \"$it\"" }
126+
}
127+
128+
projectRoot.resolve("app/build.gradle" + if (useApplyPluginGroovySyntax) "" else ".kts").toFile()
115129
.replaceAllPlaceholders(mapOf("PLUGINS" to pluginsText))
116130
}
117131

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/build
2+
build.gradle
23
build.gradle.kts
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
apply plugin: 'com.android.application'
2+
@@PLUGINS@@
3+
4+
android {
5+
namespace "com.example.myapplication2"
6+
compileSdk 33
7+
buildToolsVersion "33.0.2"
8+
9+
defaultConfig {
10+
applicationId "com.example.myapplication2"
11+
minSdk 21
12+
targetSdk 33
13+
versionCode 1
14+
versionName "1.0"
15+
16+
vectorDrawables {
17+
useSupportLibrary true
18+
}
19+
}
20+
21+
compileOptions {
22+
sourceCompatibility JavaVersion.VERSION_11
23+
targetCompatibility JavaVersion.VERSION_11
24+
}
25+
26+
buildTypes {
27+
release {
28+
minifyEnabled true
29+
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
30+
}
31+
}
32+
33+
buildFeatures {
34+
viewBinding true
35+
}
36+
37+
// LogSender must be applied to all the debuggable product flavors
38+
flavorDimensions += "version"
39+
productFlavors {
40+
demo {
41+
dimension "version"
42+
applicationIdSuffix ".demo"
43+
versionNameSuffix "-demo"
44+
}
45+
46+
full {
47+
dimension "version"
48+
applicationIdSuffix ".full"
49+
versionNameSuffix "-full"
50+
}
51+
}
52+
}
53+
54+
dependencies {
55+
implementation "androidx.appcompat:appcompat:1.6.1"
56+
implementation "androidx.constraintlayout:constraintlayout:2.1.4"
57+
implementation "com.google.android.material:material:1.9.0"
58+
}

gradle-plugin/src/test/resources/sample-project/app/build.gradle.kts

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)