This is the fifth day of my participation in Gwen Challenge

preface

The last article said that we could use Kotlin as a scripting language instead of Groovy. This article will transform Groovy into Kotlin step by step, starting with a new project. Ps: Migration can be incomplete, and the project supports both Groovy and Kotlin scripting languages simultaneously

Why is there an advantage to using Kotlin compared to Groovy?

Most Android developers are already working with Kotlin, so it’s easy to choose Kotlin as a scripting language. Here’s a table to compare groovy with Kotin

kotlin groovy
Automatic code completion Square root
Type safety Square root
Source code quick navigation Square root
refactoring automatic manual

Start changing the Gradle scripting language in your project to Kotlin

Android projects using gradle as construction tools, the project directory will generate Settings. Gradle, build. Gradle, app/build. Gradle

Some simple syntactic differences between Kotlin and Groovy

  1. Grrovy strings can use single quotes, whereas kotlin must be double quotes
  2. Groovy can omit the extension on method calls, whereas Kotlin cannot
  3. Groovy can omit the = assignment operator when assigning attributes, while Kotlin cannot
Modify the Settings. Gradle

Change the settings.gradle. KTS extension and the editor will prompt you to change the single quotation marks to double quotation marks because strings in Kotlin use double quotation marks! Then change include “:app” to the form kotlin calls include(“:app”) sync and settings.gradle is done

Modify the build. Gradle

Modify the extension build.gradle. KTS for the entire file method call parentheses based on syntax differences. The clean Task is changed to register sync

//task clean(type: Delete) {
//    delete(rootProject.buildDir)
//}
tasks.register<Delete>("clean") {
    delete(rootProject.buildDir)
}
Copy the code

Modify the app/build. Gralde

Modify the extension build.gradle.kts

Library 'apply plugin: 'kotlin-android' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android' apply plugin: Plugins {id("com.android.application") kotlin("android") kotlin(" Android.extensions ") } * versionCode, versionName, VersionCode =1 versionName="1.0" // versionCode =1 versionName="1.0" BuildTypes {release {minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), // Named ("release") {isMinifyEnabled = false proguardFiles(  getDefaultProguardFile("proguard-android-optimize.txt"), "Proguard-rules.pro")}} dependencies change all single quotes to double quotes with groovy parentheses that omit method callsCopy the code

– > git source code