stacktrace-decoroutinator: Random crashes with java.lang.InternalError: Attempt to register dex file /data/user/0/../Anonymous-DexFile@1234.jar with multiple class loaders

Using stacktrace-decoroutinator in production will randomly crash the app at first attempt of using coroutines.

I have created a very small app that demonstrates it. See https://github.com/nordfalk/decoroutinator-app

After adding SD and a simple coroutine (https://github.com/nordfalk/decoroutinator-app/commit/613915a1737be032e66b7e1d80cf4ead07f98f86) the app crashes sometimes in pre-launch-report when uploading a signed app bundle to Google Play:

FATAL EXCEPTION: main
Process: dk.dinero.decoroutinatorapplication, PID: 20162
java.lang.InternalError: Attempt to register dex file /data/user/0/dk.dinero.decoroutinatorapplication/Anonymous-DexFile@188495102.jar with multiple class loaders
	at dalvik.system.DexFile.defineClassNative(Native Method)
	at dalvik.system.DexFile.defineClass(DexFile.java:298)
	at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:291)
	at dalvik.system.DexPathList$Element.findClass(DexPathList.java:772)
	at dalvik.system.DexPathList.findClass(DexPathList.java:532)
	at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:245)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
	at dev.reformator.stacktracedecoroutinator.runtime.DecoroutinatorRuntime.load(runtime-android.kt:53)
	at dev.reformator.stacktracedecoroutinator.runtime.DecoroutinatorRuntime.load$default(runtime-android.kt:26)
	at dk.dinero.decoroutinatorapplication.MyApp.<init>(MyApp.kt:8)
	at java.lang.Class.newInstance(Native Method)
	at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:76)
	at androidx.core.app.CoreComponentFactory.instantiateApplication(CoreComponentFactory.java:51)
	at android.app.Instrumentation.newApplication(Instrumentation.java:1232)
	at android.app.LoadedApk.makeApplicationInner(LoadedApk.java:1458)
	at android.app.LoadedApk.makeApplicationInner(LoadedApk.java:1395)

Additionally, in our production app (private source) Ive:

  • tried to strip all libraries, so that only SD remains.
  • analized the APK and removed all providers, services and receivers (tools:node=“remove”)
  • analized the bytecode for the initializer of the Application object and verified that DecoroutinatorRuntime.load() is absolutely the very first thing happening.
  • moved DecoroutinatorRuntime.load() to the class initializer of the Application object
  • verified that the bytecode for androidx.core.app.CoreComponentFactory has no influence - its instantiateApplication calls android.app.AppComponentFactory as the first thing, and so does the constructur
  • verified that the PID of the process is changing every time (so, no process reuse)

Ive tried during more than a month to get this to work without random crashes at startup. I cant find out why it happens but I must have verified on 2 competely different codebases that it happens.

This issue is related to https://github.com/Anamorphosee/stacktrace-decoroutinator/issues/29 .

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 25 (13 by maintainers)

Commits related to this issue

Most upvoted comments

some fixes were released in 2.4.5

They seems to have fixed the crashing! 🥳 I’ll roll out to a few hundred beta users and see what’ll happen.

some fixes were released in 2.4.5

@nordfalk version 2.4.4 was released. And since it ability to generate frames in runtime switching on by config:

stacktraceDecoroutinator {
    addAndroidRuntimeDependency = true
}

@nordfalk There’s an unknown frame in your last trace (dev.reformator.stacktracedecoroutinator.common.internal.UnknownKt.unknown(unknown.kt:20)). It appears when Decoroutinator cannot find a transformed class for a frame. Could you please add config below to your build.gradle.kts file:

stacktraceDecoroutinator {
    _isAndroid = true
    addGeneratorDependency = true
}

to allow Decoroutinator to generates frames at runtime and check if there are unknown frames any more?

Hi, I just wanted to say that Ive found a way to make it work!

I had:

plugins {
    id("com.android.application")
    id("com.google.gms.google-services")
    id("com.datadoghq.dd-sdk-android-gradle-plugin")
    id("kotlin-android")
    id("dev.reformator.stacktracedecoroutinator") version "2.4.3"
}

This worked in debug builds, but it didnt work in production builds - I didnt get good stack traces. Invoking DecoroutinatorCommonApi.getStatus { it() } crashed with:

java.lang.NoSuchMethodException: dev.reformator.stacktracedecoroutinator.common.DecoroutinatorCommonApi$getStatus$contunuation$1.getStatus [interface kotlin.coroutines.Continuation]
	at java.lang.Class.getMethod(Class.java:2103)
	at java.lang.Class.getDeclaredMethod(Class.java:2081)
	at dev.reformator.stacktracedecoroutinator.common.DecoroutinatorCommonApi$getStatus$contunuation$1.b(api-common.kt:13)
	at dev.reformator.stacktracedecoroutinator.common.DecoroutinatorCommonApi.a(api-common.kt:26)

After I added -keep class dev.reformator.stacktracedecoroutinator.** { *; } to proguard-rules.pro it gave the output: DecoroutinatorStatus(successful=false, description=class [kotlin.coroutines.jvm.internal.BaseContinuationImpl] from Kotlin stdlib is not transformed)

BUT

When I change to

plugins {
    id("dev.reformator.stacktracedecoroutinator") version "2.4.3"
    id("com.android.application")
    id("com.google.gms.google-services")
    id("com.datadoghq.dd-sdk-android-gradle-plugin")
    id("kotlin-android")
}

then it works! 🥳 🥳 🥳

Sorry. My bad. Didn’t change version in one place. Please try version 2.4.1

@nordfalk Just released SD 2.4.0 with the Gradle plugin.

Cool! I tried and in my test project I can use https://s01.oss.sonatype.org/content/repositories/snapshots/ but for some reason I fail to do it in my real project.

I have

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        kotlin_version = "1.9.0"
        navigation_version = "2.7.6"
        compose_compiler = "1.5.1"
    }

    repositories {
        google()
        mavenCentral()
        maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.2.2'
        classpath "com.google.gms:google-services:4.3.15"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigation_version"
        classpath("com.datadoghq:dd-sdk-android-gradle-plugin:1.9.0")
    }
}

plugins {
    id("com.google.devtools.ksp") version "1.9.0-1.0.13" apply false
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url 'https://s3.amazonaws.com/tgl.maven' }
        maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots' }
    }

but when I edit in app/gradle.build to add stacktracedecoroutinator

plugins {
    id("com.android.application")
    id("com.google.gms.google-services")
    id("com.datadoghq.dd-sdk-android-gradle-plugin")
    id("kotlin-android")
    id("dev.reformator.stacktracedecoroutinator") version "2.4.0-SNAPSHOT"
}

I get this error:


The client will now receive all logging from the daemon (pid: 371171). The daemon log file: /home/j/.gradle/daemon/8.2/daemon-371171.out.log
Starting 14th build in daemon [uptime: 38 mins 51.36 secs, performance: 99%, GC rate: 0.00/s, heap usage: 0% of 4 GiB]
Closing daemon's stdin at end of input.
The daemon will no longer process any standard input.
Using 16 worker leases.
Now considering [/home/j/dinero-android-app3, /home/j/dinero-android-app] as hierarchies to watch
Watching the file system is configured to be enabled if available
File system watching is active
The configuration classpath is both resolvable and consumable. This is considered a legacy configuration and it will eventually only be possible to be one of these.
The configuration classpath is both consumable and declarable. This combination is incorrect, only one of these flags should be set.
The configuration classpath is both resolvable and consumable. This is considered a legacy configuration and it will eventually only be possible to be one of these.
The configuration classpath is both consumable and declarable. This combination is incorrect, only one of these flags should be set.
The configuration classpath is both resolvable and consumable. This is considered a legacy configuration and it will eventually only be possible to be one of these.
The configuration classpath is both consumable and declarable. This combination is incorrect, only one of these flags should be set.
The configuration classpath is both resolvable and consumable. This is considered a legacy configuration and it will eventually only be possible to be one of these.
The configuration classpath is both consumable and declarable. This combination is incorrect, only one of these flags should be set.
Starting Build
Settings evaluated using settings file '/home/j/dinero-android-app3/settings.gradle'.
Using local directory build cache for the root build (location = /home/j/.gradle/caches/build-cache-1, removeUnusedEntriesAfter = 7 days).
Projects loaded. Root project using build file '/home/j/dinero-android-app3/build.gradle'.
Included projects: [root project 'dinero-android-app3', project ':app', project ':appfeature_assistant', project ':appfeature_bookkeeping', project ':appfeature_insights', project ':appfeature_invoice', project ':base', project ':util']

> Configure project :
Evaluating root project 'dinero-android-app3' using build file '/home/j/dinero-android-app3/build.gradle'.
The configuration detachedConfiguration1 is both resolvable and consumable. This is considered a legacy configuration and it will eventually only be possible to be one of these.
The configuration detachedConfiguration1 is both consumable and declarable. This combination is incorrect, only one of these flags should be set.
The configuration detachedConfiguration1 is both resolvable and consumable. This is considered a legacy configuration and it will eventually only be possible to be one of these.
The configuration detachedConfiguration1 is both consumable and declarable. This combination is incorrect, only one of these flags should be set.
The configuration :classpath is both resolvable and consumable. This is considered a legacy configuration and it will eventually only be possible to be one of these.
The configuration :classpath is both consumable and declarable. This combination is incorrect, only one of these flags should be set.
The configuration :classpath is both resolvable and consumable. This is considered a legacy configuration and it will eventually only be possible to be one of these.
The configuration :classpath is both consumable and declarable. This combination is incorrect, only one of these flags should be set.

> Configure project :app
Evaluating project ':app' using build file '/home/j/dinero-android-app3/app/build.gradle'.
The configuration detachedConfiguration1 is both resolvable and consumable. This is considered a legacy configuration and it will eventually only be possible to be one of these.
The configuration detachedConfiguration1 is both consumable and declarable. This combination is incorrect, only one of these flags should be set.
The configuration detachedConfiguration1 is both resolvable and consumable. This is considered a legacy configuration and it will eventually only be possible to be one of these.
The configuration detachedConfiguration1 is both consumable and declarable. This combination is incorrect, only one of these flags should be set.
Resource missing. [HTTP GET: https://repo.maven.apache.org/maven2/dev/reformator/stacktracedecoroutinator/dev.reformator.stacktracedecoroutinator.gradle.plugin/2.4.0-SNAPSHOT/maven-metadata.xml]
Resource missing. [HTTP HEAD: https://repo.maven.apache.org/maven2/dev/reformator/stacktracedecoroutinator/dev.reformator.stacktracedecoroutinator.gradle.plugin/2.4.0-SNAPSHOT/dev.reformator.stacktracedecoroutinator.gradle.plugin-2.4.0-SNAPSHOT.pom]

FAILURE: Build failed with an exception.

* Where:
Build file '/home/j/dinero-android-app3/app/build.gradle' line: 6

* What went wrong:
Plugin [id: 'dev.reformator.stacktracedecoroutinator', version: '2.4.0-SNAPSHOT'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'dev.reformator.stacktracedecoroutinator:dev.reformator.stacktracedecoroutinator.gradle.plugin:2.4.0-SNAPSHOT')
  Searched in the following repositories:
    Gradle Central Plugin Repository

But it seems that your non-snapshot build works fine - this builds:

plugins {
    id("com.android.application")
    id("com.google.gms.google-services")
    id("com.datadoghq.dd-sdk-android-gradle-plugin")
    id("kotlin-android")
    id("dev.reformator.stacktracedecoroutinator")
}