Skip to content

Commit aba0edc

Browse files
authored
Add workaround for androidx toolchain issues: try to load main dispat… (#709)
Embed proguard rules to JAR resources to workaround Androidx issue with ServiceLoader, update documentation Fixes #657
1 parent b058ba1 commit aba0edc

File tree

7 files changed

+20
-13
lines changed

7 files changed

+20
-13
lines changed

README.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,9 @@ 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
132+
### R8 and ProGuard
133133

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-
```
134+
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.
143135

144136
## Building
145137

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ package kotlinx.coroutines
88

99
import kotlinx.coroutines.internal.*
1010
import kotlinx.coroutines.scheduling.*
11-
import kotlin.coroutines.*
1211
import java.util.*
12+
import kotlin.coroutines.*
1313

1414
/**
1515
* Name of the property that defines the maximal number of threads that are used by [Dispatchers.IO] coroutines dispatcher.
@@ -20,7 +20,6 @@ public const val IO_PARALLELISM_PROPERTY_NAME = "kotlinx.coroutines.io.paralleli
2020
* Groups various implementations of [CoroutineDispatcher].
2121
*/
2222
public actual object Dispatchers {
23-
2423
/**
2524
* The default [CoroutineDispatcher] that is used by all standard builders like
2625
* [launch][CoroutineScope.launch], [async][CoroutineScope.async], etc
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
}

ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public sealed class HandlerDispatcher : MainCoroutineDispatcher(), Delay {
3333

3434
@Keep
3535
internal class AndroidDispatcherFactory : MainDispatcherFactory {
36+
companion object {
37+
@JvmStatic // accessed reflectively from core
38+
fun getDispatcher(): MainCoroutineDispatcher = Main
39+
}
40+
3641
override fun createDispatcher(): MainCoroutineDispatcher = Main
3742

3843
override val loadPriority: Int

0 commit comments

Comments
 (0)