Welcome to the series of tutorials on Android Development with Kotlin. This series is designed with the goal to help you build Android Applications using Kotlin.
欢迎使用Kotlin进行有关Android开发的系列教程。 本系列旨在帮助您使用Kotlin构建Android应用程序。
We’ll start with the absolute basics of Android Development and as the series progresses you’ll gain expertise in doing Android Development with Kotlin.
我们将从Android开发的绝对基础开始,随着系列的发展,您将获得使用Kotlin进行Android开发的专业知识。
In this tutorial, we’ll start with the introduction of Android Studio. If you’ve used Android Studio in the past, do go through this tutorial to know the new features and changes that Android Studio 3.0 brought in.
在本教程中,我们将首先介绍Android Studio。 如果您过去使用过Android Studio,请仔细阅读本教程以了解Android Studio 3.0带来的新功能和更改。
Android Studio入门 (Getting Started with Android Studio)
Android Studio is based on JetBrains’ IntelliJ IDEA software and is specifically used for Android Development. It is available for Windows, Mac, and Linux operating systems. You can download it for your relevant OS from this link.
Android Studio基于JetBrains的IntelliJ IDEA软件,专门用于Android开发。 它可用于Windows,Mac和Linux操作系统。 您可以从此链接为相关的操作系统下载它。
Note: You need the Java Development Kit installed on your system to build and run Android Studio projects.
注意 :您需要在系统上安装Java开发工具包才能构建和运行Android Studio项目。
Let’s start our Android Studio IDE for the first time!
让我们第一次启动我们的Android Studio IDE!

With the introduction of Android Studio 3.0, the loading screen looks cooler!
随着Android Studio 3.0的推出,加载屏幕看起来更酷!
Updating Android Studio IDE
If you have an older Android Studio IDE, do update it from the menu: Android Studio -> Check For Updates.
更新Android Studio IDE
如果您使用的是较旧的Android Studio IDE,请从菜单中进行更新:Android Studio->检查更新。
Subsequently, update the Gradle plugin by the prompts that follow.
随后,通过随后的提示更新Gradle插件。
Your root build.gradle
file looks like this:
您的root build.gradle
文件如下所示:
buildscript {
repositories {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}
Android Studio功能 (Android Studio Features)
Android Studio 3.0 is a big update over the previous 2.3.2 and is focused on aiding the developers.
Following are the major features that have arrived with this update:
Android Studio 3.0是对先前的2.3.2的一个重大更新,致力于帮助开发人员。
以下是此更新附带的主要功能:
- Built-in Kotlin Support 内置Kotlin支持
- Changes in Gradle Syntax Gradle语法的变化
- Extended Support for Java 8 对Java 8的扩展支持
- Optimizing Using Android Profiler 使用Android Profiler进行优化
- Android Instant App Support Android Instant App支持
1.在Android Studio中使用Kotlin (1. Using Kotlin in Android Studio)
For pre-Android Studio 3.0 IDE following was the procedure to enable Kotlin support in your application:
对于Android Studio 3.0之前的IDE,以下是在您的应用程序中启用Kotlin支持的过程:
Go to Android Studio | Preferences | Plugins | Install JetBrains plugin | Kotlin.
转到Android Studio | 首选项| 插件| 安装JetBrains插件| Kotlin 。
The following screen demonstrates the same:

以下屏幕演示了相同的内容:
With the Android Studio 3.0 update, the Android SDK provides built-in Kotlin support while creating a new project as shown below.

借助Android Studio 3.0更新,Android SDK在创建新项目时提供了内置的Kotlin支持,如下所示。

Choose Target Devices
选择目标设备
Note: What happens when instant support is enabled in the above window would be covered in a later section.
注意 :在上述窗口中启用即时支持后,将在后面的部分中介绍。
The root build.gradle
file looks like this:
根build.gradle
文件如下所示:
buildscript {
ext.kotlin_version = '1.1.51'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
To facilitate smaller and faster updates, Android Studio 3.0 utilizes Google’s Maven Repository google()
by default instead of using the Android SDK Manager to find updates to Android Support Library, Google Play Services, and Firebase Maven dependencies.
为了方便进行更小和更快的更新,默认情况下,Android Studio 3.0使用Google的Maven存储库google()
,而不是使用Android SDK管理器查找对Android支持库,Google Play服务和Firebase Maven依赖项的更新。
The app module’s build.gradle
is given below.
该应用模块的build.gradle
在下面给出。
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 26
defaultConfig {
applicationId "net.androidly.androidstudiokotlin"
minSdkVersion 16
targetSdkVersion 26
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-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
2. Gradle语法的变化 (2. Changes in Gradle Syntax)
With the introduction of Android Studio 3.0, the keyword compile
is deprecated. implementation
and api
are used instead.
Typically all compile
statements are replaced with implementation
.
An implementation
statement rebuilds only the single module whereas the api
statement would rebuild all modules that are dependent on the current one. So unless your module doesn’t leak its interfaces with other modules, implementation
should be used.
随着Android Studio 3.0的引入,不推荐使用关键字compile
。 改为使用implementation
和api
。
通常,所有的compile
语句都用implementation
代替。
implementation
语句仅重建单个模块,而api
语句将重建依赖于当前模块的所有模块。 因此,除非您的模块不会泄漏其与其他模块的接口,否则应使用implementation
。
compile 'com.android.support:appcompat-v7:26.1.0'
//changes to
implementation 'com.android.support:appcompat-v7:26.1.0'
3. Java 8支持 (3. Java 8 Support)
Unlike the previous versions of Android Studio wherein you had to enable third-party plugins,
apply plugin: 'me.tatarka.retrolambda'
Android Studio 3.0 has inbuilt support for Java 8.
与之前的Android Studio版本(其中您必须启用第三方插件)不同,
apply plugin: 'me.tatarka.retrolambda'
Android Studio 3.0内置了对Java 8的支持。
It can be enabled from File | Project Structure as shown below:

可以通过File | 项目结构如下图:
Android Profiler (Android Profiler)
The Android Profiler tool in Android Studio 3.0 replaces the Android Monitor tools. These new profiling tools provide real-time data for your app’s CPU, memory, and network activity.
Android Studio 3.0中的Android Profiler工具取代了Android Monitor工具。 这些新的性能分析工具可为您的应用程序的CPU,内存和网络活动提供实时数据。
To open the tool goto: View | Tool Windows | Android Profiler
打开工具转到:视图| 工具窗口| Android Profiler
The tool displays the CPU, memory, and network profiler timelines in a single frame.
该工具在单个帧中显示CPU,内存和网络事件探查器时间轴。
4. Android Instant App支持 (4. Android Instant App Support)
Android Studio 3.0 comes with the support for Instant Apps. Instant Apps allow the user to load a part of the native application without the need to install the complete application. You must have come across hyperlinks in the browser that asks you to install the application to view the contents. Instant App rescues the user from this. It allows for viewing the native app in your browser without the need to download it from the play store.
Android Studio 3.0附带了对Instant Apps的支持。 Instant Apps允许用户加载本机应用程序的一部分,而无需安装完整的应用程序。 您必须在浏览器中遇到超链接,该超链接要求您安装应用程序以查看内容。 Instant App可以从中拯救用户。 它允许您在浏览器中查看本机应用程序,而无需从Play商店下载。
Instant App Support is available for API > 23 and can be enabled from the following window.
Instant App Support可用于API> 23,并可从以下窗口启用。
This is how the project structure for an instant app looks like:

即时应用程序的项目结构如下所示:
Let’s look at each module separately.
让我们分别看每个模块。
- Base feature module:
The fundamental module of your instant app is the base feature module. All other feature modules must depend on the base feature module. The base feature module contains shared resources, such as activities, fragments, and layout files. This creates a feature APK when built.
The build.gradle of this module looks like:
基本功能模块 :apply plugin: 'com.android.feature' android { compileSdkVersion 26 baseFeature true defaultConfig { minSdkVersion 23 targetSdkVersion 26 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { api 'com.android.support:appcompat-v7:26.1.0' api 'com.android.support.constraint:constraint-layout:1.0.2' application project(':app') feature project(':feature') }
即时应用程序的基本模块是基本功能模块。 所有其他功能模块必须依赖于基本功能模块。 基本功能模块包含共享资源,例如活动,片段和布局文件。 构建后会创建功能APK。
该模块的build.gradle如下所示: - Instant App Module:
The build.gradle of this module looks like:apply plugin: 'com.android.instantapp' dependencies { implementation project(':feature') implementation project(':base') }
It builds the Instant App APK that the user views without downloading the app.
即时应用模块 :
该模块的build.gradle如下所示:它会构建用户查看的Instant App APK,而无需下载该应用。
- App Module:
It builds our application’s APK. 应用模块 :
它构建了我们应用程序的APK。 - Feature Module:
This contains code for the part of application that would be shown in the instant APK 功能模块 :
其中包含将在即时APK中显示的应用程序部分的代码
The structure of an instant app on a high level looks like:
即时应用程序的高层结构如下:
Note: It can contain multiple features too.
注意:它也可以包含多个功能。
摘要 (Summary)
Besides the above major changes, Android Studio 3.0 has wholesome other changes. Majorly, performance improvements, build speed optimizations to accelerate your development.
除了上述主要更改之外,Android Studio 3.0还有其他有益的更改。 主要是性能改进,构建速度优化以加速您的开发。
We love the Dark Theme in Android Studio and recommend you the same. It’s available from Preferences | Appearances | Themes | Darcula as shown below.
我们喜欢Android Studio中的“黑暗主题”,并推荐您同样使用。 可从首选项| 外观| 主题| Darcula如下所示。
This brings an end to Android Studio getting started tutorial. Let’s start with our Android Studio Hello World Application Using Kotlin in our next tutorial.
这结束了Android Studio入门教程。 在下一个教程中,让我们从使用Kotlin的Android Studio Hello World应用程序开始。
翻译自: https://www.journaldev.com/5/android-studio-kotlin