1 package dagger.hilt.android.plugin.util
2 
3 import com.android.Version
4 
5 /**
6  * Simple Android Gradle Plugin version class since there is no public API one. b/175816217
7  */
8 internal data class SimpleAGPVersion(
9   val major: Int,
10   val minor: Int,
11 ) : Comparable<SimpleAGPVersion> {
12 
compareTonull13   override fun compareTo(other: SimpleAGPVersion): Int {
14     return compareValuesBy(
15       this,
16       other,
17       compareBy(SimpleAGPVersion::major).thenBy(SimpleAGPVersion::minor)
18     ) { it }
19   }
20 
21   companion object {
22 
<lambda>null23     val ANDROID_GRADLE_PLUGIN_VERSION by lazy { parse(Version.ANDROID_GRADLE_PLUGIN_VERSION) }
24 
parsenull25     fun parse(version: String?) =
26       tryParse(version) ?: error("Unable to parse AGP version: $version")
27 
28     private fun tryParse(version: String?): SimpleAGPVersion? {
29       if (version == null) {
30         return null
31       }
32 
33       val parts = version.split('.')
34       if (parts.size == 1) {
35         return SimpleAGPVersion(parts[0].toInt(), 0)
36       }
37 
38       return SimpleAGPVersion(parts[0].toInt(), parts[1].toInt())
39     }
40   }
41 }
42