Android Studio 中Kotlinx开发
来源:互联网 发布:现在的淘宝太坑了 编辑:程序博客网 时间:2024/06/06 07:37
-使用AndroidStudio新建一个工程,File–>Settings–>Plugins, 输入Kotlin,安装kotlin完毕后,重启AS
-打开MainActivity,打开菜单栏Code–>选择Convert Java File to Kotlin File 或者使用快捷键Ctrl+Alt+Shift+K 转换为Kotlin文件
**
-打开Tool菜单,如图所示,配置build.gradle文件 , 然后Asyn Now
布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="simcpux.sourceforge.net.myapplication.MainActivity"> <TextView android:id="@+id/hello" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
package simcpux.sourceforge.net.myapplicationimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) hello.text = "kotlin" }}
-现在运行程序会报错,如下错误Error:(5, 8) Unresolved reference: kotlinx Error:(12, 9) Unresolved reference: hello
-只需要在整个工程里配置文件里 classpath “org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version”
// 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.0.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-android-extensions:$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}
-在项目里配置文件里引用进去即可 apply plugin: ‘kotlin-android-extensions’
apply plugin: 'com.android.application'apply plugin: 'kotlin-android'apply plugin: 'kotlin-android-extensions'//启用扩展支持直接使用IDandroid { compileSdkVersion 25 buildToolsVersion "25.0.1" defaultConfig { applicationId "simcpux.sourceforge.net.myapplication" minSdkVersion 17 targetSdkVersion 25 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } sourceSets { main.java.srcDirs += 'src/main/kotlin' }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:25.0.1' compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"}repositories { mavenCentral()}
-运行成功
阅读全文
0 0
- Android Studio 中Kotlinx开发
- 找不到 kotlinx.android.synthetic***
- android studio运行Kotilin程序出现Error:(5, 8) Unresolved reference: kotlinx 解决办法
- kotlinx.android.synthetic.** 坑点
- Kotlin中找不到kotlinx解决方法
- Android Studio中NDK开发
- Android Studio中NDK开发
- Android Studio中NDK开发
- Android Studio中NDK开发
- Android Studio中NDK开发
- Android studio 开发中遇到问题
- 在Android studio中进行NDK开发
- Android Studio中实战Jni开发
- 在Android studio中进行NDK开发
- Android Studio开发项目中问题总结
- Android Studio开发过程中各种问题
- Android Studio开发中编写作者信息
- Android Studio中JNI NDK开发(一)
- 私记
- Oracle通过imp导入dmp文件相关
- npm更新和nodejs更新
- C#当用户在ritchbox中单击鼠标右键时,弹出一个快捷菜单,单击某一地名,在ritchbox中显示相关信息
- WannaCry警示:学会检测和减轻云端恶意内容
- Android Studio 中Kotlinx开发
- NAT和IPsec并存的几种模型(方案一)
- Android浏览器调用APP
- Centos IPV4没有IP地址
- Vue基础之Vue模板语法
- Android 之 Binder与进程间通信
- fastboot的安装使用
- IDEA同时打开多个项目
- 使用icexls进行java Excel导入导出