How to Use Kotlin in an Existing Android App?

前端 未结 9 979
清酒与你
清酒与你 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条回答
  • 2021-01-05 04:16

    Just click ctrl+alt+shift+k to convert your whole app from java to kotlin. And remember to keep your system connected to active internet while you do so because it needs to download the required plugins. I think you'll be good to go after doing this.

    0 讨论(0)
  • 2021-01-05 04:17

    To enable Kotlin into your existing Android project. Follow these steps:-

    1. Application-level build file:- Add

      apply plugin: 'kotlin-android'

      apply plugin: 'kotlin-android-extensions'

      Add In dependency:- implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    2. Project Level build File:-

      dependencies {
          classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
          // NOTE: Do not place your application dependencies here; they belong
          // in the individual module build.gradle files
      }
      
    0 讨论(0)
  • 2021-01-05 04:20

    In Android Studio 4, there is no need to manually configure Kotlin for a Java project.

    When adding Kotlin files to a Java project, Android Studio will automatically setup Kotlin.

    Here is the full procedure:

    1. Add any new Android component (Activity for example)

    1. Choose Kotlin as the language

    1. Android Studio downloads Kotlin

    Note: When it finished, it could not resolve the R file in the Kotlin Activity. To fix this, I just hit hovered over the red R in the editor and hit alt-enter.

    Finally, in my MainActivity.java:

    Intent launchNewIntent = new Intent(this, KotlinActivity.class);
    startActivityForResult(launchNewIntent, 0);
    

    This code opens the KotlinActivity from the MainActivity in Java.

    0 讨论(0)
提交回复
热议问题