4

I'm running instrumentation tests and getting this compile error:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformClassesAndResourcesWithR8ForDebugAndroidTest'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$3.accept(ExecuteActionsTaskExecuter.java:151)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$3.accept(ExecuteActionsTaskExecuter.java:148)
        at org.gradle.internal.Try$Failure.ifSuccessfulOrElse(Try.java:191)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:141)
        ...
Caused by: java.lang.RuntimeException: com.android.tools.r8.CompilationFailedException: Compilation failed to complete
        at com.android.builder.profile.Recorder$Block.handleException(Recorder.java:55)
        at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:108)
        at com.android.build.gradle.internal.pipeline.TransformTask.transform(TransformTask.java:230)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:103)
        at org.gradle.api.internal.project.taskfactory.IncrementalTaskInputsTaskAction.doExecute(IncrementalTaskInputsTaskAction.java:46)
        ...
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete
        at com.android.tools.r8.utils.z.a(:55)
        at com.android.tools.r8.R8.runForTesting(:3)
        at com.android.tools.r8.R8.run(:3)
        at com.android.builder.dexing.R8Tool.runR8(r8Tool.kt:195)

Caused by: com.android.tools.r8.utils.AbortException: Error: offset: 0, line: 16559, column: 1, 'void zza(com.google.android.gms.common.internal.BaseGmsClient,int,android.os.IInterface)' already has a mapping
        at com.android.tools.r8.utils.Reporter.a(:21)
        at com.android.tools.r8.naming.SeedMapper$a.build(:1)
        ...

If I look in app/build/outputs/mappings/debug/mapping.txt I see that method listed twice.

com.google.android.gms.common.internal.BaseGmsClient -> com.google.android.gms.common.internal.BaseGmsClient:
    ...
    344:344:void zza(com.google.android.gms.common.internal.BaseGmsClient,int,android.os.IInterface) -> zza
    ...
    350:350:void zza(com.google.android.gms.common.internal.BaseGmsClient,int,android.os.IInterface) -> zza
  1. Weirdly not happening on a basic app compile. Not sure why R8 is doing anything w/ test code.
  2. Is this an R8 issue or Play Services issue?
  3. How can I resolve this. A gradle clean + invalidate/restart didn't do anything, nor did manually deleting and regenerating the mappings.txt file.
tir38
  • 9,810
  • 10
  • 64
  • 107

2 Answers2

11

I may have some answers.

Regarding 1. If you compile you app with minifyEnabled set to true and run instrumentation tests afterwards, your app may have minified classes, methods etc. All tests therefore needs to be recompiled with R8 to have all minified names corrected in your tests. Concretely, you tests are being compiled by R8 with you app on library-path and a proguard configuration that says -applymapping .

Regarding 2. This is an R8 issue. You might have some luck by adding the following to your project level build.gradle file:

buildscript {

    repositories {
        maven {
            url 'http://storage.googleapis.com/r8-releases/raw'
        }
    }

    dependencies {
        classpath 'com.android.tools:r8:1.5.45'          // Must be before the Gradle Plugin for Android.
        classpath 'com.android.tools.build:gradle:X.Y.Z' // Your current AGP version.
     }
}

Regarding 3. If the fix in 2. is not working, you can follow the following bug at the R8 bug-tracker: https://issuetracker.google.com/issues/122924648

It may also be possible to live without instrumentation on the minified app until the issue is resolved.

MortenKJ
  • 296
  • 3
  • 3
  • I am pretty confused by the thread in the issue tracker, because it looks like the following things have happened: 1) It was marked "Won't Fix (Infeasible)" 2) There's a new issue to track it and it's marked as "Fixed": https://issuetracker.google.com/issues/140851070 But for me it's still not fixed. I'm not sure what to do other than disable R8 for my instrumented tests. – yuval Jan 11 '20 at 00:15
  • What version of R8 are you using and what is the error you are seeing? – MortenKJ Jan 12 '20 at 11:04
  • my version of R8 is 1.5.69 – yuval Jan 13 '20 at 19:07
  • As noted in https://issuetracker.google.com/issues/140851070#comment24, the suggestion is to use version 1.6.58. You could try out that version and report on the issue-tracker if that is not working for you. – MortenKJ Jan 15 '20 at 08:12
2

I have the exact same error and the suggestions from @MortenKJ didn't work for me.

My workaround is not very satisfying but it is the best I can do right now. Whenever I want to run instrumentation tests, I set minifyEnabled to false and minSdkVersion to 21 (for this sdk version multidex is enabled by default and there is no error that the dex method limit is reached).

Instrumentation tests run as expected now.

RobC
  • 22,977
  • 20
  • 73
  • 80