Kotlin搭建第一个Android程序(Intellij idea)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhangmingbao2016/article/details/55272832

Kotlin 是Java语言的补充者,而非替代品,具体的对比可以参考文章:https://code.tutsplus.com/articles/java-vs-kotlin-should-you-be-using-kotlin-for-android-development–cms-27846
这篇博文将像大家介绍如何使用Kotlin搭建搭建第一个Android程序,中途也遇到一些坑,希望能帮助到大家
工具:Intellij idea(刚开始我是用的eclipse,那个坑货大家可以去自己体会一下(解释一下,免得大家喷我这句话:本来很强大,但对于Kotlin,它太过陈旧),建议大家不要用,对于As,有人说As是Intellij idea去掉其他功能只保留Android的产物,我相信这也不是无缘之风,说到底,其实是差不多的)
首先,你的把Intellij idea配置成Android开发环境,不多说,教程多的满天飞.
然后新建一个Android程序,要我教吗?给钱!!!!!!!!!!!!!!!!!
新建Android项目的人性化截图
接下来是重头戏,如图:打开MainActivity.java,
这里写图片描述
将MainActivity.java转换成Kotlin文件(MainActivity.kt),转换的办法有多种,最简单的是按Ctrl+Shift+A组合键会打开功能搜索框,然后输入convert就会出现convert java File to Kotlin File选项如图:
这里写图片描述
双击该选项,就会完成相应的转换(第二种办法就是点击Code菜单项会有convert java File to KotlinFile选项),转换后如图:

这里写图片描述接下来,在项目中配置Kotlin,如果我们新建的项目,项目中会自动完成Kotlin相关配置,但是如果是将java文件转换为Kotlin文件,系统则不会为项目配置Kotlin,配置方法为:按Ctrl+Shift+A组合键弹出功能搜索框,输入configure Kotlin会有Configure Kotlin in project 选项如图:
这里写图片描述
双击该选项,按如下图选择:
这里写图片描述
点击OK,再接下来,就是添加一些配置文件,首先修改build.gredle文件,再系统自动生成的基础上添加一下内容:classpath “org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version”如图:
这里写图片描述
然后修改app/build.gredle文件,在自动生成的文件中添加如下内容:apply plugin: ‘kotlin-android-extensions’如图:
这里写图片描述
然后sync一下项目慢慢等待吧,等完成了Gredle就可以跑项目了,下面是我测试的xml文件和MainActivity.kt文件:app/build.gredle,,,


apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.zhangmingbao.test2"
        minSdkVersion 15
        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'
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

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'
    testCompile 'junit:junit:4.12'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
repositories {
    mavenCentral()
}

build.gredle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.0.6'
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.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
}

activity_main.xml

<?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:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context="com.example.zhangmingbao.test2.MainActivity">

    <TextView
            android:id="@+id/myText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"/>
</RelativeLayout>

MainActivity.kt

package com.example.DEmoApp

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        myText.text = "12222222"
        myText.textSize = 20.0f
    }
}

中途遇到的错误:
提示:kotlinx Unresolved as a symnble
原因:apply plugin: ‘kotlin-android-extensions’和classpath “org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version”没有添加在制定的地方,具体添加位置还请看上文。

到此结束了,欢迎大家吐槽!

猜你喜欢

转载自blog.csdn.net/zhangmingbao2016/article/details/55272832