使用Kotlin开发Android应用 - 环境搭建 (1)
一. 在Android Studio上安装Kotlin插件
按快捷键Command+,
-> 在Preferences界面找到Plugins -> 点击Browse repositories ...
-> 输入Kotlin
-> 安装Kotlin
(老版本需要安装 Kotlin
和Kotlin Extensions For Android
两个插件, 最新版本的Kotlin插件包含了Android Extensions
, 因此不需要再安装Kotlin Extensions For Android
插件了), 如图:

从插件信息上可以看到Kotlin Extensions For Android
插件已经被淘汰掉了, 它被合并到Kotlin插件中, 请看下图:

二. 配置构建脚本
在构建脚本中配置kotlin插件和kotlin库
- (1) 在project目录下的build.gradle文件中定义kotlin的版本
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
//可以在module的build.gradle文件中使用, 使用方式为: "$key"
ext.kotlin_version = '1.0.3'
- (2) 在module的build.gradle文件中配置kotlin插件和kotlin库
build.gradle文件内容如下:
buildscript {
repositories {
jcenter()
}
dependencies {
//1. 添加插件到classpath
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'com.android.application'
//2. 应用kotlin插件
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.stone.kotlin_demo"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
//3. 将kotlin源文件添加到编译路径下
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
//4. 使用kotlin库
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
三. 编写Kotlin Hello Kotlin
程序
在AS选中MainActivity的Java文件, 然后找到菜单Code->Convert Java File to Kotlin File, 这样Java源文件(
*.java
)就变成了Kotlin源文件*.kl
, 如图:
转换后的MainActivity.kl内容如下:
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv.text = "Hello Kotlin"
tv.textSize = 20.0f
}
}
activity_main文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.stone.kotlin_demo.MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
除了转换已有的Java源文件外, Kotlin插件可以让我们直接创建Kotlin源文件, 操作如下:
选择源文件所在的包 -> 右键 -> New -> Kotlin Activity
或者 Kotlin File/Class
,
如图:

Hello Kotlin
程序, 界面如下:
至此使用kotlin开发android应用, 环境搭建已经讲完
欲知后事如何, 请听下回讲解......
=======================================================
更新(2017-02-13): 使用图形界面配置Kotlin (更详细的内容请参考官网文档)
安装Kotlin插件

创建一个安卓项目, 将其中的一个Activity文件转换为Kotlin文件.
选中默认生成的MainActivity.java将java源码转换成Kotlin源码(MainActivity.kt), 如图:

还有一种方法是在AS中打开MainActivity.java -> 然后按两次Shift键, 调出 "Search Everywhere" 输入框 -> 再在输入框中输入Convert -> 点击 "Convert Java File to Kotlin File", 即可完成转换, 如下:

或者按"Shift+Command+A" 组合键, 然后在对话框中输入"Convert", 再点击"Convert Java File to Kotlin File", 即可, 如下:

配置项目 (就是在build.gradle中使用kotlin插件和其标准库)
在转换好的MainActivity.kt文件中, 双击Shift键 (或者按"Shift+Command+A"组合键), 调出 "Search Everywhere" 输入框, 在输入框中输入 "Configure", 点击"Configure Kotlin in Project", 如图:

或 ("Shift+Command+A"组合键操作图)

这时候出现下面的配置面板

在此配置面板中你可以选择在哪些module中加入kotlin配置以及选择kotlin的版本.
配置完之后会在项目根目录下的build.gradle中添加kotlin插件, 而在module的build.gradle中应用kotlin插件并添加kotlin标准库的依赖.
如下:


其实和手动配置没有多大区别, 都是在gradle配置中使用kotlin插件和依赖kotlin标准库, 只是用图形界面操作更加方便 !!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)