Kotlin 中文文档 

如何在Android studio中使用KotLin

 

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

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文件中添加简单的设置就好

看示例:

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、

1. <span style="white-space:pre">  </span>apply plugin: 'kotlin-android'

2、 

1. <span style="white-space:pre">  </span>compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"


3、

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:

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"