Use Kotlin to develop Android applications (II): Create a new project, kotlinandroid

Source: Internet
Author: User

Use Kotlin to develop Android applications (II): Create a new project, kotlinandroid

This is the second article about Kotlin. If you discover any problems, please continue to "shoot bricks ".

 

Original article title: Kotlin for Android (II): Create a new project

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

Antonio Leiva (http://antonioleiva.com/about)

Original article published:

 

 

After having a clear idea of what Kotlin is and what it can do for us, we should configure Android Studio to help us develop Android apps with Kotlin. You only need to perform a few steps at the beginning, and then configure Gradle for each new project.

 

In this group of articles, I will create a simplified version of Bandhook, Which I developed earlier. It mainly links to the music Rest API and then returns information about a group of bands. Go to the Bandhook Kotlin project on Github to view the relevant code.

 

Create a new project and download the Kotlin plug-in

In Android Studio, you can create a basic Android project with only one Activity.

 

The first thing to do is to download the Kotlin plug-in. Find the Plugins item in Preferences of Android Studio. Search for the Kotlin plug-in there. Then install the Kotlin plug-in and restart the IDE.

 

 

Adding the Kotlin plug-in depends on the build. gradle of the application.

The root build. gradle requires a new dependency, which is required to use Kotlin in our main module:

 

1 buildscript {2     repositories {3         jcenter()4     }5     dependencies {6         classpath 'com.android.tools.build:gradle:1.1.3'7         classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.11.91'8     }9 }

 

Configure the build. gradle Module

First, apply the Kotlin plug-in:

 

1 apply plugin: 'com.android.application'2 apply plugin: 'kotlin-android'

 

Then, add the Kotlin library to your dependency:

 

1 dependencies {2     compile fileTree(dir: 'libs', include: ['*.jar'])3     compile 'org.jetbrains.kotlin:kotlin-stdlib:0.11.91'4 }

 

Finally, you need to add the Kotlin folder, which is created in the next step to store the source code folder:

 

 1 android { 2     compileSdkVersion 22 3     buildToolsVersion "22.0.0" 4   5     ... 6   7     sourceSets { 8         main.java.srcDirs += 'src/main/kotlin' 9     }10 }

 

Alternatively, you can skip this step and use the Android Studio function for the next step:

 

 

I prefer to do this step manually to manage my Gradle file organization, but the second option is easier.

 

Create a Kotlin folder

You can easily change from "Android" to "Project )". Go to "app-> src-> main" and create a folder named "kotlin:

 

 

Convert Java Activity to Kotlin File

The Kotlin plug-in can convert Java Activity to the Kotlin class. In the "Code" menu, select "Convert Java File to Kotlin File", we can easily Convert our Activity to the Kotlin class:

 

 

IDE recommends that you move the new file to the Kotlin file. Click "Move File" (if you see this option, you can manually Move the File ).

 

You will get very similar code converted to Kotlin. I suggest you read these codes until you understand the differences between them:

 

 1 public class MainActivity : ActionBarActivity() { 2   3     override fun onCreate(savedInstanceState: Bundle?) { 4         super.onCreate(savedInstanceState) 5         setContentView(R.layout.activity_main) 6     } 7   8   9     override fun onCreateOptionsMenu(menu: Menu): Boolean {10         // Inflate the menu; this adds items to the action bar if it is present.11         getMenuInflater().inflate(R.menu.menu_main, menu)12         return true13     }14  15     override fun onOptionsItemSelected(item: MenuItem): Boolean {16         // Handle action bar item clicks here. The action bar will17         // automatically handle clicks on the Home/Up button, so long18         // as you specify a parent activity in AndroidManifest.xml.19         val id = item.getItemId()20  21         //noinspection SimplifiableIfStatement22         if (id == R.id.action_settings) {23             return true24         }25  26         return super.onOptionsItemSelected(item)27     }28 }

 

Main differences

As long as you read the previous code, we can see some obvious differences. More differences will be read in subsequent articles:

 

  • Replace the "extends" keyword with a colon
  • Explicitly use "override": In Java, we can use annotations to make our code clearer, but not necessary. Kotlin forces us to do this.
  • Use "fun" for Functions": Kotlin is an object-oriented functional development language. It is very similar to other development languages, such as Scala. Java methods are represented by functions.
  • Use different naming methods for function parameters: Type and name are written in another way and separated by colons.
  • Option with semicolon: The end of each line does not require a semicolon. If you want to use it, you can also. However, we can save time without a semicolon and the code is clearer.
  • Other secondary details: I have already talked about '? 'Symbol. It indicates that the parameter can be null ). Processing null is different from using Java.

 

Summary

Although we can think that it is very difficult to use a new programming language, Kotlin is the simplest and interoperable programming language created by the JetBrains team and covers the needs of insufficient Java. Because Android Studio is also based on JetBrains, it is easy to integrate into this IDE and start to use it.

 

In the next article, we will introduce some tips and tips to make it easier for us to develop Android apps with Kotlin.

 

Previous Article: http://www.cnblogs.com/figozhg/p/4983919.html

 

 

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.