Use Kotlin to create the first Android Project (KAD 01) and kotlinandroid

Source: Internet
Author: User

Use Kotlin to create the first Android Project (KAD 01) and kotlinandroid

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

By Antonio Leiva

Time: Nov 21,201 6

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

 

First course of the Kotlin series: Use Kotlin to create the first Android project.

 

 

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

 

From the very beginning, if you have heard of the Kotlin language but haven't used it yet, these articles are perfect for you. Hope you like them!

 

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

 

Create a Kotlin project from scratch

First, you need to download the latest stable version of Android Studio. The Canary or Beta version is also possible, but the new version is sometimes added with the Kotlin plug-in.

 

Once completed, install the Kotlin plug-in. IntelliJ is installed by default, but it is different from Android Studio.

 

1. Install the Kotlin plug-in

To install the Kotlin plug-in, go to the "Preferences" Plugins and find it in the search bar:

 

2. Create a new Android Project

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

 

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

 

3. Convert the new Activity to the Kotlin code

You only need to select menu options. It's almost the same.

 

The Kotlin plug-in includes a conversion converter that converts Java code to Kotlin code (instead of vice versa, you will not do this anyway ). Open the class you want to convert and selectCode-> Convert Java File to Kotlin File.

 

The following code is obtained:

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 this content.

 

4. convert a project to a Kotlin Project

Another utility can perform this conversion for you. Just goTools> Kotlin> Configure Kotlin in ProjectIn the dialog box, add some changesBuild. gradleFile:

 

What has this changed? Add the new dependency Kotlin plug-inBuild. gradleTop layer:

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 plug-in is also appliedBuild. gradleModule:

1 apply plugin: 'kotlin-android'

 

Add the Kotlin library as dependency:

1 dependencies {2     ...3     compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"4 }

 

Conclusion

Okay. Now you can run this project, and it can be seen that it runs very well.

 

At the first compilation, it takes a little longer than Java, and according to this article, incremental build is even faster than Java.

 

Great, right?Let me know what you think about this article.

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

Note:

In "installing the Kotlin plug-in", the original author seems to be working on the Macbook. On Windows 10 Android Studio 2.2.2

Try to install Kotlin, which is slightly different:

Configure --> Plugins

If Kotlin cannot be found in Plugins, You need to click the "Search in repositories" link in the dialog box:

From the result of searching for Kotlin, find Kotlin ages. Then, click "Install" on the right side of the dialog box for installation.

Related Article

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.