
本文介绍了10、Kotlin DSL求职学习资料,有助于帮助完成毕业设计以及求职,是一篇很好的资料。
对技术面试,学习经验等有一些体会,在此分享。
0x1、DSL简介
DSL (domain specific language,领域特定/专用语言
),指的是:专注于特定领域问题的计算机语言,如常见的SQL、正则表达式等。
与通用编程语言
(Java、Python等)能开发一个完整应用不同,DSL只关注于某个领域,如SQL仅支持数据库相关操作、正则表达式只支持字符串检索与替换。
DSL不涉及类似数据结构的细节,以简洁
的形式进行表达,直观易懂,使得读、调用代码的成本得以降低,有些甚至没有编程经验的都能进行使用。
0x2、DSL分类
根据 是否从宿主语言构建而来
,分为 内部DSL
(从一种宿主语言构建而来,如Gradle)和 外部DSL
(从零开始构建,需实现语法分析器等,如XML、JSON)。
Tips:Kotlin构建的DSL属于内部DSL
0x3、Kotlin DSL应用范例
Anko
不知道你有没有听过或用过 Anko
这个库,一个用DSL的方式代替XML来生成UI布局 的神器,曾经红极一时。
比如下面这样的XML布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/ed_todo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/main_edit_hint" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/main_button_text" />
</LinearLayout>
用Anko来实现:
verticalLayout {
setGravity(Gravity.CENTER_VERTICAL)
editText {
hintResource = R.string.main_edit_hint
}.lparams(width = matchParent, height = wrapContent)
button {
textResource = R.string.main_button_text
onClick {
toast("click!")
}
}.lparams(width = matchParent, height = wrapContent)
}
直接在kt文件中编写控件,无需findViewById直接操作控件(显示隐藏、赋值操作等),除了类型安全、空安全、代码复用性强等优势外,不用经历XML格式的解析过程,一定程度上也节约了CPU资源,提升了渲染速度。
Gradle构建
在《Kotlin实用指南 | 0x2 – 基础知识》里就提到过:
Android项目采用Gradle构建,默认使用groovy脚本构建,而在Gradle 4.0开始,正式支持Kotlin脚本构建,也就是Gradle Kotlin DSL。
接着演示下从Groovy DSL迁移到Kotlin DSL上,先是项目层级的build.gradle文件:
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
将文件扩展名从 .gradle
改为 .gradle.kts
,比如此处改为:builg.gradle.kts
buildscript {
val kotlinVersion = "1.3.72"
repositories {
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:4.0.0")
classpath(kotlin("gradle-plugin", version = kotlinVersion))
}
}
allprojects {
repositories {
google()
jcenter()
}
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
0x1、DSL简介
DSL (domain specific language,领域特定/专用语言
),指的是:专注于特定领域问题的计算机语言,如常见的SQL、正则表达式等。
与通用编程语言
(Java、Python等)能开发一个完整应用不同,DSL只关注于某个领域,如SQL仅支持数据库相关操作、正则表达式只支持字符串检索与替换。
DSL不涉及类似数据结构的细节,以简洁
的形式进行表达,直观易懂,使得读、调用代码的成本得以降低,有些甚至没有编程经验的都能进行使用。
0x2、DSL分类
根据 是否从宿主语言构建而来
,分为 内部DSL
(从一种宿主语言构建而来,如Gradle)和 外部DSL
(从零开始构建,需实现语法分析器等,如XML、JSON)。
Tips:Kotlin构建的DSL属于内部DSL
0x3、Kotlin DSL应用范例
Anko
不知道你有没有听过或用过 Anko
这个库,一个用DSL的方式代替XML来生成UI布局 的神器,曾经红极一时。
比如下面这样的XML布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/ed_todo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/main_edit_hint" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/main_button_text" />
</LinearLayout>
用Anko来实现:
verticalLayout {
setGravity(Gravity.CENTER_VERTICAL)
editText {
hintResource = R.string.main_edit_hint
}.lparams(width = matchParent, height = wrapContent)
button {
textResource = R.string.main_button_text
onClick {
toast("click!")
}
}.lparams(width = matchParent, height = wrapContent)
}
直接在kt文件中编写控件,无需findViewById直接操作控件(显示隐藏、赋值操作等),除了类型安全、空安全、代码复用性强等优势外,不用经历XML格式的解析过程,一定程度上也节约了CPU资源,提升了渲染速度。
Gradle构建
在《Kotlin实用指南 | 0x2 – 基础知识》里就提到过:
Android项目采用Gradle构建,默认使用groovy脚本构建,而在Gradle 4.0开始,正式支持Kotlin脚本构建,也就是Gradle Kotlin DSL。
接着演示下从Groovy DSL迁移到Kotlin DSL上,先是项目层级的build.gradle文件:
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
将文件扩展名从 .gradle
改为 .gradle.kts
,比如此处改为:builg.gradle.kts
buildscript {
val kotlinVersion = "1.3.72"
repositories {
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:4.0.0")
classpath(kotlin("gradle-plugin", version = kotlinVersion))
}
}
allprojects {
repositories {
google()
jcenter()
}
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
0x1、DSL简介
DSL (domain specific language,领域特定/专用语言
),指的是:专注于特定领域问题的计算机语言,如常见的SQL、正则表达式等。
与通用编程语言
(Java、Python等)能开发一个完整应用不同,DSL只关注于某个领域,如SQL仅支持数据库相关操作、正则表达式只支持字符串检索与替换。
DSL不涉及类似数据结构的细节,以简洁
的形式进行表达,直观易懂,使得读、调用代码的成本得以降低,有些甚至没有编程经验的都能进行使用。
0x2、DSL分类
根据 是否从宿主语言构建而来
,分为 内部DSL
(从一种宿主语言构建而来,如Gradle)和 外部DSL
(从零开始构建,需实现语法分析器等,如XML、JSON)。
Tips:Kotlin构建的DSL属于内部DSL
0x3、Kotlin DSL应用范例
Anko
不知道你有没有听过或用过 Anko
这个库,一个用DSL的方式代替XML来生成UI布局 的神器,曾经红极一时。
比如下面这样的XML布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/ed_todo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/main_edit_hint" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/main_button_text" />
</LinearLayout>
用Anko来实现:
verticalLayout {
setGravity(Gravity.CENTER_VERTICAL)
editText {
hintResource = R.string.main_edit_hint
}.lparams(width = matchParent, height = wrapContent)
button {
textResource = R.string.main_button_text
onClick {
toast("click!")
}
}.lparams(width = matchParent, height = wrapContent)
}
直接在kt文件中编写控件,无需findViewById直接操作控件(显示隐藏、赋值操作等),除了类型安全、空安全、代码复用性强等优势外,不用经历XML格式的解析过程,一定程度上也节约了CPU资源,提升了渲染速度。
Gradle构建
在《Kotlin实用指南 | 0x2 – 基础知识》里就提到过:
Android项目采用Gradle构建,默认使用groovy脚本构建,而在Gradle 4.0开始,正式支持Kotlin脚本构建,也就是Gradle Kotlin DSL。
接着演示下从Groovy DSL迁移到Kotlin DSL上,先是项目层级的build.gradle文件:
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
将文件扩展名从 .gradle
改为 .gradle.kts
,比如此处改为:builg.gradle.kts
buildscript {
val kotlinVersion = "1.3.72"
repositories {
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:4.0.0")
classpath(kotlin("gradle-plugin", version = kotlinVersion))
}
}
allprojects {
repositories {
google()
jcenter()
}
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
部分转自互联网,侵权删除联系
最新评论