Android之使用Kotlin构建Gradle

Android StudioGradle
3.4.25.1.1

首先kotlin-dsl不是什么新鲜的东西了,Gradle5.0发布的时候就有了
Gradle Kotlin DSL目前的版本是1.0.2

现在是否可以抛弃groovy拥抱kotlin了呢,~~迁移还是有点小麻烦的!

目前在Android Studio中创建项目时,默认情况下使用Groovy创建Gradle脚本,那如何向kotlin-dsl迁移呢?

语法替换

一般情况下,Groovy中使用单引号或者双引号来表达字符串。但是Kotlin必须需要双引号。

对Gradle脚本(以app目录下的build.gralde为例)中的所有单引号执行查找和替换(cmd + R),并将其全部更改为双引号。
然后挨个开始转为kotlin-dsl中的语法

apply plugin: "com.android.application"

apply plugin: "kotlin-android"

apply plugin: "kotlin-android-extensions"

转换之后:(先不用管大量报错)

plugins{
    id("com.android.application")
    kotlin("android")
    kotlin("android.extensions")
}

转换前:

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.1"
    defaultConfig {
        applicationId "com.crucio.test"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
        }
    }
}


改为

android {
    compileSdkVersion (29)
    buildToolsVersion ("29.0.1")
    defaultConfig {
        applicationId = "com.crucio.test"
        minSdkVersion (21)
        targetSdkVersion (29)
        versionCode = 1
        versionName = "1.0"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        getByName("release"){
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }
}



dependencies {
    val kotlin_version = "1.3.41"
    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
    implementation ("org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version")
    implementation ("androidx.appcompat:appcompat:1.0.2")
    implementation ("androidx.core:core-ktx:1.0.2")
    implementation ("androidx.constraintlayout:constraintlayout:1.1.3")
    testImplementation ("junit:junit:4.12")
    androidTestImplementation ("androidx.test:runner:1.2.0")
    androidTestImplementation ("androidx.test.espresso:espresso-core:3.2.0")
}

这时候我们修改build.gradle文件名称,改为build.gradle.kts
如果语法没有错误会出现如下图所示:

提示:有新的脚本依赖项可用 这时候点击 Enable auto-reload

这时候发现迁移成功了。但是kotlin能不能实现类似于groovy ext依赖呢?buildSrc登场~

buildSrc

自动补全+单击跳转来了,无需在文件之间手动来回切换~
先看下官方对buildSrc介绍

  1. 在项目根目录下新建一个名为buildSrc的文件夹(与项目里的app文件夹同级)。

  2. 在buildSrc文件夹里创建名为build.gradle.kts的文件


plugins{
    `kotlin-dsl`
}

repositories {
	 // 必不可少
    jcenter()
}
  1. 在buildSrc文件夹里创建src/main/kotlin文件夹,如下图所示。并在该文件夹下创建kt文件。

写完相关依赖代码后我们再去build.gradle.kts文件中进行替换

看见没? 已经可以自动提示补全了。

buildSrc插件

如果嫌弃手动buildSrc有点麻烦,那插件大法也是有的。

举例一个 buildSrcVersions 自动生成 buildSrc 目录不过插件会生成一个空的 settings.gradle.kts 文件,可以考虑删掉。

补充

关于自定义构建类型
groovy中我们自定义构件类型是这样的

buildTypes {
  release {
    ...
  }
  debug {
    ...
  }
}

但是在kotlin-dsl中buildTypes依赖于NamedDomainObjectContainer

	/**
     * Encapsulates all build type configurations for this project.
     *
     * <p>For more information about the properties you can configure in this block, see {@link
     * BuildType}.
     */
    public void buildTypes(Action<? super NamedDomainObjectContainer<BuildType>> action) {
        checkWritability();
        action.execute(buildTypes);
    }

NamedDomainObjectContainer以及父类NamedDomainObjectCollection有几个用于访问这些字符串键的函数
默认都会有release 与 debug 我们可以用getByName,因为构建容器中默认是有映射的

	/**
     * Locates an object by name, failing if there is no such object. The given configure action is executed against
     * the object before it is returned from this method.
     *
     * @param name The object name
     * @param configureAction The action to use to configure the object.
     * @return The object with the given name, after the configure action has been applied to it. Never returns null.
     * @throws UnknownDomainObjectException when there is no such object in this collection.
     * @since 3.1
     */
    T getByName(String name, Action<? super T> configureAction) throws UnknownDomainObjectException;

如果想要自定义构件类型,getByName会抛异常,我们可以用maybeCreate

/**
     * Looks for an item with the given name, creating and adding it to this container if it does not exist.
     *
     * @param name The name to find or assign to the created object
     * @return The found or created object. Never null.
     */
    T maybeCreate(String name);

结尾

先简单说这么多,

附上根目录的build.gradle.kts


buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath(Libs.com_android_tools_build_gradle)
        classpath(Libs.kotlin_gradle_plugin)
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

tasks {
    val clean by registering(Delete::class) {
        delete(buildDir)
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值