怎么在Android Studio中使用Kotlin?

  1、使用Android Studio的插件

  2、将Android Studio升级到3.0版本:目前不推荐,因为3.0的版本目前还是Dev Channel渠道,也就是开发渠道,还没正式发布

  所以,今天我们就讲讲如何使用第1种方式来创建第一个Kotlin项目:

插件安装:

  1、进入Plugins\Install JetBrains plugins中,搜索Kotlin后安装

  2、正常创建一个Android 项目(平时怎么创建的现在还是怎么创建),截图如下

  

  在这边,我把它上传到我的Github上,这样方便全程跟踪每次的更改内容的记录

  3、通过转换工具将java源文件转换成Kotlin

  

  

  转换之后的结果变化如下:java源文件的后缀变成.kt,类的继承方式变了

  

  4、配置Kotlin的依赖

以上操作,会在Project的build.gradle文件中加入红色标注内容

 // Top-level build file where you can add configuration options common to all sub-projects/modules.

 buildscript {
4 ext.kotlin_version = '1.1.2-3'//Kotlin扩展属性
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
10 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"//子模块构建脚本时需要的classpath // 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
}

同时在子模块中(app module)的build.gradle中加入红色标注内容

 1 apply plugin: 'com.android.application'
2 apply plugin: 'kotlin-android'//引入Kotlin插件,如果是java 模块,引入的是'kotlin'插件
3
4 android {
5 compileSdkVersion 25
6 buildToolsVersion "25.0.2"
7 defaultConfig {
8 applicationId "com.aso.firstkotlin"
9 minSdkVersion 15
10 targetSdkVersion 25
11 versionCode 1
12 versionName "1.0"
13 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 }
15 buildTypes {
16 release {
17 minifyEnabled false
18 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 }
20 }
21 }
22
23 dependencies {
24 compile fileTree(dir: 'libs', include: ['*.jar'])
25 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
26 exclude group: 'com.android.support', module: 'support-annotations'
27 })
28 compile 'com.android.support:appcompat-v7:25.3.1'
29 testCompile 'junit:junit:4.12'
30 compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"//Kotlin依赖包
31 }
32 repositories {
33 mavenCentral()
34 }

5、到以上4个步骤操作完成之后,选择同步Gradle(即Sync Now事件),结束后,恭喜,我们的第一个Kotlin工程就已经创建并且配置好了。

6、接下来,因为我们安装了Kotlin插件,所以我们在创建新的文件时,会多出如图红色快的选项,然后根据需要创建即可

 

最新文章

  1. GIS的发展
  2. SQL Server代理警报
  3. 端午小长假--前端基础学起来03CSS为网页添加样式
  4. poj3020
  5. 关于SOAP
  6. SPOJ - OTOCI LCT
  7. 剑指offer 替换字符串中的空格
  8. Node.js 博客实例(五)编辑与删除功能
  9. 关于js的那些事儿
  10. Redis 从数据库配置
  11. C# 获取网页源代码
  12. bzoj3812&uoj37 主旋律
  13. autium designer smart pdf一个小问题
  14. Qt 布局管理
  15. hihocoder 编程练习赛23
  16. 转-【exp/imp】将US7ASCII字符集的dmp文件导入到ZHS16GBK字符集的数据库中
  17. C 500uS状态机架构
  18. php CURL模拟GET、POST请求。
  19. 【WEB前端开发最佳实践系列】CSS篇
  20. 面试题之一(Spring和堆栈和逻辑运算符)

热门文章

  1. Fang Fang HDU - 5455 (思维题)
  2. [LUOGU] 4933 大师
  3. 面试:A
  4. Django Template(模板系统)
  5. ansible plugins 列表
  6. EntityFramewordCore 2.2 DBFirst简单使用
  7. 清北学堂模拟赛d3t1 a
  8. 有用的生活有关的website
  9. hdu_1013_Digital Roots_201310121652
  10. ASP.NET MVC 源码分析(一)