关于高版本Android Studio使用KTS(koltin)作为脚本代替Gradle构建的细节

前两天刚刚才写了安卓构建下载太慢的博客,今天新建项目又用到kotlin作为脚本构建项目时出新问题。一开始我是直接从自己的文章复制粘贴到新项目的gradle当中,结果上来就报好几个红,给我人干懵了。
Unexpected tokens (use ';' to separate expressions on the same line)

pluginManagement {
    repositories {
        maven{ url 'https://maven.aliyun.com/repository/public'}
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "M3Test"
include(":app")

后知后觉发现自己使用了kotlin作为构建脚本。
百度之后改成了:

pluginManagement {
    repositories {
        maven { url = uri('https://maven.aliyun.com/repository/public') }
        maven { url = uri('https://maven.aliyun.com/repository/google' }
        maven { url = uri('https://maven.aliyun.com/repository/jcenter') }
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "M3Test"
include(":app")

构建时依然报错:
Too many characters in a character literal 'https://maven.aliyun.com/repository/public'

我又一脸蒙蔽,阅读完报错之后,尝试将单引号改为双引号:

pluginManagement {
    repositories {
        maven { url = uri("https://maven.aliyun.com/repository/public") }
        maven { url = uri("https://maven.aliyun.com/repository/google") }
        maven { url = uri("https://maven.aliyun.com/repository/jcenter") }
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "M3Test"
include(":app")
 

构建成功。

@see also : https://blog.csdn.net/weixin_71703379/article/details/130960999

kotlin有成熟的lambda语法,相对于java的语法更为高级。kotlin运用于前后端与脚本。这里的问题就是kotlin对lambda表达式当中dsl的最好示例。

@see also: https://zhuanlan.zhihu.com/p/24800713

你可能感兴趣的:(android,studio,android,ide)