diff --git a/play-services-ads/core/src/main/java/com/google/android/gms/dynamite/descriptors/com/google/android/gms/ads/dynamite/ModuleDescriptor.java b/play-services-ads/core/src/main/java/com/google/android/gms/dynamite/descriptors/com/google/android/gms/ads/dynamite/ModuleDescriptor.java index 7a14482b94..f147bc76dd 100644 --- a/play-services-ads/core/src/main/java/com/google/android/gms/dynamite/descriptors/com/google/android/gms/ads/dynamite/ModuleDescriptor.java +++ b/play-services-ads/core/src/main/java/com/google/android/gms/dynamite/descriptors/com/google/android/gms/ads/dynamite/ModuleDescriptor.java @@ -7,7 +7,6 @@ import android.content.Context; import android.content.ContextWrapper; -import android.util.Log; import android.webkit.WebSettings; import androidx.annotation.Keep; @@ -16,9 +15,17 @@ public class ModuleDescriptor { public static final String MODULE_ID = "com.google.android.gms.ads.dynamite"; public static final int MODULE_VERSION = 230500001; - public static void init(ContextWrapper context) { - Context baseContext = context.getBaseContext(); - WebSettings.getDefaultUserAgent(baseContext); - Log.d("ModuleDescriptor", "init: context: " + baseContext); + /** + * The ads module might try to access the user agent, requiring initialization on the main thread, + * which may result in deadlocks when invoked from any other thread. This only happens with microG, + * because we don't use the highly privileged SELinux Sandbox that regular Play Services uses + * (which allows apps to read the user-agent from Play Services instead of the WebView). To prevent + * the issue we pre-emptively initialize the WebView. + */ + public static void init(Context context) { + if (context instanceof ContextWrapper) { + context = ((ContextWrapper) context).getBaseContext(); + } + WebSettings.getDefaultUserAgent(context); } } diff --git a/play-services-core/src/main/java/com/google/android/gms/chimera/container/DynamiteModuleInfo.java b/play-services-core/src/main/java/com/google/android/gms/chimera/container/DynamiteModuleInfo.java index 9f25008f24..bad67c00a5 100644 --- a/play-services-core/src/main/java/com/google/android/gms/chimera/container/DynamiteModuleInfo.java +++ b/play-services-core/src/main/java/com/google/android/gms/chimera/container/DynamiteModuleInfo.java @@ -8,9 +8,7 @@ import java.util.Collection; import java.util.Collections; -import static android.content.Context.CONTEXT_IGNORE_SECURITY; -import static android.content.Context.CONTEXT_INCLUDE_CODE; -import android.content.ContextWrapper; +import android.content.Context; public class DynamiteModuleInfo { private Class descriptor; @@ -53,9 +51,9 @@ public Collection getMergedClasses() { } } - public void init(ContextWrapper dynamiteContext) { + public void init(Context dynamiteContext) { try { - descriptor.getMethod("init", ContextWrapper.class).invoke(null, dynamiteContext); + descriptor.getMethod("init", Context.class).invoke(null, dynamiteContext); } catch (Exception e) { // Ignore }