15
15
package com.google.firebase.installations.lint
16
16
17
17
import com.android.builder.model.MavenCoordinates
18
+ import com.android.builder.model.Variant
18
19
import com.android.tools.lint.detector.api.Category
19
20
import com.android.tools.lint.detector.api.Context
20
21
import com.android.tools.lint.detector.api.Detector
21
22
import com.android.tools.lint.detector.api.Implementation
22
23
import com.android.tools.lint.detector.api.Issue
23
24
import com.android.tools.lint.detector.api.Location
25
+ import com.android.tools.lint.detector.api.Project
24
26
import com.android.tools.lint.detector.api.Severity
25
27
import com.android.tools.lint.detector.api.Scope
26
28
import java.util.EnumSet
@@ -46,14 +48,25 @@ class IncompatibleIidVersionDetector : Detector() {
46
48
severity = Severity .ERROR ,
47
49
implementation = IMPLEMENTATION
48
50
)
49
- }
50
51
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
+ }
55
67
56
- for (variant in context.project.gradleProjectModel.variants) {
68
+ override fun beforeCheckEachProject (context : Context ) = catching(context) {
69
+ for (variant in getVariants(context.project)) {
57
70
for (lib in variant.mainArtifact.dependencies.libraries) {
58
71
val coordinates = lib.resolvedCoordinates
59
72
if (coordinates.groupId == " com.google.firebase" && coordinates.artifactId == " firebase-iid" ) {
@@ -68,18 +81,45 @@ class IncompatibleIidVersionDetector : Detector() {
68
81
}
69
82
}
70
83
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
+
71
99
private fun isCompatibleVersion (coordinates : MavenCoordinates ): Boolean {
72
100
val versionComponents = coordinates.version.split(' .' , limit = 3 ).toTypedArray()
73
101
74
102
// Incompatible if major version is before v20
75
- if (20 > versionComponents.get( 0 ) .toInt()) {
103
+ if (20 > versionComponents[ 0 ] .toInt()) {
76
104
return false
77
105
}
78
106
// Compatible if major version is after v21
79
- if (21 <= versionComponents.get( 0 ) .toInt()) {
107
+ if (21 <= versionComponents[ 0 ] .toInt()) {
80
108
return true
81
109
}
82
110
// 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
+ }
84
124
}
85
125
}
0 commit comments