Android add kotlin to project

Setting up Android Studio for Kotlin development

If you have been following the latest trends in the AndroidDev world, you are likely to have heard about Kotlin.

Kotlin is a [not so] new programming language developed by JetBrains — the makers of IntelliJ IDE which the Android Studio is based on. Kotlin is statically typed, it runs on the JVM and offers a lot of benefits. There are tons of articles that talk about the advantages of Kotlin in comparison to Java, so I won’t be covering that in this post.

In this post however, we will learn how to set up Android Studio for Kotlin development and will write our “Hello World” app in Kotlin.

Install Kotlin Plugin for Android Studio

The good people at JetBrains have created an IntelliJ/Android Studio Plugin for Kotlin. First, we’re going to go ahead and install the plugin.

To do this, navigate to Preferences > Plugins > Browse Repositories and search for “Kotlin” then, click on “Install” as shown in the figure below.

When the install is complete, you will need to restart Android Studio to apply the new plugin.

Just in case you’re using IntelliJ IDEA 15 or above, you’re in luck — Koltin plugin ships with the IDE.

Create a new Android project

Now that we have the plugin installed, let’s go ahead and create a new Android project — the usual way we would. Navigate to File > New > New Project and follow through project creation wizard. Select the “Create Empty Activity” option at the end.

For more information on creating an Android project, check out this guide https://developer.android.com/studio/projects/create-project.html.

Apply Kotlin Plugin to the project

Next step is to apply the Kotlin plugin to the project. There is an automated tool to do this but sometimes, the tool messes things up, so let’s just walk through the manual process of applying the plugin in our build.gradle files (both at the project level and the app-module level).

Читайте также:  Look Ma, I'm using Jinja!
a. Add Kotlin Gradle Plugin

To configure the plugin, we first need to add the plugin to the root project’s build.gradle , same way it’s done for the Gradle plugin (automatically). Add the plugin to the dependencies closure in the project build.gradle file. The project build.gradle file looks like this:

buildscript  ext.kotlin_version = "1.0.6" // replace with the latest (stable) version: https://github.com/JetBrains/kotlin/releases/latest repositories  jcenter() > dependencies  classpath 'com.android.tools.build:gradle:2.2.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" > > 

Few notes about the block of code above:

  • We created a variable ext.kotlin_version .
    The reason we have this is we may have other places in the project where we need to reference the Kotlin version, so it makes sense to “externalize” that value. You can check out this post about externalizing your dependencies versions using gradle extra properties for more info.
  • We placed this variable in the buildscript closure. This is because, the buildscript is the entry point into this file when the project is being built. If we place the variable outside this closure, this variable won’t be available before the project is built, and the build will fail.
b. Apply the Kotlin Android Plugin

After adding the Kotlin Gradle plugin, the next step is to apply the plugin. To do this, we need to add apply plugin: kotlin-android to the app-module’s build.gradle file

apply plugin: 'com.android.application' apply plugin: 'kotlin-android' // apply kotlin android plugin 

Convert Activity Code from Java to Kotlin

We’ve applied the plugin and setup all we need to, but our “Empty Activity” generated code is still in Java 🤔.

Luckily for us, the Kotlin plugin can help us convert our code from Java to Kotlin 😆.

Currently, our Java Activity looks like this:

To do this, select the file and navigate to Code > Convert Java File to Kotlin File, or use the shortcut — Command + Alt + Shift + K (I believe you can replace Command with control if you’re on a PC).

After converting our code to Kotlin, our “empty activity” looks like this:

Bonus: Staying up to date with Kotlin versions

You’ve made it this far :). Let’s add a bonus tip.

Kotlin is still heavily being worked on, and there are (frequent) fixes and updates. It’s generally a good idea to stay up to date with the new features and changes in the language. The Kotlin Android Studio plugin helps us manage the versions of Kotlin. If you navigate to Tools > Kotlin > Configure Kotlin Plugin Updates as shown in the screenshots below, you will be able to select the update channel — Stable , Early Access Preview — 1.0.x or Early Access Preview — 1.1 — which is currently the bleeding edge version 🙂

Further reading

There are a number of great resources scattered around the internet for learning Kotlin. Some of them are:

In future posts, I’ll be writing about some specific features of the language.

Thanks for reading this post, I am always open to questions, and comments. If you have any, kindly drop them in the comments section below or send me a tweet.

If you found this post useful, or know someone that may find it useful, please feel free to share.

Источник

Add Kotlin to an existing app

Android Studio provides full support for Kotlin, enabling you to add Kotlin files to your existing project and convert Java language code to Kotlin. You can then use all of Android Studio’s existing tools with your Kotlin code, including autocomplete, lint checking, refactoring, debugging, and more.

If you’re starting a new project and want to use Kotlin, see Create a project.

For samples, check out our Kotlin code samples.

Add Kotlin to an existing project

To add Kotlin to your project, do the following:

  1. Click File > New, and choose one of the various Android templates, such as a new blank Fragment, as shown in figure 1. If you don’t see the list of templates in this menu, first open the Project window, and select your app module. create a new blank fragment
  2. In the wizard that appears, choose Kotlin for the Source Language. Figure 2 shows the New Android Activity dialog for when you want to create a new activity. dialog that lets you choose Kotlin for your source language
  3. Continue through the wizard.

Alternatively, you can click File > New > Kotlin File/Class to create a basic Kotlin file. If you don’t see this option, open the Project window and select the java directory. The New Kotlin File/Class window lets you define the file name and provides several choices for the file type: File, Class, Interface, Enum Class, or Object. The choice you make determines the basic scaffolding created for you in the new Kotlin file. If you choose Class, Android Studio creates a new Kotlin source file with the given name and a matching class definition. If you choose Interface, an interface is declared in the file, and so on.

If this is the first time you have added a new Kotlin class or file to your project directly (not using the Android templates), Android Studio displays a warning that Kotlin is not configured in the project, as shown in figure 3. Configure Kotlin by clicking Configure either in the upper right corner of the editor or in the event log alert that pops up in the lower-right corner.

warning dialog that prompts you to configure Kotlin for your project

Choose the option to configure Kotlin for All modules containing Kotlin files when prompted, as shown in figure 4:

choose to configure Kotlin for all modules that contain Kotlin code

Once you click OK, Android Studio adds Kotlin to your project classpath and applies the Kotlin Android plugin to each module that contains Kotlin files. Your build.gradle files should look similar to the examples below:

Groovy

// Project build.gradle file. buildscript < ext.kotlin_version = '1.4.10' . dependencies < classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" >>

Kotlin

// Project build.gradle.kts file. buildscript < extra["kotlin_version"] = "1.4.10" . dependencies < classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") >>

Источник

A Step-by-Step Tutorial to Add Kotlin to Your Java Android Project

Discover a comprehensive tutorial about how to add Kotlin to your existing Java Android project. With the robust support of Kotlin in Android Studio, you can now include Kotlin files in your Java project and make the most of powerful tools such as debugging, code completion, refactoring, lint checking, and more.

How to Add Kotlin to Your Project?

To integrate Kotlin into your Java Android project, just follow these straightforward steps:

  • Open File > New, and choose an Android template.
  • Select Kotlin as the source language.
  • Complete the wizard and create your Kotlin file.

Alternatively, click File > New > File/Kotlin Class if you don’t encounter this option.

By doing so, Android Studio will show a warning message that Kotlin isn’t configured in your project. Therefore, click on “Configure” and choose to configure Kotlin for all modules that contain Kotlin files.

Organizing Kotlin Files and Converting Java Code to Kotlin

By default, Kotlin files are saved on the src/main/java/ folder. Yet, you can save them at the src/main/kotlin/ directory. If you want to convert your Java code to Kotlin, you have two easy alternatives. You can either copy your Java code into a new Kotlin file or convert the Java file to a Kotlin one by clicking on Code > Convert Java File to Kotlin File.

Frequently Asked Questions (FAQ)

1. Can I use my existing Java code with Kotlin?

Absolutely. Kotlin is interoperable with Java, which means you can use your Java code in Kotlin files.

2. What are the main benefits of using Kotlin in my Android project?

Kotlin is a modern, clear, and concise programming language that offers better performance, safety, and less boilerplate code than Java. Moreover, it provides a sleek integration with different libraries, making it easier to write efficient and maintainable code.

3. How do I know if my project is Kotlin-ready?

To check your project’s compatibility with Kotlin, select “Configure Kotlin in Project” from the “Tools” menu. This feature will scan your project for changes or updates and help you with the Kotlin conversion process.

Wrapping Up

tutorials, you’ll be integrating Kotlin to your Java Android project effortlessly. In doing so, you can optimize your coding practices, creating more efficient, maintainable, and streamlined code. Contact us anytime if you have any questions or suggestions.

Источник

Оцените статью