How to Use Kotlin in an Existing Android App?

2020-04-21 05:39发布

问题:

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?

回答1:

Yes you can mix Java and Kotlin in one project.

From Android doc :

If you're interested in using Kotlin, it's easy to get started because it works side by side with Java and C++ on Android. So you can keep your existing code, continue to use the various Android libraries, and incrementally add Kotlin code to your project. Unlike almost any other language, Kotlin is a drop-in replacement you can use bi-directionally—you can call into the Java language from Kotlin, and you can call into Kotlin from the Java language.

Also you should take a view of kotlin official doc


You can also convert Java code to Kotlin with IntelliJIDEA.



回答2:

simplest way to do this is :

  1. right click on app folder
  2. select new
  3. kotlin file/class
  4. any directory with any name "KotlinTest"
  5. it will prompt you to configure
  6. select appropriate module or all modules(if you are using wear also in your project)
  7. this will start automatic installation
  8. delete KotlinTest class


回答3:

First of all you have to download Kotlin Plugin and integrate that into android studio, after that using double shift write convert java file to kotlin file or you can use short cut Ctrl+Alt+Shift+K



回答4:

Just create a new Activity and set Source Language as Kotlin while creating activity Image



回答5:

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.



回答6:

It is possible to use Java and Kotlin side by side. If it is a small project I would recommend you converting your existing java files into Kotlin, this can be easily done using the ctrl+alt+shift_k shortcut. if the project is pretty large you can keep your java classes while creating new activities in Kotlin. I have an app that many of my model classes are written in java yet the activity and the UI itself is written in Kotlin. Since Kotlin is based on Java there is no problem having both languages in the same project as long as your java files are created in different classes than your Kotlin classes



回答7:

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
    

    }



标签: kotlin