@@ -15,6 +15,23 @@ import java.security.*
15
15
@SuppressLint(" all" )
16
16
@IgnoreJRERequirement // Never touched on Android
17
17
internal object AgentPremain {
18
+ // This should be the first property to ensure the check happens first! Add new properties only below!
19
+ private val dummy = checkIfStdlibIsAvailable()
20
+
21
+ /* *
22
+ * This check ensures that kotlin-stdlib classes are loaded by the time AgentPremain is initialized;
23
+ * otherwise the debug session would fail with `java.lang.NoClassDefFoundError: kotlin/Result`.
24
+ */
25
+ private fun checkIfStdlibIsAvailable () {
26
+ try {
27
+ Result .success(42 )
28
+ } catch (t: Throwable ) {
29
+ error(" kotlinx.coroutines debug agent failed to load.\n " +
30
+ " Please ensure that the Kotlin standard library is present in the classpath.\n " +
31
+ " Alternatively, you can disable kotlinx.coroutines debug agent by removing `-javaagent=/path/kotlinx-coroutines-core.jar` from your VM arguments.\n " +
32
+ t.cause)
33
+ }
34
+ }
18
35
19
36
private val enableCreationStackTraces = runCatching {
20
37
System .getProperty(" kotlinx.coroutines.debug.enable.creation.stack.trace" )?.toBoolean()
@@ -69,25 +86,3 @@ internal object AgentPremain {
69
86
}
70
87
}
71
88
}
72
-
73
- /* *
74
- * This class is loaded if and only if kotlinx-coroutines-core was used as a javaagent argument.
75
- * It serves as a "safe" wrapper around [AgentPremain] that does not require any kotlin-stdlib classes to be loaded for its initialization.
76
- *
77
- * Initialization of [AgentPremain] may fail, e.g., because it started before the required kotlin-stdlib classes were loaded.
78
- * If `kotlinx.coroutines.apply.debug.agent` is set to `false`, the coroutine debug agent will not be applied.
79
- */
80
- @Suppress(" unused" )
81
- internal object SafeAgentPremain {
82
- @JvmStatic
83
- fun premain (args : String? , instrumentation : Instrumentation ) {
84
- try {
85
- AgentPremain .premain(args, instrumentation)
86
- } catch (t: Throwable ) {
87
- error(" Something went wrong while loading kotlinx.coroutines debug agent.\n " +
88
- " Please ensure that Kotlin Stdlib is present in the classpath.\n " +
89
- " Alternatively, to disable coroutines debug agent you can remove `-javaagent=/path/kotlinx-coroutines-core.jar` from your VM arguments.\n " +
90
- t.cause)
91
- }
92
- }
93
- }
0 commit comments