Android tests don't compile when Kotlin classes are referenced

1,191 views
Skip to first unread message

Kirill Rakhman

unread,
Oct 13, 2014, 12:25:31 PM10/13/14
to adt...@googlegroups.com
I'm following the guide over at http://kotlinlang.org/docs/reference/using-gradle.html and have successfully added the Kotlin plugin to my Android's application build script. I've added the kotlin folder to the app's source set like this:

    sourceSets {
        main
.java.srcDirs += 'src/main/kotlin'
   
}

And I can launch my app and use Kotlin classes without problems.

However, if I try to run the tests, the compilation fails with the following output:

JavaClassTest.java:14: error: package package.name does not exist
import package.name.KotlinClass;
                               
^
JavaClassTest.java:32: error: cannot find symbol
   
private KotlinClass getResult() {
           
^
  symbol
:   class KotlinClass
  location
: class JavaClassTest

When running with --debug flag, the folder "build\tmp\kotlin-classes\debugTest" seems to be part of the class path, but this folder doesn't exist. However the folder "build\tmp\kotlin-classes\debug" is there and contains all the necessary classes.

I would really appreciate some help, since I'm not the biggest Gradle pro. Thanks in advance.

Cross reference to StackOverflow: http://stackoverflow.com/questions/26340426/android-unit-test-of-kotlin-class-fails-with-cannot-find-symbol-class

Xavier Ducrohet

unread,
Oct 13, 2014, 1:41:20 PM10/13/14
to adt...@googlegroups.com
The android plugin for gradle isn't really compatible with other java-based plugin (for now).

This means that the kotlin plugin cannot plug into the tasks created by our plugins. You'd have to do a custom plugin for kotlin that works exclusively with our plugin.

--
You received this message because you are subscribed to the Google Groups "adt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adt-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Xavier Ducrohet
Android SDK Tech Lead
Google Inc.
http://developer.android.com | http://tools.android.com

Please do not send me questions directly. Thanks!

Kirill Rakhman

unread,
Oct 13, 2014, 6:14:44 PM10/13/14
to adt...@googlegroups.com
What I ended up doing was adding a custom task to copy the content of the debug folder into
debugTest and adding a dependency to the compileDebugTestJava task. The script looks something like this:

afterEvaluate { project ->
    project.tasks.findAll {
        it.name.startsWith('compile') && it.name.endsWith('DebugTestJava')
    }.each { Task task ->
        task.dependsOn copyKotlinToTest
    }
}

task copyKotlinToTest(type: Copy) {
    from new File(buildDir, 'tmp/kotlin-classes/debug')
    into new File(buildDir, 'tmp/kotlin-classes/debugTest')
}


--
You received this message because you are subscribed to a topic in the Google Groups "adt-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/adt-dev/LvlkgNlTvtA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adt-dev+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages