Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple transformed artifacts selected #15536

Closed
gavra0 opened this issue Dec 13, 2020 · 14 comments
Closed

Multiple transformed artifacts selected #15536

gavra0 opened this issue Dec 13, 2020 · 14 comments

Comments

@gavra0
Copy link
Contributor

gavra0 commented Dec 13, 2020

Using duplicates.zip project and running ./gradlew :app:mergeExtDexDebugAndroidTest :app:mergeExtDexDebug (it is important to run tasks in the same Gradle invocation) fails with Gradle 6.7+ because debug runtime classpath contains unexpected artifacts. This succeeds with Gradle 6.6.1.

When building debugAndroidTest variant, we do the following:

  • resolve debugAndroidTest runtime classpath with artifactType=android-dex, this is A
  • resolve debug runtime classpath with artifactType=android-dex, this is B
  • final result is all files in A that are not found in B (this is needed in order to avoid duplicate classes in test and main APK)

Because desugaring is enabled, some artifacts e.g lifecycle-livedata-core-2.2.0 will be dexed twice. Two output artifacts will be produced, as dependency graphs for debug and debugAndroidTest differ. However, this should only result in having lifecycle-livedata-core-2.2.0 DEX in both APKs, and it should not result in having these two versions in the debug runtime classpath.

Workarounds include:

  • running tasks in separate Gradle invocations
  • disabling dex desugaring by increasing minSdkVersion in app/build.gradle to 24+
@adammurdoch
Copy link
Member

Should be fixed now via #15637

@adammurdoch adammurdoch added this to the 7.0 RC1 milestone Dec 22, 2020
@manask88
Copy link

manask88 commented Jan 4, 2021

@adammurdoch , thanks for fixing this bug. I'm wondering if it would be possible to cherry pick this into 6.8 . In case it cannot be cherry picked, wondering when will the next RC be cut. Thanks!

@benjaminRomano
Copy link

+1 to cherrypicking into 6.8. For us, upgrading to 7.0 is going to take a while as we need to audit and update all our gradle plugins to remove usages of deprecated features.

@Onlinedispatcher
Copy link

Same here, please backport

@davidburstromspotify
Copy link

We also ran into this issue just now. We'd also appreciate a backport, if possible.

@alaershov
Copy link

alaershov commented Apr 2, 2021

Also seeing this in production, please backport this to 6.8.
7.0 is pretty far, and all plugins supporting it is even further. We have some workarounds:

  1. don't build two APKs in the same Gradle command
tasks.named('mergeExtDexDebugAndroidTest').configure {
    mustRunAfter tasks.named('mergeExtDexDebug')
}

but it would be much better to have a fix it in Gradle.

@opatry
Copy link

opatry commented Apr 6, 2021

I can reproduce with 6.8.3 (seems ok on 7.0 RC2 + AGP 7.0.0-alpha09) (my repro project if it can help https://github.com/opatry/desugaring-issue)

Do you confirm it's not part of 6.8?

Can't make @alaershov's 2nd suggestion work though :(

@archfz
Copy link

archfz commented Oct 5, 2021

I have wasted simply too much time on this. If anyone else searching for this and wondering where @alaershov's 2nd suggestion should be placed, see here https://github.com/alaershov/android-merge-ext-dex-bug-sample/blob/master/app/build.gradle#L75

Or more specifically add at the end in app/build.gradle the following:

// @TODO: See https://github.com/gradle/gradle/issues/15536
gradle.projectsEvaluated {
   tasks.named('mergeExtDexDebugAndroidTest').configure {
       mustRunAfter tasks.named('mergeExtDexDebug')
   }
}

@archfz
Copy link

archfz commented Oct 8, 2021

I think this is more complete. The build was working locally in debug mode but not in release mode on pipeline.

// @TODO: See https://github.com/gradle/gradle/issues/15536
gradle.projectsEvaluated {
   if (tasks.findByName('mergeExtDexDebugAndroidTest')) {
       // In case of building assembleAndroidTest debug.
       tasks.named('mergeExtDexDebugAndroidTest').configure {
           mustRunAfter tasks.named('mergeExtDexDebug')
       }
   } else {
       // In case of building assembleAndroidTest release.
       tasks.named('mergeExtDexDebug').configure {
           mustRunAfter tasks.named('mergeExtDexDebugAndroidTest')
       }
   }
}

@archfz archfz mentioned this issue Oct 9, 2021
13 tasks
@juddey
Copy link

juddey commented Oct 12, 2021

Confirming the fix for those of you stuck on 6.x. Good work @archfz 👍
In my case the task names were slightly different, but still the same result.

Here's the diff FYI:

diff --git a/android/app/build.gradle b/android/app/build.gradle
index 8c79e9b..f3c7423 100644
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -284,3 +284,18 @@ task copyDownloadableDepsToLibs(type: Copy) {
 }
 
 apply plugin: 'com.google.gms.google-services'
+
+gradle.projectsEvaluated {
+   if (tasks.findByName('assembleAndroidTest')) {
+       // In case of building assembleAndroidTest debug.
+       tasks.named('assembleAndroidTest').configure {
+           mustRunAfter tasks.named('mergeExtDexRelease')
+       }
+   } else {
+       // In case of building assembleAndroidTest release.
+       tasks.named('mergeExtDexRelease').configure {
+           mustRunAfter tasks.named('assembleAndroidTest')
+       }
+   }
+}
+
diff --git a/android/build.gradle b/android/build.gradle
index c4173a1..9102fca 100644
--- a/android/build.gradle
+++ b/android/build.gradle
@@ -19,7 +19,7 @@ buildscript {
         jcenter()
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:4.1.0'
+        classpath 'com.android.tools.build:gradle:4.2.0'
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
         classpath 'com.google.gms:google-services:4.0.0'
         // NOTE: Do not place your application dependencies here; they belong
diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties
index 0ce4d3c..2837ece 100644
--- a/android/gradle/wrapper/gradle-wrapper.properties
+++ b/android/gradle/wrapper/gradle-wrapper.properties
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.9-all.zip

@archfz
Copy link

archfz commented Oct 13, 2021

Actually the fix is not working consistently for me, we do have both build debug and release for android test, on pipeline it's stiff failing and I am having a very difficult time understanding the issue. That is why I opted to cherry pick the fix from 7.x, lets hope gradle team releases it soon into 6.x

@mrbrentkelly
Copy link

// @TODO: See https://github.com/gradle/gradle/issues/15536
gradle.projectsEvaluated {
   if (tasks.findByName('mergeExtDexDebugAndroidTest')) {
       // In case of building assembleAndroidTest debug.
       tasks.named('mergeExtDexDebugAndroidTest').configure {
           mustRunAfter tasks.named('mergeExtDexDebug')
       }
   } else {
       // In case of building assembleAndroidTest release.
       tasks.named('mergeExtDexDebug').configure {
           mustRunAfter tasks.named('mergeExtDexDebugAndroidTest')
       }
   }
}

@archfz should the else branch be using mergeExtDexRelease instead of debug to cover release builds?

@archfz
Copy link

archfz commented Oct 23, 2021

@mrbrentkelly Indeed something like that would sound logical, but this is my current version and I still have issues.

// @TODO: See https://github.com/gradle/gradle/issues/15536
gradle.projectsEvaluated {
   if (tasks.findByName('mergeExtDexDebugAndroidTest')) {
       if (tasks.findByName('mergeExtDexDebug')) {
           tasks.named('mergeExtDexDebugAndroidTest').configure {
               mustRunAfter tasks.named('mergeExtDexDebug')
           }
       }
   }

   if (tasks.findByName('mergeExtDexReleaseAndroidTest')) {
        if (tasks.findByName('mergeExtDexDebug')) {
            tasks.named('mergeExtDexReleaseAndroidTest').configure {
                mustRunAfter tasks.named('mergeExtDexDebug')
            }
        }
//         if (tasks.findByName('mergeExtDexRelease')) {
//             tasks.named('mergeExtDexReleaseAndroidTest').configure {
//                 mustRunAfter tasks.named('mergeExtDexRelease')
//             }
//         }
   }
}

@mrbrentkelly
Copy link

Thanks @archfz 👍

I've tried a few different things and this is what appears to be working best for me...

gradle.projectsEvaluated {
    if (tasks.findByName('mergeExtDexRelease') && tasks.findByName('mergeExtDexReleaseAndroidTest')) {
        tasks.named('mergeExtDexRelease').configure {
            mustRunAfter tasks.named('mergeExtDexReleaseAndroidTest')
        }
    }

    if (tasks.findByName('mergeExtDexDebugAndroidTest') && tasks.findByName('mergeExtDexDebug')) {
        tasks.named('mergeExtDexDebugAndroidTest').configure {
            mustRunAfter tasks.named('mergeExtDexDebug')
        }
    }
}

achou11 added a commit to digidem/mapeo-mobile that referenced this issue Jan 27, 2022
Suspicion is that 6.9.0 contains a bug causing one of our CI steps to
fail sporadically (e.g. https://github.com/digidem/mapeo-mobile/runs/4968297075?check_suite_focus=true). Potentially relevant issue was reported (gradle/gradle#15536) and apparently has been addressed (gradle/gradle#18572). According to gradle/gradle#18572 (review), looks like the fix was backported to 6.9.2
achou11 added a commit to digidem/mapeo-mobile that referenced this issue Jan 27, 2022
Suspicion is that 6.9.0 contains a bug causing one of our CI steps to
fail sporadically (e.g. https://github.com/digidem/mapeo-mobile/runs/4968297075?check_suite_focus=true). Potentially relevant issue was reported (gradle/gradle#15536) and apparently has been addressed (gradle/gradle#18572). According to gradle/gradle#18572 (review), looks like the fix was backported to 6.9.2
achou11 added a commit to digidem/mapeo-mobile that referenced this issue Jan 27, 2022
Suspicion is that 6.9.0 contains a bug causing one of our CI steps to fail sporadically (e.g. https://github.com/digidem/mapeo-mobile/runs/4968297075?check_suite_focus=true). Potentially relevant issue was reported (gradle/gradle#15536) and apparently has been addressed (gradle/gradle#18572). According to gradle/gradle#18572 (review), looks like the fix was backported to 6.9.2
karlhorky added a commit to upleveled/hotline-bling-codealong that referenced this issue Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests