How to Use Kotlin in an Existing Android App?

前端 未结 9 1022
清酒与你
清酒与你 2021-01-05 03:18

I have an Android App developed using Java. I now want to start using Kotlin for the same app. Is it possible to use Kotlin and Java side-by-side in an existing app?

9条回答
  •  旧时难觅i
    2021-01-05 04:07

    To enable Kotlin support for your existing Java-only project follow the steps and Android Studio will automatically set-up everything for you :-

    1. Go to Tools -> Kotlin -> Configure Kotlin in Project
    2. Choose Single Module and select the spefiic module or All modules if you want to add kotlin support for all of your modules.

    You're done and good to go!

    NOTE :- Android Studio will automatically add the following lines of code for you in the respective files -

    In Application-level Gradle file :-

    apply plugin: 'kotlin-android'
    ......
    dependencies{
    ......
    implementation "androidx.core:core-ktx:+"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    }
    ......
    repositories {
        mavenCentral()
    }
    

    In Project-level Gradle file

    buildscript {
        ext.kotlin_version = '1.3.72'
        ......
        dependencies {
            ......
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    
        }
    }
    

提交回复
热议问题