安裝插件
File -> Settings -> Plugins -> Browse repositories -> 搜索 kotlin
gradle添加依賴
Module的build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.lxs.kotlinconfig"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}
repositories {
maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}
Project的build.gradle
buildscript {
ext.kotlin_version = '1.1.2-4'
repositories {
maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
kotlin.incremental=true增量編譯的機制,可以加快編譯速度 項目根目錄的gradle.properties里配置
把Java代碼轉換成kotlin代碼
Code -> Convert Java File to Kotlin File
快捷鍵 ctrl+alt+shift+k
或者ctrl+shift+A 輸入Convert Java File to Kotlin File
Kotlin提供以下代表數字的內置類型
類型 位寬
Double 64
Float 32
Long 64
Int 32
Short 16
Byte 8
Kotlin支持二進制(0b開頭)十六進制(ox開頭)但不支持八進制。
Kotlin支持數字下划線
val oneMillion = 1_000_000
val creditCardNumber = 1234_5678_9012_3456L
val socialSecurityNumber = 999_99_9999L
val hexBytes = 0xFF_EC_DE_5E
val bytes = 0b11010010_01101001_10010100_10010010
Kotlin中按位操作
shl(bits) 左移運算符(Java <<)
shr(bits) 右移運算符(Java >>)
ushr(bits) 無符號右移,忽略符號位,空位都以0補齊(Java >>>)
and(bits) 按位和
or(bits) 按位或
xor(bits) 按位xor
inv() 逐位倒置
Kotlin中自帶非空檢查如要為空val s:String? = null;
Kotlin中支持的轉義字符\t,\b,\n,\r,\',\",\\和\$。要編碼任何其他字符,請使用Unicode轉義序列語法:'\uFF00'
Kotlin中引入類對象this@類名
Kotlin中運算符重載
一元前綴運算符
+a a.unaryPlus()
-a a.unaryMinus()
!a a.not()
遞增和遞減
a++ a.inc()
a-- a.dec()
算術運算符
a + b a.plus(b)
a - b a.minus(b)
a * b a.times(b)
a / b a.div(b)
a % b a.rem(b),a.mod(b)(已棄用)
a..b a.rangeTo(b)
a in b b.contains(a)
a !in b !b.contains(a)
索引訪問操作符 方括號轉換為調用get和set適當數量的參數。
a[i] a.get(i)
a[i, j] a.get(i, j)
a[i_1, ..., i_n] a.get(i_1, ..., i_n)
a[i] = b a.set(i, b)
a[i, j] = b a.set(i, j, b)
a[i_1, ..., i_n] = b a.set(i_1, ..., i_n, b)
自身運算
a += b a.plusAssign(b)
a -= b a.minusAssign(b)
a *= b a.timesAssign(b)
a /= b a.divAssign(b)
a %= b a.modAssign(b)
等於非等於
a == b a?.equals(b) ?: (b === null)
a != b !(a?.equals(b) ?: (b === null))
比較運算符
a > b a.compareTo(b) > 0
a < b a.compareTo(b) < 0
a >= b a.compareTo(b) >= 0
a <= b a.compareTo(b) <= 0
集合類
val items = listOf("item1", "item2", "item3")
val items:MutableList<String> = mutableListOf("item1", "item2", "item3");
遍歷
for (item in items) {
println(item)
}
for (index in items.indices) {
println("item at $index is ${items[index]}")
}
val set = setOf("item1", "item2", "item3");
val set:MutableSet<String> = mutableSetOf("item1", "item2", "item3");
val map = mapOf("a" to 1, "b" to 2, "c" to 3)
val map = mutableMapOf<String,Int>("a" to 1, "b" to 2, "c" to 3)
for ((k, v) in map) {
println("$k -> $v")
}
for (i in 1..4 step 2) 正序從1到4步長為2 step無時默認為1
for (i in 4 downTo 1 step 2) 倒序從4到1步長為2
for (i in 1 until 10)不包含上限
println控制台輸出
結合DataBinding 使用時需添加如下
kapt {
generateStubs = true
}
dependencies {
kapt "com.android.databinding:compiler:2.3.0"
}
