天天看點

Kotlin的簡單使用練習

如果還沒下載下傳kotlin可以先去setting裡面下載下傳kotlin下載下傳好後可以進行配置

直接上代碼

先在父build.gradle裡添加代碼

ext.kotlin_version = ‘1.1.2-4’

‘1.1.2-4’為最新版

classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version”

Kotlin的簡單使用練習

然後配置子build.gradle

添加

apply plugin: ‘kotlin-android’

apply plugin: ‘kotlin-android-extensions’

productFlavors {

free {

versionName “1.0-free”

}

}

compile “org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version”

效果圖

Kotlin的簡單使用練習
Kotlin的簡單使用練習

也可以直接複制

apply plugin: ‘com.android.application’

apply plugin: ‘kotlin-android’

apply plugin: ‘kotlin-android-extensions’

android {

compileSdkVersion 25

buildToolsVersion “25.0.2”

productFlavors {

free {

versionName “1.0-free”

}

}

defaultConfig {

applicationId “com.gxt.hrys.myapplication”

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”

}

repositories {

mavenCentral()

}

然後基本的配置完成 可以寫代碼了

點選help的Find Action

輸入Convert java file to kotlin 後可轉換mainactivity

可以直接省略findviewbyid() 直接可以這樣寫t1.setText(“aaa”)

t1為mainActivity布局textview的id

第一次寫 多多見諒 。