Starting with Kotlin in Android Studio

Hi,

I am sorry for being such a java/intelij/gradle/maven noob. I have a small java android project that I imported into Android Studio (from Eclipse). It compiled and ran OK.

Now I wanted to add kotlin to it. I followed the blogpost.

  1. I installed the Kotlin plugin
  2. created folder kotlin in main
  3. edited the 2 build.gradle files, one is inside app, another on the project level

project/

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

buildscript {

  repositories {

  mavenCentral()

  }

  dependencies {

  classpath ‘org.jetbrains.kotlin:kotlin-gradle-plugin:0.6.1673’

  classpath ‘org.jetbrains.kotlin:kotlin-gradle-plugin-core:0.6.1673’

  classpath ‘com.android.tools.build:gradle:0.7.+’

  }

}

allprojects {

  repositories {

  mavenCentral()

  }

}


app/

apply plugin: ‘android’

apply plugin: ‘kotlin-android’

dependencies {

  compile 'org.jetbrains.kotlin:kotlin-stdlib:0.6.1673'

  compile 'com.android.support:support-v4:+'

}

android {

  compileSdkVersion 19

  buildToolsVersion "19.0.1"

  defaultConfig {

  minSdkVersion 10

  targetSdkVersion 17

  }

  buildTypes {

  release {

           runProguard false

           proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'

  }

  }

}

---

First I got errors about missing kotlin-gradle-plugin-core . I tried different versions or just 6+ but the error stayed the same. I added it amongst dependencies and I got to (hopefully) next problem:


/home/jimny/AndroidStudioProjects/CebelcaPOS/app/build.gradle

A problem occurred evaluating project ':app'. > com.google.common.base.Objects.toStringHelper(Ljava/lang/Object;)Lcom/google/common/base/Objects$ToStringHelper; ---

Which I have no idea about. Can anyone help me, I really like what Kotlin language offers and I am not totally inexperienced developer otherwise, but I had little contact with whole JVM stack.

Best regards,
Janko

Hi, It looks like a bug in your kotlin-android plugin, we don't add all dependencies for subprojects (I've created an issue http://youtrack.jetbrains.com/issue/KT-4527)

As a workaround add the following lines in your /app build.gradle

buildscript {
  repositories {
  mavenCentral()
  }
}

It should help you with both exceptions.

What about configuration of android kotlin project with gradle: you also should add ‘kotlin’ directory as a srcDir

android {
  sourceSets {
  main.java.srcDirs += ‘src/main/kotlin’
  }
}


            

wow, thanks Natalia, it works now! (I intentionally removed sourceSets, trying to figure out minimise things that could cause the error)

Now I need to do some more detailed reading on the language specifics for the next steps.

//edit:
I already changed few small (record) classes to Kotlin while majority still in Java and it all works … GREAT! and code repetition is already reduces :slight_smile: