首页 > 程序开发 > 移动开发 > 其他 >

Kotlin 中文文档_Kotlin语法教程_如何在Android studio中使用KotLin

2017-06-19

Kotlin 中文文档_Kotlin语法教程_如何在Android studio中使用KotLin。

如何在Android studio中使用KotLin

在根目录build.gradle里边添加相应的依赖就好

\

看示例:

// 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.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
    }
}

allprojects {
    repositories {
        jcenter()
    }
添加了版本号以及要使用的俩个依赖,如果需要还可以导入其他的依赖。

在app的目录(也就是你android代码目录)的build.gradle文件中添加简单的设置就好
看示例:
buildscript {
    repositories {
        jcenter()


    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'
    }
}


def getDate() {
    return Calendar.getInstance().getTimeInMillis();
}


allprojects {
    repositories {
        jcenter()


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

dependencies {
    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:preference-v7:23.1.1'
    compile 'org.apache.commons:commons-compress:1.10'
    compile 'commons-net:commons-net:3.3'
    compile 'com.github.zafarkhaja:java-semver:0.9.0'
    compile 'org.unbescape:unbescape:1.1.1.RELEASE'
    compile 'org.msgpack:msgpack:0.6.12'
    compile 'com.googlecode.juniversalchardet:juniversalchardet:1.0.3'
    compile 'org.tukaani:xz:1.5'
    compile 'ch.acra:acra:4.6.2'
    testCompile 'junit:junit:4.12'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }


    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }


    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 4
        versionName "1.7.0-unstable"
        if(System.getenv("NIGHTLY_BUILD")) {
            versionName += "+" + System.getenv("NIGHTLY_BUILD_COMMIT").substring(0, 7)
        }
    }


    lintOptions {
        if (System.getenv("NIGHTLY_BUILD")) {
            checkReleaseBuilds false
        }
        abortOnError false
    }


    signingConfigs {
        release {
            if (System.getenv("KEYSTORE_FILE") != null) {
                storeFile = file(System.getenv("KEYSTORE_FILE"))
                storePassword = System.getenv("KEYSTORE_PWD")
                keyAlias = System.getenv("KEYSTORE_ALIAS")
                keyPassword = System.getenv("KEYSTORE_ALIAS_PWD")
            }
            return true
        }
    }


    buildTypes {
        debug {
            buildConfigField "java.util.Date", "BUILD_TIME", "new java.util.Date(" + getDate() + "L)"
            buildConfigField "String", "BUILD_NAME", "\"" + System.getenv("USER") + "\"";
            minifyEnabled false
            shrinkResources false
            debuggable true
            jniDebuggable true
            zipAlignEnabled true
            multiDexEnabled true
        }
        release {
            buildConfigField "java.util.Date", "BUILD_TIME", "new java.util.Date(" + getDate() + "L)"
            buildConfigField "String", "BUILD_NAME", "\"" + System.getenv("USER") + "\"";
            if (System.getenv("KEYSTORE_FILE") != null) {
                signingConfig signingConfigs.release
            }
            multiDexEnabled true
            return true
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}
repositories {
    mavenCentral()
}
主要添加了有3个地方:
1、
	apply plugin: 'kotlin-android'

2、
	compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
3、
android{
	 ......
		sourceSets {
			main.java.srcDirs += 'src/main/kotlin'
		}
	}
这样就可以正常使用了。
如果出现
Execution failed for task ':app:clean'.
> Unable to delete file: C:\Users\User\KotlinGameEngine\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.0.1\jars\classes.jar
类似这样的问题,那么只要在app目录下的build.gradle文件中添加task:

	task clean(type: Exec) {
    ext.lockhunter = '\"C:\\LockHunter.exe\"'
    def buildDir = file(new File("build"))
    commandLine 'cmd', "$lockhunter", '/delete', '/silent', buildDir
}
如果出现Unresolved reference: kotlinx这样的问题,那么需要在app目录下的build.gradle文件中添加:
apply plugin: 'kotlin-android-extensions'
以及要确保classpath配置了classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
相关文章
最新文章
热点推荐