Kotlin 中文文档

 

Kotlin 中文文档 

标签: Kotlinkotlin中文文档
 分类:

转载地址: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里边添加相应的依赖就好
看示例:
[plain] view plain copy
 print?
  1.     // Top-level build file where you can add configuration options common to all sub-projects/modules.  
  2. buildscript {  
  3.     ext.kotlin_version = '1.0.6'  
  4.     repositories {  
  5.         jcenter()  
  6.     }  
  7.     dependencies {  
  8.         classpath 'com.android.tools.build:gradle:2.0.0'  
  9.         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"  
  10.         classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"  
  11.     }  
  12. }  
  13.   
  14. allprojects {  
  15.     repositories {  
  16.         jcenter()  
  17.     }  



添加了版本号以及要使用的俩个依赖,如果需要还可以导入其他的依赖。

在app的目录(也就是你android代码目录)的build.gradle文件中添加简单的设置就好
看示例:
[plain] view plain copy
 print?
  1. buildscript {  
  2.     repositories {  
  3.         jcenter()  
  4.   
  5.   
  6.     }  
  7.     dependencies {  
  8.         classpath 'com.android.tools.build:gradle:2.0.0'  
  9.     }  
  10. }  
  11.   
  12.   
  13. def getDate() {  
  14.     return Calendar.getInstance().getTimeInMillis();  
  15. }  
  16.   
  17.   
  18. allprojects {  
  19.     repositories {  
  20.         jcenter()  
  21.   
  22.   
  23.     }  
  24. }  
  25. apply plugin:  'com.android.application'  
  26. apply plugin: 'kotlin-android'  
  27.   
  28.   
  29. dependencies {  
  30.     compile 'com.android.support:support-v4:23.1.1'  
  31.     compile 'com.android.support:appcompat-v7:23.1.1'  
  32.     compile 'com.android.support:design:23.1.1'  
  33.     compile 'com.android.support:preference-v7:23.1.1'  
  34.     compile 'org.apache.commons:commons-compress:1.10'  
  35.     compile 'commons-net:commons-net:3.3'  
  36.     compile 'com.github.zafarkhaja:java-semver:0.9.0'  
  37.     compile 'org.unbescape:unbescape:1.1.1.RELEASE'  
  38.     compile 'org.msgpack:msgpack:0.6.12'  
  39.     compile 'com.googlecode.juniversalchardet:juniversalchardet:1.0.3'  
  40.     compile 'org.tukaani:xz:1.5'  
  41.     compile 'ch.acra:acra:4.6.2'  
  42.     testCompile 'junit:junit:4.12'  
  43.     compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"  
  44. }  
  45.   
  46.   
  47. android {  
  48.     compileSdkVersion 23  
  49.     buildToolsVersion '23.0.2'  
  50.   
  51.   
  52.     compileOptions {  
  53.         sourceCompatibility JavaVersion.VERSION_1_7  
  54.         targetCompatibility JavaVersion.VERSION_1_7  
  55.     }  
  56.   
  57.   
  58.     packagingOptions {  
  59.         exclude 'META-INF/LICENSE.txt'  
  60.         exclude 'META-INF/NOTICE.txt'  
  61.     }  
  62.   
  63.   
  64.     defaultConfig {  
  65.         minSdkVersion 9  
  66.         targetSdkVersion 22  
  67.         versionCode 4  
  68.         versionName "1.7.0-unstable"  
  69.         if(System.getenv("NIGHTLY_BUILD")) {  
  70.             versionName += "+" + System.getenv("NIGHTLY_BUILD_COMMIT").substring(0, 7)  
  71.         }  
  72.     }  
  73.   
  74.   
  75.     lintOptions {  
  76.         if (System.getenv("NIGHTLY_BUILD")) {  
  77.             checkReleaseBuilds false  
  78.         }  
  79.         abortOnError false  
  80.     }  
  81.   
  82.   
  83.     signingConfigs {  
  84.         release {  
  85.             if (System.getenv("KEYSTORE_FILE") != null) {  
  86.                 storeFile = file(System.getenv("KEYSTORE_FILE"))  
  87.                 storePassword = System.getenv("KEYSTORE_PWD")  
  88.                 keyAlias = System.getenv("KEYSTORE_ALIAS")  
  89.                 keyPassword = System.getenv("KEYSTORE_ALIAS_PWD")  
  90.             }  
  91.             return true  
  92.         }  
  93.     }  
  94.   
  95.   
  96.     buildTypes {  
  97.         debug {  
  98.             buildConfigField "java.util.Date", "BUILD_TIME", "new java.util.Date(" + getDate() + "L)"  
  99.             buildConfigField "String", "BUILD_NAME", "\"" + System.getenv("USER") + "\"";  
  100.             minifyEnabled false  
  101.             shrinkResources false  
  102.             debuggable true  
  103.             jniDebuggable true  
  104.             zipAlignEnabled true  
  105.             multiDexEnabled true  
  106.         }  
  107.         release {  
  108.             buildConfigField "java.util.Date", "BUILD_TIME", "new java.util.Date(" + getDate() + "L)"  
  109.             buildConfigField "String", "BUILD_NAME", "\"" + System.getenv("USER") + "\"";  
  110.             if (System.getenv("KEYSTORE_FILE") != null) {  
  111.                 signingConfig signingConfigs.release  
  112.             }  
  113.             multiDexEnabled true  
  114.             return true  
  115.         }  
  116.     }  
  117.     sourceSets {  
  118.         main.java.srcDirs += 'src/main/kotlin'  
  119.     }  
  120. }  
  121. repositories {  
  122.     mavenCentral()  
  123. }  



主要添加了有3个地方:
1、
[plain] view plain copy
 print?
  1. <span style="white-space:pre">  </span>apply plugin: 'kotlin-android'  

2、 
[plain] view plain copy
 print?
  1. <span style="white-space:pre">  </span>compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"  


3、
[plain] view plain copy
 print?
  1. android{  
  2.      ......  
  3.         sourceSets {  
  4.             main.java.srcDirs += 'src/main/kotlin'  
  5.         }  
  6.     }  



这样就可以正常使用了。

如果出现
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:

[plain] view plain copy
 print?
  1.     task clean(type: Exec) {  
  2.     ext.lockhunter = '\"C:\\LockHunter.exe\"'  
  3.     def buildDir = file(new File("build"))  
  4.     commandLine 'cmd', "$lockhunter", '/delete', '/silent', buildDir  
  5. }  




如果出现Unresolved reference: kotlinx这样的问题,那么需要在app目录下的build.gradle文件中添加:
apply plugin: 'kotlin-android-extensions'
以及要确保classpath配置了classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"







 
1
1
 

 

 
 
查看评论

  暂无评论

 
 
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
 
 
 
 
    个人资料
posted @ 2017-08-29 15:29  程序猿-北漂一族  阅读(482)  评论(0编辑  收藏  举报