Skip to content

Commit 35d2d8a

Browse files
committed
Support AGP 3.6+ in the iid compatibility check.
AGP 3.6 introduced a breaking change to lint-api making the check fail. The change switch to use reflection to support both versions of the api. Additionally the change makes the check failures more developer friendly to make sure this check does not cause failures in upcoming AGP versions. Versions tested: 3.2 through 4.1.0-alpha04 Fixes #1407
1 parent 7d0a217 commit 35d2d8a

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

firebase-installations/customer-lint-checks/src/main/java/com/google/firebase/installations/lint/IncompatibleIidVersionDetector.kt

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
package com.google.firebase.installations.lint
1616

1717
import com.android.builder.model.MavenCoordinates
18+
import com.android.builder.model.Variant
1819
import com.android.tools.lint.detector.api.Category
1920
import com.android.tools.lint.detector.api.Context
2021
import com.android.tools.lint.detector.api.Detector
2122
import com.android.tools.lint.detector.api.Implementation
2223
import com.android.tools.lint.detector.api.Issue
2324
import com.android.tools.lint.detector.api.Location
25+
import com.android.tools.lint.detector.api.Project
2426
import com.android.tools.lint.detector.api.Severity
2527
import com.android.tools.lint.detector.api.Scope
2628
import java.util.EnumSet
@@ -46,14 +48,25 @@ class IncompatibleIidVersionDetector : Detector() {
4648
severity = Severity.ERROR,
4749
implementation = IMPLEMENTATION
4850
)
49-
}
5051

51-
override fun beforeCheckEachProject(context: Context) {
52-
if (!context.project.isGradleProject) {
53-
return
54-
}
52+
@JvmField
53+
val IID_COMPATIBILITY_CHECK_FAILURE = Issue.create(
54+
id = "IidCompatibilityCheckFailure",
55+
briefDescription = "Firebase IID Compatibility Check Unable To Run",
56+
explanation = """
57+
The check failed to run as it encountered unknown failure.
58+
This is most likely caused by a new version of Android Gradle Plugin that this check does not support.
59+
Please make sure your build does not depend on firebase-iid version earlier than 20.1.1 as it will cause issues.
60+
""",
61+
category = Category.LINT,
62+
priority = 1,
63+
severity = Severity.INFORMATIONAL,
64+
implementation = IMPLEMENTATION
65+
)
66+
}
5567

56-
for (variant in context.project.gradleProjectModel.variants) {
68+
override fun beforeCheckEachProject(context: Context) = catching(context) {
69+
for (variant in getVariants(context.project)) {
5770
for (lib in variant.mainArtifact.dependencies.libraries) {
5871
val coordinates = lib.resolvedCoordinates
5972
if (coordinates.groupId == "com.google.firebase" && coordinates.artifactId == "firebase-iid") {
@@ -68,18 +81,45 @@ class IncompatibleIidVersionDetector : Detector() {
6881
}
6982
}
7083

84+
private fun getVariants(project: Project): List<Variant> {
85+
if (!project.isGradleProject) {
86+
return listOf()
87+
}
88+
89+
// using reflection here due to breaking change in lint-api 26.6.0 that changed the return type
90+
// of getGradleProject()
91+
val method = project.javaClass.getMethod("getGradleProjectModel")
92+
method.isAccessible = true
93+
val model = method.invoke(project)
94+
val variantsMethod = model.javaClass.getMethod("getVariants")
95+
variantsMethod.isAccessible = true
96+
return variantsMethod.invoke(model) as List<Variant>
97+
}
98+
7199
private fun isCompatibleVersion(coordinates: MavenCoordinates): Boolean {
72100
val versionComponents = coordinates.version.split('.', limit = 3).toTypedArray()
73101

74102
// Incompatible if major version is before v20
75-
if (20 > versionComponents.get(0).toInt()) {
103+
if (20 > versionComponents[0].toInt()) {
76104
return false
77105
}
78106
// Compatible if major version is after v21
79-
if (21 <= versionComponents.get(0).toInt()) {
107+
if (21 <= versionComponents[0].toInt()) {
80108
return true
81109
}
82110
// Its compatible if major version is v20 and minor version is after v20.1
83-
return 1 <= versionComponents.get(1).toInt()
111+
return 1 <= versionComponents[1].toInt()
112+
}
113+
114+
private inline fun catching(context: Context, block: () -> Unit) {
115+
try {
116+
block()
117+
} catch (ex: Throwable) {
118+
context.report(
119+
IID_COMPATIBILITY_CHECK_FAILURE,
120+
Location.create(context.file),
121+
"Check failed with exception: $ex"
122+
)
123+
}
84124
}
85125
}

0 commit comments

Comments
 (0)