Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use Kotlin backticked method names in androidTest - bad descriptor exception

In my unit tests I use Kotlin's backticked methods for better readability, e.g.

@Test fun `Foo should return bar`()

It works nice and well for tests in <module>/src/test directory, but when I try to do the same in <module>/src/androidTest I get an exception:

Error:java.lang.IllegalArgumentException: bad descriptor: Lcom/packageName/MainActivityTest$Foo should return bar$1;
Error:Execution failed for task ':sample:transformClassesWithDexBuilderForDebugAndroidTest'. > com.android.build.api.transform.TransformException: org.gradle.tooling.BuildException: com.android.dx.cf.iface.ParseException: bad descriptor: Lcom/packageName/MainActivityTest$Foo should return bar$1;

Is there some trick to make it work?

like image 914
Michał Klimczak Avatar asked Sep 19 '17 08:09

Michał Klimczak


2 Answers

As @nhaarman mentioned, Android does not support backticked function names. From the Kotlin Coding Conventions:

In tests (and only in tests), it's acceptable to use method names with spaces enclosed in backticks. (Note that such method names are currently not supported by the Android runtime.) Underscores in method names are also allowed in test code.

class MyTestCase {
    @Test fun `ensure everything works`() { ... }
    @Test fun ensureEverythingWorks_onAndroid() { ... }
}
like image 166
Matthew R. Avatar answered Oct 23 '22 13:10

Matthew R.


Support for spaces in function names has been added and is now available in API 30.

To use it, set buildToolsVersion, compileSdkVersion and targetSdkVersion to 30+ and run your tests on an Android 30+ device. If you want to use this in anything else than tests, you'll have to set minSdkVersion to 30+ as well.

like image 14
Cristan Avatar answered Oct 23 '22 11:10

Cristan