[Android-Kotlin学习日志]AndroidStudio中Kotlin环境搭建
来源:互联网 发布:mac锁定触控板快捷键 编辑:程序博客网 时间:2024/06/05 15:33
Google I/O 2017 上将Kotlin升级为Android开发的官方语言,关于Kotlin的特点之类的建议去官网自行了解Kotlin官网。
我所使用的开发工具是AndroidStudio,如果你用IDE相差也不大。下面开始进行开发环境的搭建。
1.下载Kotlin插件
Settings -> plugins -> BrowseRepositories,搜索“Kotlin”
注(网上很多教程中会有Kotlin Android Extensions ,这个插件已经搜索不到,之后可以在gradle中进行配置)
2.插件安装成功(若没有,你可能需要重启Studio)
这两个Kotlin文件应该不需要解释吧。
3.配置Kotlin(如果你有Activity参考m2)
m1:new->Kotlin Activity
m2:Code->Convert java File to Kotlin File
kt Activity创建好之后,studio出现会提示
点击提示,选择如下
完成之后 Sync Now一下gradle。Kotlin环境就配置好了,这时候你就可以使用了。但不知道你是否还记得前面提到的 Kotlin Android Extensions,接下来就开始配置。
4.配置Kotlin Android Extensions
在Module gradle中添加
buildscript { repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" }}
配置完成后即可使用import kotlinx.android.synthetic.main.activity_main.*
5.验证Project gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript { ext.kotlin_version = '1.1.2-4' repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.2' 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 { jcenter() }}task clean(type: Delete) { delete rootProject.buildDir}
Model gradle
apply plugin: 'com.android.application'apply plugin: 'kotlin-android'apply plugin: 'kotlin-android-extensions'android { compileSdkVersion 25 buildToolsVersion "25.0.3" defaultConfig { applicationId "com.siny.kotlinx" minSdkVersion 17 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.1.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"}buildscript { repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" }}repositories { mavenCentral()}
完成以上步骤,Kotlin开发环境就搭建好了。下面测试一下,我们的程序是否能运行
MainActivity代码:
import kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) tvMessage.text="Hello Siny!!" }}
activity_main.xml
<TextView android:id="@+id/tvMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"/>
运行界面:
- [Android-Kotlin学习日志]AndroidStudio中Kotlin环境搭建
- Kotlin学习-AndroidStudio环境搭建与配置
- Kotlin Android 环境搭建
- 在AndroidStudio上搭建 Kotlin 开发环境
- Kotlin语言学习之AndroidStudio集成Kotlin开发环境
- AndroidStudio Kotlin项目搭建
- Android Studio Kotlin环境搭建
- Kotlin Android Studio 环境搭建
- Kotlin Android环境的搭建
- 在androidstudio中学习kotlin(一)
- Android Kotlin学习笔记(二)---环境搭建
- androidstudio Kotlin环境(离线)配置
- [Kotlin]Kotlin学习笔记(一):环境搭建及Kotlin特色语法学习笔记
- kotlin学习1(环境搭建)
- 零基础 Kotlin开发 Android 环境搭建
- Kotlin for Android(搭建环境)
- Kotlin开发环境搭建
- Kotlin环境搭建
- thinkphp uploadify ajax上传图片,网上很多有问题,我简单总结一下
- 什么时候使用字节流、什么时候使用字符流,二者的区别
- 文章标题
- 第十届“中国电机工程学会杯”全国大学生电工数学建模竞赛 A 题:微电网日前优化调度
- WeakReference与SoftReference
- [Android-Kotlin学习日志]AndroidStudio中Kotlin环境搭建
- Python~~简介介绍
- NSString NSCFString NSCFConstantString isMemberOfClass 遇到的相关的问题
- SQL Server 2008 收缩日志 清空删除大日志文件
- linux的yum update命令
- DEDECMS自动编号(序号)[field:global.autoindex/]
- dp四边形优化 Hdu 2829 Lawrence 题解
- RecyclerView在GridLayoutManager情况下实现四周都有分割线的ItemDecoration
- Android动画效果生动有趣的通知NiftyNotification(Android Toast替代品)