(紧跟Google)搭建Kotlin工程

Why Kotlin For Android?
Since Android took the world by storm, developers have had no alternatives to Java app development. Although its usage is widespread, Java comes with a lot of historical baggage.
Java 8 solved some language issues and corrected even more with Java 10. In order to get much of a benefit from the corrections made in these two releases, you have to set the minimum SDK to Android 24 just to use Java 8, which isn’t an option for many developers. For almost everybody, Java 10 isn’t even on the radar.
Kotlin aims to fill that gap of a missing modern language for the Android platform. There are a few core tenets that Kotlin lives by; it strives to be:
1.Concise to reduce the amount of boilerplate code you need to write.
2.Expressive to make your code more readable and understandable.
3.Safe to avoid entire classes of errors such as null pointer exceptions.
4.Versatile for building server-side applications, Android apps or frontend code running in the browser.
5.Interoperable to leverage existing frameworks and libraries of the JVM with 100 percent Java interoperability.
Above all, it’s a new language! What could be more exciting? iOS developers can’t have all the fun. :]

Setting up Your Environment

By default, Android Studio has no idea what to do with Kotlin, so the first step is to install the Kotlin plugin and configure Kotlin in your project.

Installing the Kotlin Plugin

Go to Android Studio\Preferences and select the Plugins entry.

这里写图片描述

On the Plugins screen, click on Install JetBrains plugin…

这里写图片描述

Search for and select Kotlin from the list and click Install.

这里写图片描述
这里写图片描述

When you’re finished with downloading and installing, the next step is following the prompts to restart the IDE.

这里写图片描述

Configure Kotlin in Project

Now the IDE knows what to do with Kotlin, but your project app doesn’t, so your next move is to modify the project’s build configuration.

Go to Tools\Kotlin\Configure Kotlin in Project.

这里写图片描述

Select Android with Gradle from the Choose Configurator popup that appears.

这里写图片描述

On the Configure Kotlin in Project popup, select the plugin version you want to use (at the time of writing this tutorial, the current version is 1.0.3) and click OK.

这里写图片描述

This action will make a number of changes to your build.gradle files.
build.gradle (Project: omg-android-starter):
buildscript {
  ext.kotlin_version = '1.0.3' // 1
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:2.1.3'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // 2

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
  }
}

allprojects {
  repositories {
    jcenter()
  }
}
build.gradle (Module: OMG Android):
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android' // 3

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.2"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 23
    }
  sourceSets {
    main.java.srcDirs += 'src/main/kotlin' // 4
  }
}

dependencies {
  compile 'com.android.support:appcompat-v7:23.2.0'
  compile 'com.loopj.android:android-async-http:1.4.4'
  compile 'com.squareup.picasso:picasso:2.1.1'
  compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // 5
}
repositories {
  mavenCentral()
}
Let’s go over this step-by-step:
1.Declares the version of Kotlin configured in the project
2.Declares a classpath dependency artifact that contains the Kotlin Gradle plugin with the version declared earlier
3.Specifies the use of the Kotlin Android plugin via apply plugin command
4.Defines that source files found in src/main/kotlin will be compiled. Strictly speaking, gradle will compile source Kotlin files found in src/main/java, but it’s nice to put to put Kotlin files in a Kotlin directory.
5.Added the Kotlin Standard Library as a compile time dependency to the project
Click on Sync Now to build the project. Build and run.

猜你喜欢

转载自blog.csdn.net/lvzhongdi/article/details/72529440