forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove usage of unstable and removed Android Gradle Plugin APIs.
The AndroidComponentsExtension's methods androidTests() and unitTests() are removed from AGP 7.0, migrate away from their usages by using the new variant.androidTest and variant.unitTest methods. To support backward compatibility for users with AGP 4.2 reflect into the old and removed methods. Furthermore to protect against unstable API changes this change adds a postsubmit Github Action to compile the Hilt Gradle plugin against the latest released Android Gradle Plugin. This combined with turning ON all warnings as error will help such that if an API is marked as @deprecated, then the build will fail. Due to all warnings as error being ON a few additional suppressed are added, all previous ones are commented and some trivial API usages from AGP were removed. In essence only APIs from the 'com.android.build.api' package are safe to use. Fixes: #2618 RELNOTES=Fix compatibility issue with the Hilt Gradle Plugin and Android Gradle Plugin 7.0 PiperOrigin-RevId: 374291978
- Loading branch information
1 parent
133e710
commit 371a2c3
Showing
13 changed files
with
189 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...ilt/android/plugin/src/main/kotlin/dagger/hilt/android/plugin/util/AndroidGradleCompat.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (C) 2021 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dagger.hilt.android.plugin.util | ||
|
||
import com.android.build.api.AndroidPluginVersion | ||
import com.android.build.api.component.AndroidTest | ||
import com.android.build.api.component.UnitTest | ||
import com.android.build.api.extension.AndroidComponentsExtension | ||
import com.android.build.api.extension.VariantSelector | ||
import com.android.build.api.variant.ApplicationVariant | ||
import com.android.build.api.variant.LibraryVariant | ||
|
||
/** | ||
* Compatibility method to go over each Variant as Component (the newer | ||
* 'android.build.api.variant.Variant' version, not the older one | ||
* 'com.android.build.gradle.api.BaseVariant') | ||
*/ | ||
@Suppress("UnstableApiUsage") | ||
fun AndroidComponentsExtension<*, *, *>.allTestVariants( | ||
onAndroidTest: (AndroidTest) -> Unit, | ||
onUnitTest: (UnitTest) -> Unit | ||
) { | ||
if ( | ||
findClass("com.android.build.api.AndroidPluginVersion") != null && | ||
this.pluginVersion >= AndroidPluginVersion(7, 0).beta(1) | ||
) { | ||
this.onVariants { variant -> | ||
when (variant) { | ||
is ApplicationVariant -> variant.androidTest | ||
is LibraryVariant -> variant.androidTest | ||
else -> null | ||
}?.let { onAndroidTest(it) } | ||
variant.unitTest?.let { onUnitTest(it) } | ||
} | ||
} else { | ||
// This methods were removed in 7.0.0-beta01 but are available in 4.2.0 | ||
AndroidComponentsExtension::class.java.getDeclaredMethod( | ||
"androidTests", VariantSelector::class.java, Function1::class.java | ||
).invoke(this, this.selector().all(), onAndroidTest) | ||
AndroidComponentsExtension::class.java.getDeclaredMethod( | ||
"unitTests", VariantSelector::class.java, Function1::class.java | ||
).invoke(this, this.selector().all(), onUnitTest) | ||
} | ||
} | ||
|
||
fun findClass(fqName: String) = try { | ||
Class.forName(fqName) | ||
} catch (ex: ClassNotFoundException) { | ||
null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/bin/bash | ||
|
||
set -eu | ||
|
||
readonly MVN_GOAL="$1" | ||
readonly VERSION_NAME="$2" | ||
shift 2 | ||
readonly EXTRA_MAVEN_ARGS=("$@") | ||
|
||
# Builds and deploy the Gradle plugin. | ||
_deploy_plugin() { | ||
local plugindir=java/dagger/hilt/android/plugin | ||
./$plugindir/gradlew -p $plugindir --no-daemon clean \ | ||
publishAllPublicationsToMavenRepository -PPublishVersion="$VERSION_NAME" | ||
local outdir=$plugindir/build/repo/com/google/dagger/hilt-android-gradle-plugin/$VERSION_NAME | ||
# When building '-SNAPSHOT' versions in gradle, the filenames replaces | ||
# '-SNAPSHOT' with timestamps, so we need to disambiguate by finding each file | ||
# to deploy. See: https://stackoverflow.com/questions/54182823/ | ||
local suffix | ||
if [[ "$VERSION_NAME" == *"-SNAPSHOT" ]]; then | ||
# Gets the timestamp part out of the name to be used as suffix. | ||
# Timestamp format is ########.######-#. | ||
suffix=$(find $outdir -name "*.pom" | grep -Eo '[0-9]{8}\.[0-9]{6}-[0-9]{1}') | ||
else | ||
suffix=$VERSION_NAME | ||
fi | ||
mvn "$MVN_GOAL" \ | ||
-Dfile="$(find $outdir -name "*-$suffix.jar")" \ | ||
-DpomFile="$(find $outdir -name "*-$suffix.pom")" \ | ||
-Dsources="$(find $outdir -name "*-$suffix-sources.jar")" \ | ||
-Djavadoc="$(find $outdir -name "*-$suffix-javadoc.jar")" \ | ||
"${EXTRA_MAVEN_ARGS[@]:+${EXTRA_MAVEN_ARGS[@]}}" | ||
} | ||
|
||
# Gradle Plugin is built with Gradle, but still deployed via Maven (mvn) | ||
_deploy_plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters