diff --git a/README.md b/README.md index b0f519d451..865e6ec214 100644 --- a/README.md +++ b/README.md @@ -129,17 +129,9 @@ 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 +### R8 and 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 ; -} -``` +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. ## Building diff --git a/common/kotlinx-coroutines-core-common/src/CoroutineExceptionHandler.kt b/common/kotlinx-coroutines-core-common/src/CoroutineExceptionHandler.kt index 1ed1c4bc63..1a955b7189 100644 --- a/common/kotlinx-coroutines-core-common/src/CoroutineExceptionHandler.kt +++ b/common/kotlinx-coroutines-core-common/src/CoroutineExceptionHandler.kt @@ -4,7 +4,6 @@ package kotlinx.coroutines -import kotlinx.coroutines.internal.* import kotlin.coroutines.* internal expect fun handleCoroutineExceptionImpl(context: CoroutineContext, exception: Throwable) diff --git a/core/kotlinx-coroutines-core/resources/META-INF/proguard/coroutines.pro b/core/kotlinx-coroutines-core/resources/META-INF/proguard/coroutines.pro new file mode 100644 index 0000000000..86108f3ca7 --- /dev/null +++ b/core/kotlinx-coroutines-core/resources/META-INF/proguard/coroutines.pro @@ -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 ; +} diff --git a/core/kotlinx-coroutines-core/src/Dispatchers.kt b/core/kotlinx-coroutines-core/src/Dispatchers.kt index 76e032ada9..ca3d4c02c5 100644 --- a/core/kotlinx-coroutines-core/src/Dispatchers.kt +++ b/core/kotlinx-coroutines-core/src/Dispatchers.kt @@ -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. @@ -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 diff --git a/ui/kotlinx-coroutines-android/animation-app/app/proguard-rules.pro b/ui/kotlinx-coroutines-android/animation-app/app/proguard-rules.pro index ec1a65802f..73855392fd 100644 --- a/ui/kotlinx-coroutines-android/animation-app/app/proguard-rules.pro +++ b/ui/kotlinx-coroutines-android/animation-app/app/proguard-rules.pro @@ -1,3 +1,5 @@ +-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {} +-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {} -keepclassmembernames class kotlinx.** { volatile ; } diff --git a/ui/kotlinx-coroutines-android/example-app/app/proguard-rules.pro b/ui/kotlinx-coroutines-android/example-app/app/proguard-rules.pro index ec1a65802f..73855392fd 100644 --- a/ui/kotlinx-coroutines-android/example-app/app/proguard-rules.pro +++ b/ui/kotlinx-coroutines-android/example-app/app/proguard-rules.pro @@ -1,3 +1,5 @@ +-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {} +-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {} -keepclassmembernames class kotlinx.** { volatile ; } diff --git a/ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt b/ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt index b822047529..2181b1cda4 100644 --- a/ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt +++ b/ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt @@ -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