zoukankan      html  css  js  c++  java
  • kotlin第一个项目的搭建

      怎么在Android Studio中使用Kotlin?

      1、使用Android Studio的插件

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

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

    插件安装:

      1、进入PluginsInstall JetBrains plugins中,搜索Kotlin后安装

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

      

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

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

           

      

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

      

       4、配置Kotlin的依赖

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

     1 // Top-level build file where you can add configuration options common to all sub-projects/modules.
     2 
     3 buildscript {
     4     ext.kotlin_version = '1.1.2-3'//Kotlin扩展属性
     5     repositories {
     6         jcenter()
     7     }
     8     dependencies {
     9         classpath 'com.android.tools.build:gradle:2.2.2'
    10         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"//子模块构建脚本时需要的classpath
    11 
    12         // NOTE: Do not place your application dependencies here; they belong
    13         // in the individual module build.gradle files
    14     }
    15 }
    16 
    17 allprojects {
    18     repositories {
    19         jcenter()
    20     }
    21 }
    22 
    23 task clean(type: Delete) {
    24     delete rootProject.buildDir
    25 }

     同时在子模块中(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插件,所以我们在创建新的文件时,会多出如图红色快的选项,然后根据需要创建即可

      

  • 相关阅读:
    微信小程序开发--页面之间的跳转
    iOS基础UI控件介绍-Swift版
    iOS技术栈-Swift版
    SpringBoot实战(四)获取接口请求中的参数(@PathVariable,@RequestParam,@RequestBody)
    SpringBoot实战(三)代码热部署
    SpringBoot实战(二)Restful风格API接口
    SpringBoot实战(一)HelloWorld
    Spring Boot浅谈(是什么/能干什么/优点和不足)
    CentOS的基本设置界面
    虚拟机VM安装Linux系统CentOS7
  • 原文地址:https://www.cnblogs.com/dfsu/p/6955524.html
Copyright © 2011-2022 走看看