Create an Android project using Kotlin

Source: Internet
Author: User
Tags jcenter

If you've already used Android studio and Gradle, this chapter will be easier. I will not give a lot of details and because the user interface and details may always change.

Our app is made up of a simple weather app, just as Google's beginners Course in Udacity is used. We may focus on different things, but the idea of the app is the same, and you'll find that there are a lot of different things in a typical app. If your Android development level is low, I recommend this, this process is relatively easy.

1. Create a project in Android studio

First, open Android Studio and choose Create new Project , then it will let you enter a name, you can take a name, such as: Weather App . Then you need to enter the company domain name. This field is not particularly important if you don't actually publish the app, but you can use your own domain if you have one. Then give any selected directory as the Save address for this item.

Next, it will let you choose the smallest API version. We chose API 15 because we have a library that requires at least API 15 to work. In any case you are targeting most of the anroid users. Do not select any other platforms except phones and tablets now.

Finally, we need to select an activity template to use as a portal. We can choose Add no Activity and start from scratch (this is a good way if this is a Kotlin project), but I will choose Blank Activity , because I will show you a little bit of fun Kotlin plugin later.

Do not care about the activity name, layout, etc. for the time being. These you will know in the next article. If we need to, I'll change it later. Click on Finish it and let it continue creating the project.

2. Configure Gradle

The Kotlin plugin includes a tool that lets us configure Gradle. But I still tend to keep my control over the Gradle file read and write, otherwise it will only get messy and not be easy. Anyway, it's a good idea to know how it works before using automatic tools. So this time, we're going to do it manually.

First, you need to modify the parent as follows build.gradle :

buildscript {    ‘23.1.1‘    ext.kotlin_version = ‘1.0.0‘    ext.anko_version = ‘0.8.2‘    repositories {        jcenter()        dependencies {            classpath ‘‘            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }}allprojects { repositories { jcenter() }}

As you can see, we created a variable to store the current Kotlin version. When you read this, check the latest version as there may be an updated version already published. We need to use that version number in several different places, such as you need to add a new Kotlin plugin dependency . You will build.gradle need to go to the Kotlin standard library again in the modules you specify.

The same is true for us support library , as is the case with Anko libraries. This way it is easier to modify all the version numbers in one place. And with the same version number, the update does not need to change every place.

We will add Kotlin standard library, Anko Library, and Kotlin and Kotlin Android Extensions Plugin plug-in to dependencies.

 apply plugin:  ' ' Apply plugin: Span class= "hljs-string" > ' kotlin-android ' Apply plugin:  ' kotlin-android-extensions ' Android {...} dependencies {compile  "$ Support_version "Compile " Org.jetbrains.kotlin:kotlin-stdlib:  $kotlin _version "compile " org.jetbrains.anko:anko-common:< Span class= "hljs-variable" > $anko _version "}buildscript {repositories {jcenter ()} dependencies {classpath " Org.jetbrains.kotlin:kotlin-android-extensions: $kotlin _version "}} 

Anko is a very powerful Kotlin library for simplifying some Android tasks. We'll learn some of the Anko later, but it's enough now to just add anko-common . The library is divided into a series of small parts so that we don't add the unused parts.

3. Convert Mainactivity to Kotlin code

Kotlin plugin contains an interesting feature that can translate Java code into Kotlin code. As with any automation, the results will not be perfect, but it provides a lot of help before you can start writing your code using the Kotlin language on the first day.

So we use it in the class. Open the file, and then select Code Convert Java File to Kotlin File . Compare their differences to make you more familiar with the language.

4. Test if everything is ready

We would like to write some code to test whether Kotlin Android extensions is working. I'm not going to explain the code right now, but I want to make sure that it works in your environment. This is probably the hardest part of the configuration.

First, open and activity_main.xml then set the ID of the TextView:

<TextView    android:id="@+id/message"    android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

Then, manually add an import statement to the activity (don't worry that you don't understand it now).


In onCreate , you can now get and access this textview directly.

override fun onCreate(savedInstanceState: Bundle?) {    super.onCreate(savedInstanceState)    setContentView(R.layout.activity_main)    message.text = "Hello Kotlin!"}

Thanks to the interoperability between Kotlin and Java, we can manipulate the Getter/setter method in the Java library in the Kotlin as an action property. We'll explain the properties later, but I'd like to remind you that we can use message.text them instead message.setText . The compiler will convert it into generic Java code, so there is no performance overhead for this use.

Now run the app, and it's working. Check whether TextView is the new content that is displayed. If you have questions or want to see the code, see Kotlin for Android developers repository. As long as each section changes the code, I commit, so be sure to check all the code changes.

Create an Android project using Kotlin

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: 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.