Kotlin在Android Studio中安装与配置
来源:互联网 发布:薛之谦淘宝店铺号 编辑:程序博客网 时间:2024/05/20 00:16
Kotlin的安装与配置
因为本人使用的开发工具是Android Studio,所以这里只说明一下Kotlin在Android Studio中的安装与配置。
Kotli插件的安装
在安装Kotlin插件之前,Android Studio、JDK、SDK都已经安装和配置完成,也就是说Android Studio是可以运行调试的。 打开Android Studio,进入setting设置,如下图:
在左侧找到Plugins并点击,在右侧显示的内容找到Browse Repositories,点击进入搜索页面,如下图:
在最上面的输入框输入Kotlin,在搜索出的内容中找到Kotlin,点击后,右会出现一个install按钮,点击进行安装,如下图:
安装成功后,重启Android Studio。
Kotlin在项目中的配置
新建一个项目,新建成功后,在菜单栏的选项卡中找到”Tools”–>”Kotlin”–>”Configure Kotlin in Project”,也就是在项目中配置Kotlin:
进入配置选项后,选择第二个选项All Models containing Kotlin files,然后点击OK
然后,选择Code–>Convert Java File to Kotlin File,将文件转换为Kotlin格式的文件,转换成功后,所有的文件都变成了kt格式的文件。打开Project下的build.gradle文件,在buildscript标签和dependencies 标签内分别加入以下内容:
buildscript { //按照自己的Kotlin版本号填写 ext.kotlin_version = '1.1.3-2' repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.1' //添加的Kotlin的gradle配置 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 }}
然后打开app内的build.gradle文件,进行如下配置:
apply plugin: 'com.android.application'//下面两行是要添加的内容apply plugin: 'kotlin-android'apply plugin: 'kotlin-android-extensions'android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.rushro2m.androidkotlin" minSdkVersion 16 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(include: ['*.jar'], dir: 'libs') 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.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'com.android.support:recyclerview-v7:25.3.1' //下面两条是要添加的内容 compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"}
添加成功后,进行一下同步,然后就可以在项目中进行Kotlin的开发了。
调试Kotlin是否应用成功
打开res–>layout–>activity_mian.xml文件,添加一个TextView控件:
<FrameLayout 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.rushro2m.androidkotlin.MainActivity"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="TextView" android:textSize="20sp" /></FrameLayout>
打开已经转换为kt文件的MainActivity.kt文件
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //给TextView控件设置显示文本 textView.text = "Hello Kotlin" }}
然后运行程序,发现程序可以正常调试,说明Kotlin就配置成功了,以后开发就可以使用Kotlin语音进行开发了。
- Kotlin在Android Studio中安装与配置
- Android@Kotlin 在Android studio 中配置Kotlin
- Kotlin for Android(一)在android Studio中配置Kotlin
- android studio 3.0中配置Kotlin
- Kotlin基础(一)android studio中配置Kotlin
- Kotlin基础(一)android studio中配置Kotlin
- 在Android Studio中使用Kotlin。
- 在android studio中使用kotlin
- Android Studio中安装Kotlin插件
- Kotlin学习一 Android Studio 中安装 Kotlin Plugin
- android studio配置kotlin
- Android Studio 配置Kotlin
- Android Studio配置Kotlin
- Android Studio配置kotlin
- 【Kotlin】Android Studio 配置
- Kotlin学习系列----Android Studio配置Kotlin
- android studio 配置kotlin 环境
- Android Studio 配置使用 Kotlin
- Java 网络编程四 C/S B/S(例:模拟浏览器、服务器、网络爬虫)
- iOS提上传包报错:No suitable application records were found. Verify your bundle identifier 'xx' is correct
- windows10软链接时失败时的删除操作
- SqueezeDet:一种应用于自动驾驶实时目标检测中的标准、小型、低功耗的全卷积神经网络(二)
- OpenSSL中文手册之理论基础
- Kotlin在Android Studio中安装与配置
- pat:L1-044. 稳赢
- Exception java.lang.SecurityException: class "javax.servlet.FilterRegistration"
- fcntl文件属性和ioctl文件输入输出控制函数详解
- ThinkPHP3.2.3学习
- 按需加载图片(图片懒加载)
- Ubuntu14.04 安装 Nvidia显卡驱动
- 00-Linux下安装jdk
- fputc函数