Skip to content

Commit

Permalink
Reflect to getAndroidTest() in ApplicationVariant and LibraryVariant
Browse files Browse the repository at this point in the history
Reflect into the property getter since the return type is different between earlier betas and RC but we are only interested that they return a com.android.build.api.variant.Component implementation.

This change also adds a few deprecation suppressions to old variant APIs and old transform APIs usages, these are deprecated but we keep using them for backwards compatibility reasons.

Fixes #2762

RELNOTES=Fix an incompatibility issue with AGP 7.0.0-beta05 and 7.0.0-rc01
PiperOrigin-RevId: 387098587
  • Loading branch information
danysantiago authored and Dagger Team committed Jul 27, 2021
1 parent 666df50 commit 09087db
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
agp: ['4.1.0', '4.2.0', '7.0.0-beta04']
agp: ['4.1.0', '4.2.0', '7.0.0-rc01']
steps:
- name: 'Check out repository'
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion java/dagger/hilt/android/plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

buildscript {
ext {
agp_version = System.getenv('AGP_VERSION') ?: "7.0.0-beta04"
agp_version = System.getenv('AGP_VERSION') ?: "7.0.0-rc01"
}
repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

@file:Suppress("DEPRECATION") // com.android.build.api.transform is deprecated

package dagger.hilt.android.plugin

import com.android.build.api.transform.DirectoryInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.TestExtension
import com.android.build.gradle.TestedExtension
import com.android.build.gradle.api.AndroidBasePlugin
import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.api.TestVariant
import com.android.build.gradle.api.UnitTestVariant
import dagger.hilt.android.plugin.task.AggregateDepsTask
import dagger.hilt.android.plugin.task.HiltTransformTestClassesTask
import dagger.hilt.android.plugin.util.AggregatedPackagesTransform
Expand Down Expand Up @@ -124,7 +121,9 @@ class HiltGradlePlugin @Inject constructor(

// Invokes the [block] function for each Android variant that is considered a Hilt root, where
// dependencies are aggregated and components are generated.
private fun BaseExtension.forEachRootVariant(block: (variant: BaseVariant) -> Unit) {
private fun BaseExtension.forEachRootVariant(
@Suppress("DEPRECATION") block: (variant: com.android.build.gradle.api.BaseVariant) -> Unit
) {
when (this) {
is AppExtension -> {
// For an app project we configure the app variant and both androidTest and unitTest
Expand All @@ -150,7 +149,7 @@ class HiltGradlePlugin @Inject constructor(
project: Project,
hiltExtension: HiltExtension,
androidExtension: BaseExtension,
variant: BaseVariant
@Suppress("DEPRECATION") variant: com.android.build.gradle.api.BaseVariant
) {
if (
!hiltExtension.enableExperimentalClasspathAggregation || hiltExtension.enableAggregatingTask
Expand Down Expand Up @@ -192,7 +191,8 @@ class HiltGradlePlugin @Inject constructor(
return
}

val runtimeConfiguration = if (variant is TestVariant) {
@Suppress("DEPRECATION") // Older variant API is deprecated
val runtimeConfiguration = if (variant is com.android.build.gradle.api.TestVariant) {
// For Android test variants, the tested runtime classpath is used since the test app has
// tested dependencies removed.
variant.testedVariant.runtimeConfiguration
Expand All @@ -219,10 +219,11 @@ class HiltGradlePlugin @Inject constructor(
// debugUnitTest -> testDebugCompileOnly
// release -> releaseCompileOnly
// releaseUnitTest -> testReleaseCompileOnly
@Suppress("DEPRECATION") // Older variant API is deprecated
val compileOnlyConfigName = when (variant) {
is TestVariant ->
is com.android.build.gradle.api.TestVariant ->
"androidTest${variant.name.substringBeforeLast("AndroidTest").capitalize()}CompileOnly"
is UnitTestVariant ->
is com.android.build.gradle.api.UnitTestVariant ->
"test${variant.name.substringBeforeLast("UnitTest").capitalize()}CompileOnly"
else ->
"${variant.name}CompileOnly"
Expand Down Expand Up @@ -284,7 +285,7 @@ class HiltGradlePlugin @Inject constructor(
project: Project,
hiltExtension: HiltExtension,
androidExtension: BaseExtension,
variant: BaseVariant
@Suppress("DEPRECATION") variant: com.android.build.gradle.api.BaseVariant
) {
if (!hiltExtension.enableAggregatingTask) {
// Option is not enabled, don't configure aggregating task.
Expand All @@ -295,7 +296,8 @@ class HiltGradlePlugin @Inject constructor(
"hiltCompileOnly${variant.name.capitalize()}"
).apply {
// The runtime config of the test APK differs from the tested one.
if (variant is TestVariant) {
@Suppress("DEPRECATION") // Older variant API is deprecated
if (variant is com.android.build.gradle.api.TestVariant) {
extendsFrom(variant.testedVariant.runtimeConfiguration)
}
extendsFrom(variant.runtimeConfiguration)
Expand Down Expand Up @@ -350,7 +352,11 @@ class HiltGradlePlugin @Inject constructor(
it.outputDir.set(
project.file(project.buildDir.resolve("generated/hilt/component_trees/${variant.name}/"))
)
it.testEnvironment.set(variant is TestVariant || variant is UnitTestVariant)
@Suppress("DEPRECATION") // Older variant API is deprecated
it.testEnvironment.set(
variant is com.android.build.gradle.api.TestVariant ||
variant is com.android.build.gradle.api.UnitTestVariant
)
it.crossCompilationRootValidationDisabled.set(
hiltExtension.disableCrossCompilationRootValidation
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package dagger.hilt.android.plugin.task

import com.android.build.gradle.api.UnitTestVariant
import dagger.hilt.android.plugin.AndroidEntryPointClassTransformer
import dagger.hilt.android.plugin.HiltExtension
import dagger.hilt.android.plugin.util.getCompileKotlin
Expand Down Expand Up @@ -114,7 +113,7 @@ abstract class HiltTransformTestClassesTask @Inject constructor(

fun create(
project: Project,
unitTestVariant: UnitTestVariant,
@Suppress("DEPRECATION") unitTestVariant: com.android.build.gradle.api.UnitTestVariant,
extension: HiltExtension
) {
if (!extension.enableTransformForLocalTests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,17 @@ sealed class AndroidComponentsExtensionCompat {
) : AndroidComponentsExtensionCompat() {
override fun onAllVariants(block: (ComponentCompat) -> Unit) {
actual.onVariants { variant ->
// Use reflection to get the AndroidTest component out of the variant because a binary
// incompatible change was introduced in AGP 7.0-beta05 that changed the return type of the
// method.
fun ApplicationVariant.getAndroidTest() =
this::class.java.getDeclaredMethod("getAndroidTest").invoke(this) as? Component
fun LibraryVariant.getAndroidTest() =
this::class.java.getDeclaredMethod("getAndroidTest").invoke(this) as? Component
block.invoke(ComponentCompat.Api70Impl(variant))
when (variant) {
is ApplicationVariant -> variant.androidTest
is LibraryVariant -> variant.androidTest
is ApplicationVariant -> variant.getAndroidTest()
is LibraryVariant -> variant.getAndroidTest()
else -> null
}?.let { block.invoke(ComponentCompat.Api70Impl(it)) }
variant.unitTest?.let { block.invoke(ComponentCompat.Api70Impl(it)) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dagger.hilt.android.plugin.util

import com.android.build.gradle.api.BaseVariant
import org.gradle.api.Project
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
Expand All @@ -9,7 +8,9 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
* Gets [KotlinCompile] task of an Android variant.
*/
@Suppress("UNCHECKED_CAST")
internal fun getCompileKotlin(variant: BaseVariant, project: Project) =
project.tasks.named(
"compile${variant.name.capitalize()}Kotlin"
) as TaskProvider<KotlinCompile>
internal fun getCompileKotlin(
@Suppress("DEPRECATION") variant: com.android.build.gradle.api.BaseVariant,
project: Project
) = project.tasks.named(
"compile${variant.name.capitalize()}Kotlin"
) as TaskProvider<KotlinCompile>
3 changes: 2 additions & 1 deletion util/run-local-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ pushd examples/maven && mvn compile && popd
util/run-local-gradle-tests.sh
util/run-local-gradle-android-tests.sh "4.1.0"
util/run-local-gradle-android-tests.sh "4.2.0"
util/run-local-gradle-android-tests.sh "7.0.0-beta04"
util/run-local-gradle-android-tests.sh "7.0.0-rc01"


0 comments on commit 09087db

Please sign in to comment.