How to convert Android project java project to kotlin

1. There are several reasons for the introduction
of kotlin development:

1. Most of the code on the market is kotlin, there is no way
2. Some project components cannot be used without androidx, and the latest androidx library is encapsulated in kotlin
3. The latest jetpack framework is kotlin

2. Convert the project java project to kotlin
1. Configure under the gradle file in the project root directory, plus three sentences

buildscript {
    
    
    ext.kotlin_version = "1.5.10"//加上
    repositories {
    
    
        jcenter()
        google()
    }
    dependencies {
    
    
        classpath 'com.android.tools.build:gradle:4.2.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"//加上
        //classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
    }
}

How did 1.5.10 of ext.kotlin_version = "1.5.10" come from, follow the figure below:
Open Android Studio: Tools -> Kotlin -> Configure Kotlin Plugin Updates


Check the kotlin plugin version and update to the latest version, and then keep it consistent with the kotlin version configured in the project

2. Apply the directory gradle file
   to update the plugin, plus two sentences

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'//加上
apply plugin: 'kotlin-android-extensions'//加上

   Add the dependencies corresponding to kotlin

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'

3. How to convert java code to kotlin.
   When writing code, kotlinize java code and

   click "yes"
insert image description here
   kotlin code
   . This method is mainly to study how kotlin is implemented based on java when learning kotlin code

insert image description here

Guess you like

Origin blog.csdn.net/qq_35091074/article/details/123270511
Recommended