29

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?

Michał Klimczak
  • 12,674
  • 8
  • 66
  • 99
  • 8
    No, Android's dex format does not support spaces in method names. – nhaarman Sep 19 '17 at 08:25
  • You should submit an answer so that this question can be closed, @nhaarman. :-) – sindrenm Jan 12 '18 at 15:19
  • I am facing the same issue. Is there a solution for this? – IgorGanapolsky May 08 '18 at 19:54
  • 1
    @IgorGanapolsky - this is still not supported. It is the Android runtime, no solution from the developer perspective. Sadly you need to have 2 different naming conventions - one for unit tests and one for Android tests or just not use backticks in all the tests to be consistent. – Yoel Gluschnaider Nov 01 '19 at 15:28
  • Luckily there's **AndroidX Test** now, which omits the need to have two separate test directories :) – IgorGanapolsky Nov 01 '19 at 17:48

2 Answers2

18

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() { ... }
}
Matthew R.
  • 615
  • 4
  • 12
  • Why does Swift support it? – IgorGanapolsky Nov 01 '19 at 17:48
  • What does Swift have to do with this @IgorGanapolsky – Dallas Nov 09 '22 at 18:20
  • @Dallas They are both very similar languages and compete for features. – IgorGanapolsky Nov 10 '22 at 14:14
  • 2
    Android and Swift compete for features? They're not even in the same ballpark of functionality. Android is a platform and Swift is a programming language. The whole issue here--as originally (but no longer) mentioned in the referenced docs--is that the DEX format didn't support various characters in function names. Android has since fixed that. Kotlin already supported it and was providing only a note for folks trying to do this with Android runtime tests. – Dallas Nov 10 '22 at 18:16
18

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.

Cristan
  • 12,083
  • 7
  • 65
  • 69
  • 3
    It does work on 30+ but there are some tradeoffs. Android Studio 4.1.2 underlines as error those methods (it compiles fine). Also, it does not work when Orchestrator is enabled - it compiles but it hangs on instantiating tests. Nevertheless we're getting closer! :) – rafakob Feb 23 '21 at 13:42
  • To fix the underlined error, add `@file:Suppress("IllegalIdentifier")` to the top of the file. – Ben Butterworth Mar 03 '21 at 17:11
  • At Android Studio Chipmunk, the error goes away when you set minSdkVersion to 30 or higher. Ticket: https://issuetracker.google.com/issues/208842981 – Cristan Jan 12 '22 at 10:16
  • Some recent changes in how Orchestrator runs tests might have fixed this issue. Can someone confirm? – Jose Alcérreca Sep 02 '22 at 08:30
  • Update: Backticks seem to work with Orchestrator. Documentation was updated https://developer.android.com/training/testing/instrumented-tests – Jose Alcérreca Sep 15 '22 at 10:48