Update kotlin-dsl-dependency.gradle to support nonstable versions

We need to support building with unstable versions of Gradle for
AndroidX and AGP integration branch. This change updates how
kotlin-dsl-dependency.gradle kotlin dsl jar is found.

Test: ./gradlew tasks
Change-Id: Ifb15df4f901ac06ebaef34bc0b096397bce2711d
diff --git a/buildSrc/kotlin-dsl-dependency.gradle b/buildSrc/kotlin-dsl-dependency.gradle
index e493a8f..92daade 100644
--- a/buildSrc/kotlin-dsl-dependency.gradle
+++ b/buildSrc/kotlin-dsl-dependency.gradle
@@ -15,12 +15,20 @@
  */
 
 def findGradleKotlinDsl() {
-    // TODO(137044144): After we convert this file to Kotlin (build.gradle.kts), we can just
-    // directly call the getGradleKotlinDsl() method.
-    // We're not doing that yet though because Gradle takes more time to process .kts files (at the
-    // time of writing, adding a .kts file adds roughly 10 addition seconds to build startup time).
+    /*
+     * TODO(137044144): After we convert this file to Kotlin (build.gradle.kts), we can just
+     * directly call the getGradleKotlinDsl() method.
+     * We're not doing that yet though because Gradle takes more time to process .kts files (at the
+     * time of writing, adding a .kts file adds roughly 10 addition seconds to build startup time).
+     *
+     * getGradleVersion() is in a format of X.Y.Z-rc-1 / X.Y.Z. Kotlin dsl jar always drops the
+     * "-rc-1" suffix of the version, thus we need additional substring logic.
+     */
+    def dashIndex = project.gradle.getGradleVersion().indexOf("-")
+    def kotlinDslVersion = dashIndex == -1 ? project.gradle.getGradleVersion()
+            : project.gradle.getGradleVersion().substring(0, dashIndex)
     def kotlinDsl = "" + project.gradle.getGradleHomeDir() + "/lib/gradle-kotlin-dsl-" +
-                    project.gradle.getGradleVersion() + ".jar"
+                    kotlinDslVersion + ".jar"
     if (!project.file(kotlinDsl).exists()) {
         throw new GradleException("Kotlin dsl jar does not exist: " + kotlinDsl)
     }