Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a new test folder called Kotlin for android studio

Android Studio 3.1 Canary 4
Build #AI-171.4444016, built on November 10, 2017
JRE: 1.8.0_152-release-1012-b01 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.13.16-302.fc27.x86_64

Hello,

I have the following project structure. However, as I have some kotlin files I want to create a new folder called kotlin under test and create a new package and store all my kotlin files there.

Currently I have one called Java, but I want to create the kotlin, but can't seem to be able to find out how to do it.

I would like to do the same for androidTest as well, so I can separate all my java and kotlin files.

enter image description here

like image 633
ant2009 Avatar asked Dec 07 '17 15:12

ant2009


2 Answers

Would like to give credit to denvercoder9 who posted the link to the proandoriddev.com article which solved my problem.

In my build.gradle file I have the following which worked for both the test and androidTest folders. However, I needed to create the folders first.

sourceSets {
    main { java.srcDirs = ['src/main/java', 'src/main/kotlin'] }
    test.java.srcDirs += 'src/test/kotlin'
    androidTest.java.srcDirs += 'src/androidTest/kotlin'
}

If anyone need any help on this you can reply back to this solution.

like image 104
ant2009 Avatar answered Sep 19 '22 11:09

ant2009


  • Android Testing with Kotlin

Basically, the idea is to showcase how we can test our android applications using Kotlin, so as a first step we need to setup and prepare our environment by adding Kotlin dependencies in our build.gradle file:

buildscript {
  repositories {
    mavenCentral()
    jcenter()
  }
  dependencies {
    classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.5-2'
  }
}

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

...

dependencies {
  ...
  compile "org.jetbrains.kotlin:kotlin-stdlib:1.0.6"

  ...
  testCompile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.6'
  testCompile 'org.jetbrains.kotlin:kotlin-test-junit:1.0.6'
  testCompile "com.nhaarman:mockito-kotlin:1.1.0"
  testCompile 'org.amshove.kluent:kluent:1.14'
}

Now we need to set the dedicated directories for tests written in Kotlin, this is done in our sourceSets section:

android {
  ...
  sourceSets {
    test.java.srcDirs += 'src/test/kotlin'
    androidTest.java.srcDirs += 'src/androidTest/kotlin'
  }
  ...
}
  • This tutorials walks us through the process of using Java and Kotlin in a single IDEA project.
like image 20
stefan Avatar answered Sep 18 '22 11:09

stefan