Skip to content

Commit 8ec1a5c

Browse files
committed
Revert workaround with Class.forName and embed proguard rules to JAR resources instead
1 parent cb3c105 commit 8ec1a5c

File tree

6 files changed

+15
-27
lines changed

6 files changed

+15
-27
lines changed

README.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,8 @@ coroutine dispatcher and also makes sure that in case of crashed coroutine with
129129
exception is logged before crashing Android application, similarly to the way uncaught exceptions in
130130
threads are handled by Android runtime.
131131

132-
### ProGuard
133-
134-
In obfuscated code, fields with different types can have the same names,
135-
and `AtomicReferenceFieldUpdater` may be unable to find the correct ones.
136-
To avoid field overloading by type during obfuscation, add this to your config:
137-
138-
```
139-
-keepclassmembernames class kotlinx.** {
140-
volatile <fields>;
141-
}
142-
```
132+
### R8 and ProGuard
133+
If you are using R8 or ProGuard add the options from [coroutines.pro](core/kotlinx-coroutines-core/resources/META-INF/proguard/coroutines.pro) file to your rules.
143134

144135
## Building
145136

common/kotlinx-coroutines-core-common/src/CoroutineExceptionHandler.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
package kotlinx.coroutines
66

7-
import kotlinx.coroutines.internal.*
87
import kotlin.coroutines.*
98

109
internal expect fun handleCoroutineExceptionImpl(context: CoroutineContext, exception: Throwable)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# ServiceLoader support
2+
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
3+
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
4+
5+
# Most of volatile fields are updated with AFU and should not be mangled
6+
-keepclassmembernames class kotlinx.** {
7+
volatile <fields>;
8+
}

core/kotlinx-coroutines-core/src/Dispatchers.kt

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,7 @@ private object MainDispatcherLoader {
9494
val dispatcher: MainCoroutineDispatcher =
9595
MainDispatcherFactory::class.java.let { clz ->
9696
ServiceLoader.load(clz, clz.classLoader).toList()
97-
}.maxBy { it.loadPriority }?.tryCreateDispatcher()
98-
?: (tryLoadAndroidDispatcher() ?: MissingMainCoroutineDispatcher(null))
99-
100-
private fun tryLoadAndroidDispatcher(): MainCoroutineDispatcher? {
101-
/*
102-
* Latest Android toolchain (androidx) *sometimes* mangles the name of the service loaded by ServiceLoader even if
103-
* it is present in the manifest. To workaround it (we don't want our users to suffer) we optimistically
104-
* try to load android factory manually (implementation is not mangled because it is marked with @Keep)
105-
*/
106-
val dispatcher = kotlin.runCatching {
107-
val clazz = Class.forName("kotlinx.coroutines.android.AndroidDispatcherFactory")
108-
clazz.getMethod("getDispatcher")?.invoke(null) as MainCoroutineDispatcher
109-
}
110-
return dispatcher.getOrNull()
111-
}
97+
}.maxBy { it.loadPriority }?.tryCreateDispatcher() ?: MissingMainCoroutineDispatcher(null)
11298

11399
/**
114100
* If anything goes wrong while trying to create main dispatcher (class not found,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
2+
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
13
-keepclassmembernames class kotlinx.** {
24
volatile <fields>;
35
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
2+
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
13
-keepclassmembernames class kotlinx.** {
24
volatile <fields>;
35
}

0 commit comments

Comments
 (0)