android studio 初次使用Kotlin

本文详细介绍了如何使用Kotlin从零开始搭建一个Android项目,包括配置主工程与模块的build.gradle文件,设置依赖及将MainActivity转换为Kotlin文件。

第一步 :配置主工程 build .gradle

buildscript {
    ext.kotlin_version = '1.0.3'
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        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 {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

第二步:配置module的 build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.example.administrator.kotlin"
        minSdkVersion 9
        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'
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

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.1.1'
    testCompile 'junit:junit:4.12'
}

第三步:将新建的MainActivity转化为Kotlin 文件

点击code 转化为kotlin 文件




### 使用 KotlinAndroid Studio 中进行开发 #### 插件安装 为了在 Android Studio使用 Kotlin 开发应用,需确保已安装 Kotlin 插件。通过 `Android studio -> preferences... -> Plugins -> Install JetBrains plugin…` 的路径进入插件市场,在搜索栏内键入 "Kotlin" 并选取右侧显示的安装选项完成安装过程[^1]。 #### 项目配置 当首次编辑包含 Kotlin 文件(如 `MainActivity.kt`)时,如果尚未配置 Kotlin 支持,则会收到提示建议配置 Kotlin 环境。此时可以选择点击弹窗中的 configure 按钮并选择 `Android with Gradle` 方式来快速设置;另一种方法是从菜单栏依次点击 `Tools->Kotlin->Configure Kotlin in Project` 来手动启动配置向导。按照指引操作直至结束,这一步骤将会自动修改项目的根目录下的 `build.gradle` 文件以加入必要的依赖项和支持库[^2]。 #### 编写代码 一旦上述准备工作全部就绪,即可着手编写应用程序逻辑。值得注意的是,在 Kotlin 中书写语句结尾处无需像 Java 那样强制加上分号[^3]。下面给出一段简单的例子用于展示如何创建一个带有 TextView 组件的基础界面布局: ```xml <!-- activity_main.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:text="Hello, Kotlin!" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> ``` 此外,对于活动类文件而言,通常会在对应的 `.kt` 文件里定义业务处理函数等内容。例如初始化视图组件、响应用户交互事件等动作都可以在此实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值