Android development with Kotlin (1)
Kotlin Android
According to Realm Report (2017-Q4, https://realm.io/realm-report/2017-q4 ), the development on the Android side in the past year: Java decreased from 95% to Java 85%, while Kotlin increased from 5% to 15%, as shown below
Kotlin is about to change the whole Android ecosystem
Judging from this trend, coupled with the release of the latest Android Studio 3.0 (built-in support for Kotlin development of Android projects), Kotlin will soon subvert the status of Java in the Android field.
This chapter will guide you to quickly start using Kotlin for Android application development.
14.1 Quick Start Hello World
We start with a simple Kotlin version of the Hello World Android application.
14.1.1 Preparations
First prepare the development tools. For Android development, it is better to use Android Studio, an IDE officially supported by Google.
Introduction to Android Studio 3.0
Google released the Android 8.1 Oreo developer preview on 2017-10-26 and officially released Android Studio 3.0, introducing a series of new features to its IDE.
Android Studio 3.0 is focused on accelerating Android app development and includes a number of updates, one of the most important features including support for Kotlin. As announced by Google at Google I/O 2017.5, Kotlin is officially supported for Android development. Android Studio 3.0 is the first milestone version to support the Kotlin language (before that, you can use the Kotlin plugin for Android Studio).
This version provides many convenient and practical functions such as code auto-completion and syntax highlighting. In addition, Android Studio's built-in conversion tool can easily convert Java code into Kotlin code, as shown in the following figure
raw java code
Java to Kotlin Tool
Converted Kotlin code
Install Android Studio 3.0
Android Studio is the official IDE for Android. One of the highlights of Android Studio 3.0 is the built-in support for Kotlin ( https://developer.android.google.cn/kotlin/index.html ). As announced at Google I/O 2017, Kotlin has become the official Android development language.
Using Android Studio 3.0, we can easily convert Java source code into Kotlin code automatically, and also can directly create an Android project developed in Kotlin language, just check Include Kotlin support when creating a new project.
First go to the official website to download and install: https://developer.android.google.cn/studio/install.html . The version of the installation package currently downloaded by the author is android-studio-ide-171.4408382-mac.dmg, click the dmg file after downloading
install android studio ide
Just copy it to the application.
14.1.2 Create a Kotlin-based Android project
First create a new project. If you don't have a project open, click Start a new Android Studio project in the Welcome to Android Studio window
Welcome to Android Studio window
If you have opened the project, please click File > New > New Project, as shown in the figure below
New Project
Enter the Create Android Project dialog. Configure the basic application information in the Create Android Project dialog box, pay attention to check the Kotlin support option, and click Next. As shown below
Create a Kotlin-based Android project
Enter Target Android Devices to configure application running SDK and environment information
Target Android Devices
We check the Phone and Tablet option, API 15: Android 4.0.3, click Next to enter the Add Activity interface
Add Activity interface
We choose Empty Activity, click Next, and enter the configuration Activity interface
Configure the Activity interface
After configuring the Activity Name and Layout Name, click Finish. We'll get a Kotlin version of the Hello World Android application. The project directory is as follows
Project directory
14.1.3 Description of project directory files
Among them, the dependency of the kotlin-gradle-plugin plugin is added to the top-level Gradle configuration file build.gradle
buildscript {
ext.kotlin_version = '1.1.51'
...
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
...
The content of the build.gradle configuration file in the app directory is as follows
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
dependencies {
...
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
...
}
The apply plugin: 'kotlin-android-extensions' means to use the Kotlin Android Extensions plugin. This plug-in is a Kotlin extension plug-in specifically for Android, which realizes functions with frameworks such as Data-Binding and Dagger.
The content of the layout file activity_main.xml is as follows
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.easy.kotlin.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
The MainActivity.kt code is as follows
package com.easy.kotlin.myapplication
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
The content of the AndroidManifest.xml file is as follows
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.easy.kotlin.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
14.1.4 Installation and operation
Click the Run button in the function menu bar
run
We will be prompted to select the target device for the application deployment to run
It should be noted that the mobile phone should be connected to the USB debugging mode. Click OK, Android Studio will complete packaging, installation and other matters for us. The final running effect is as follows
at last
More Kotlin learning materials can be scanned for free!
" Advanced Kotlin Enhanced Combat "
Chapter 1 Kotlin Getting Started Tutorial
● Kotlin overview
● Kotlin vs. Java
● Skillful use of Android Studio
● Understand the basic types of Kotlin
● Walk into Kotlin's array
● Walk into Kotlin collections
● Complete code
● Basic grammar
Chapter 2 Kotlin Practical Pit Avoidance Guide
● Method input parameters are constants and cannot be modified
● No Companion, INSTANCE?
● Java overloading, how to make a clever transition in Kotlin?
● Null gesture in Kotlin
● Kotlin overrides the method in the Java parent class
● Kotlin becomes "ruthless", even TODO is not spared!
● Pitfalls in is and as`
● Understanding of Property in Kotlin
● also keyword
● takeIf keyword
● How to write singleton mode
Chapter 3 Project Combat "Kotlin Jetpack Combat"
● Start with a demo that worships a great god
● What is the experience of writing Gradle scripts in Kotlin?
● The Triple Realm of Kotlin Programming
● Kotlin higher order functions
● Kotlin Generics
● Kotlin Extensions
● Kotlin delegates
● Coroutine "unknown" debugging skills
● Graphical coroutine: suspend
" The most detailed Android version of kotlin coroutine entry advanced combat in history "
Chapter 1 Introduction to the Basics of Kotlin Coroutines
● What is a coroutine
● What is Job, Deferred, and coroutine scope
● Basic usage of Kotlin coroutines
Chapter 2 Preliminary Explanation of Key Knowledge Points of Kotlin Coroutine
● Coroutine Scheduler
● Coroutine context
● Coroutine startup mode
● Coroutine scope
● suspend function
Chapter 3 Exception Handling of Kotlin Coroutines
● Generation process of coroutine exception
● Exception handling for coroutines
Chapter 4 Basic application of kotlin coroutines in Android
● Android uses kotlin coroutines
● Use coroutines in Activity and Framgent
● Use coroutines in ViewModel
● Use coroutines in other environments