携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第25天,点击查看活动详情
文章目录
前言
Kotlin(科特林)是一个用于现代多平台应用的静态编程语言,由 JetBrains 开发。
Kotlin可以编译成Java字节码,也可以编译成JavaScript,方便在没有JVM的设备上运行。
除此之外Kotlin还可以编译成二进制代码直接运行在机器上(例如嵌入式设备或 iOS)。Kotlin已正式成为Android官方支持开发语言。
在Android开发中使用Kotlin非常容易。在本教程中,我们将准备使用开发工具Android Studio进行开发学习。如果你在android上使用 Intellij IDEA开发工具,这个过程几乎是一样的。
创建一个项目工程
首先,为您的应用程序创建一个新的Kotlin Android项目
1、打开android studio,单击欢迎屏幕上的开始一个新的android studio项目(Start a new Android Studio project)或文件新建新项目(File ,New ,New project)。如下图,其它我都选择默认
注:未勾选Include C++ support,此在NDK开发调用so文件、c和c++代码时可勾选!
默认创建的代码、运行效果
MainActivity.kt
package com.luminal.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)
}
}
layout布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".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>
运行效果: