Create your first Android project with Kotlin (KAD 01)

Source: Internet
Author: User

Original title: Create Your first Android project using Kotlin (KAD 01)

Antonio Leiva

Time: Nov 21, 2016

Original link: http://antonioleiva.com/create-first-android-project-kotlin/

The original author begins the first lesson of the Kotlin series: Create the first Android project with Kotlin.

Today, I started a group of 30 papers (KAD) on the Kotlin language for Android developers. In this series of papers, I'll talk about the most wanted parts of the language and how to apply them to Android development.

From the beginning, if you've heard of the Kotlin language but haven't used it yet, then these articles are perfect for you. I hope you like them!

In today's article, I want you to demonstrate how easy it is to create a Kotlin project from scratch.

Create a Kotlin project from scratch

First thing, you need to download the latest stable version of Android Studio. Canary or beta versions should also be available, but new versions are sometimes added with the Kotlin plugin.

Once completed, install the Kotlin plugin. IntelliJ has a default installation, but not the same as Android studio.

1. Install Kotlin plug-in

To install the Kotlin plugin, go to the "Preferences" plugins and find it in the search bar:

2. Create a new Android project

Now that you have the plug-in installed (and you can install it later), you can create an Android project. This is no different from creating a typical project:

Select Blank screen in the New Activity option. Later, it is easier to convert the code.

3. Convert new activity to Kotlin code

You only need to select the menu option. It's almost over.

The Kotlin plug-in includes converters that convert Java code to Kotlin code (instead of the reverse, you won't do it anyway). Open the class you want to convert, and select Code->convert Java file to Kotlin file.

This gives you the following code:

1 class mainactivity:appcompatactivity () {2     override fun OnCreate (savedinstancestate:bundle?) {3         Super. OnCreate (savedinstancestate)4        Setcontentview (R.layout.activity_main) 5     }6 }

As you can see, although these are similar to Java code, there are still many differences. In the next article, you can learn these things.

4. Convert Project to Kotlin project

Another utility plugin can make this transition for you. Just go to Configure Kotlin in Project, Tools-Kotlin, and add some changes to the build.gradle file in this dialog box:

What does this change? The new dependency Kotlin plug-in relationship joins the build.gradle top level:

1 Dependencies {2     classpath ' com.android.tools.build:gradle:2.2.0 '3     classpath " Org.jetbrains.kotlin:kotlin-gradle-plugin: $kotlin _version "4 }

The Kotlin plugin is also applied to the build.gradle module:

1 Apply plugin: ' Kotlin-android '

Then add the Kotlin Library as a dependency:

1 Dependencies {2    ... 3     Compile "Org.jetbrains.kotlin:kotlin-stdlib: $kotlin _version"4 }

Conclusion

All right. Now you can run the project and see it running very well.

It takes a little longer than Java to compile for the first time, and, according to this article, incremental builds are even faster than Java.

It's great, isn't it? let me know what you think of this article .

-------------------------------------------------------

Note:

In the "Install Kotlin plug-in", the original author looks like it was done in a MacBook. And I'm on Windows10 Android Studio 2.2.2.

Try installing Kotlin, slightly different:

Configure-->plugins

And in plugins cannot find Kotlin, need to click "Search in Repositories" link in the dialog box:

From the results of the search Kotlin, find Kotlin languages. Then, click "Install" on the right side of the dialog box to install.

Create your first Android project with Kotlin (KAD 01)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.