最近发现很多公司对于Android开发者都要求又提升了一些,比如熟悉Kotlin的开发,会使用React Native,会使用Flutter开发语言,要么就会对前端有一定的基础小程序 Vue等等。。。
在众多知识中我选择学习Kotlin,其原因Kotlin作为谷歌霸霸主推的语言,而且它是一种兼容Java的语言,还有就是看到很多Android开发者都在使用Kotlin写项目。在大环境的驱使下,我选择学习这门新语言。

今天的学习目录

【Kotlin语言的简介】

1)Kotlin语言的介绍

Kotlin是一种基于JVM的新型编程语言,它完全兼容Java语言,Kotlin代码可以完全编译成Java字节码,也可以编译成JavaScript,方便在没有JVM的设备上运行,与Java语言相比Kot具备一下几点优势
● Kotlin更简洁,完成相同的业务功能Kotlin代码通常只有Java代码的三分之一
● Kotlin更安全,在编码阶段就能自动检出常见的空指针问题
● Kotin更强大,它提供了拓展函数 默认参数 接口委托 属性代理等,但是Java不具备这些高级特性,从而Kotlin可以完成更复杂的业务逻辑。

2)Android官方语言

(百度的资料)

Kotlin很早就被运用到Android开发中,之前一直作为Android Studio的插件提供下载

2017年5月谷歌宣布将Kotlin纳入Android Studio开发的官方语言,这就意味着Android Studio对Kotlin的编译支持会大大增强。

2017年10月 Android Studio推出正式版3.0,从3.0版本开始Android Studio自动集成Kotlin插件,在安装Android Studio3.0时候就连带着配置了Kotlin的开发环境

2017年11月Kotlin推出了1.2的发布版,该版本具有更好的阔平台性,编译性能也远远大于1.1版本,同时更好的支持Android开发。

【Kotlin开发工具】

Kotlin开发工具去Kotlin语言中文站上发现有一下几个 https://www.kotlincn.net/


我这里所使用的则Android Studio3.2

操作流程一样,跟使用Android Studio新建Java项目一样,只不过要选择Ktolin

【Kotlin简单配置】

1)Kotlin配置

Android Studio虽然插件中带有kotlin的配置,但需要在build.gradle中配置

apply plugin: ‘kotlin-android’
apply plugin: ‘kotlin-android-extensions’
在文件的末尾dependencies中增加对Kotlin插件库的编译声明
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

要想使用Anlo库得自行依赖一下

    implementation "org.jetbrains.anko:anko-sqlite:$ankoVersion"    implementation "org.jetbrains.anko:anko-sdk15:$ankoVersion"    implementation "org.jetbrains.anko:anko-sdk15-listeners:$ankoVersion"    implementation "org.jetbrains.anko:anko-design:$ankoVersion"    implementation "org.jetbrains.anko:anko-design-listeners:$ankoVersion"    implementation "org.jetbrains.anko:anko-appcompat-v7-listeners:$ankoVersion"

简单的使用

最后贴出我的build.gradle配置文件

// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {    ext.kotlin_version = '1.2.61'    repositories {        google()        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:3.2.0'        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 {        google()        jcenter()    }}task clean(type: Delete) {    delete rootProject.buildDir}

app文件下的build.gradle配置文件

apply plugin: 'com.android.application'apply plugin: 'kotlin-android'apply plugin: 'kotlin-android-extensions'android {    compileSdkVersion 28    defaultConfig {        applicationId "com.xiaya.mykotlin"        minSdkVersion 17        targetSdkVersion 28        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}dependencies {    implementation fileTree(dir: 'libs', include: ['*.jar'])    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"    implementation 'com.android.support:appcompat-v7:28.0.0'    implementation 'com.android.support.constraint:constraint-layout:1.1.3'    testImplementation 'junit:junit:4.12'    androidTestImplementation 'com.android.support.test:runner:1.0.2'    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'    implementation "org.jetbrains.anko:anko-sdk15:0.9.1" // So here it's 15 too    implementation "org.jetbrains.anko:anko-appcompat-v7:0.9.1"    implementation "org.jetbrains.anko:anko-design:0.9.1"    implementation "org.jetbrains.anko:anko-recyclerview-v7:0.9.1"}

2)Kotlin简单使用

以前开发者想要操作控件的时候,必须findViewById。但是使用Kotlin语言的时候,只需要在所处的Activitity添加一行

import kotlinx.android.synthetic.main.activity_main.*

Kotlin的控件变量就会自动映射,无需findViewById

  tv.text = "你好哈"  btn.setOnClickListener { toast("点了一下") }

这里的toast方法是Anko库中的方法,使用者需要依赖即可使用。

小结

通过这次学习应该要掌握的技能
(1) 了解Kotlin的简介以及所使用的开发工具
(2) 可以使用Android Studio运行成功一个Kotlin项目
(3) 使用Kotlin进行一些简单操作

更多相关文章

  1. Flutter 1.0 正式版: Google 的跨平台 UI 工具包
  2. 【Android的从零单排开发日记】之入门篇(五)——Android四大组件之
  3. 2019年Android开发者常见面试题(一)
  4. 开发者大杀器 —— Battery Historian,刨根问底,揪出 Android(安卓
  5. Android(安卓)Studio3.0开发JNI流程------JNI中字符串拼接的三种
  6. Android开发者快速上手Kotlin(十) 之 Android工程实战和Kotlin总
  7. Android(安卓)APN的设置问题--进一步讨论
  8. 实战:Android活动目录LiveFolder开发
  9. 关于android混合开发模式Hybrid逻辑梳理

随机推荐

  1. android 自定义线程池ThreadPoolUtils工
  2. Android(安卓)根文件系统启动分析
  3. Android(安卓)相对布局 简单编程
  4. android之CheckBox
  5. Android(安卓)之 资源自适应与国际化
  6. EventBus Usage
  7. Android条形统计图实现
  8. ViewPager介绍
  9. Android(安卓)WebView
  10. android 查看apk中资源文件