Kotlin 中文文檔

轉載地址:http://www.tuicool.com/articles/faqyMzE




gitbook 牆內訪問速度很糟糕 現在有了 牆內地址 啦 :)

國內服務器由 掘金 贊助

稀土掘金:挖掘最優質的互聯網技術 / 聯合編輯每日精選內容 / 移動端優質閱讀體驗

本書源碼在github

pd下載 ePub下載

記得要點 star star star

發現有翻譯的不好的或者錯誤歡迎到 github 提issue

號外 號外 Kotlin 1.0 正式發佈

Android 世界的 Swift 終於發佈1.0版本

Kotlin 是一個實用性很強的語言,專注於互通,安全,簡潔,工具健全...

無縫支持 Java+Kotlin 項目,可以更少的使用樣版代碼,確保類型安全。

Kotlin 1.0 更新日誌

還換了logo :)

Kotlin LOC (軟件規模代碼行) 如下圖

近期我會重新讀一遍 Kotlin 官方文檔 並對現在的這份文檔進行更新(又立 flag 了) -- 2016.2.16

如何在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"







發佈了130 篇原創文章 · 獲贊 53 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章