Skip to content

Add workaround for androidx toolchain issues: try to load main dispat… #709

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

Merged
merged 3 commits into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,8 @@ coroutine dispatcher and also makes sure that in case of crashed coroutine with
exception is logged before crashing Android application, similarly to the way uncaught exceptions in
threads are handled by Android runtime.

### ProGuard

In obfuscated code, fields with different types can have the same names,
and `AtomicReferenceFieldUpdater` may be unable to find the correct ones.
To avoid field overloading by type during obfuscation, add this to your config:

```
-keepclassmembernames class kotlinx.** {
volatile <fields>;
}
```
### R8 and ProGuard
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style note: let's put an empty line after titles (it reads better in source, since it is actually a separate paragraph).


## Building

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package kotlinx.coroutines

import kotlinx.coroutines.internal.*
import kotlin.coroutines.*

internal expect fun handleCoroutineExceptionImpl(context: CoroutineContext, exception: Throwable)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# ServiceLoader support
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}

# Most of volatile fields are updated with AFU and should not be mangled
-keepclassmembernames class kotlinx.** {
volatile <fields>;
}
3 changes: 1 addition & 2 deletions core/kotlinx-coroutines-core/src/Dispatchers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ package kotlinx.coroutines

import kotlinx.coroutines.internal.*
import kotlinx.coroutines.scheduling.*
import kotlin.coroutines.*
import java.util.*
import kotlin.coroutines.*

/**
* Name of the property that defines the maximal number of threads that are used by [Dispatchers.IO] coroutines dispatcher.
Expand All @@ -20,7 +20,6 @@ public const val IO_PARALLELISM_PROPERTY_NAME = "kotlinx.coroutines.io.paralleli
* Groups various implementations of [CoroutineDispatcher].
*/
public actual object Dispatchers {

/**
* The default [CoroutineDispatcher] that is used by all standard builders like
* [launch][CoroutineScope.launch], [async][CoroutineScope.async], etc
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
-keepclassmembernames class kotlinx.** {
volatile <fields>;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
-keepclassmembernames class kotlinx.** {
volatile <fields>;
}
5 changes: 5 additions & 0 deletions ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public sealed class HandlerDispatcher : MainCoroutineDispatcher(), Delay {

@Keep
internal class AndroidDispatcherFactory : MainDispatcherFactory {
companion object {
@JvmStatic // accessed reflectively from core
fun getDispatcher(): MainCoroutineDispatcher = Main
}

override fun createDispatcher(): MainCoroutineDispatcher = Main

override val loadPriority: Int
Expand Down