diff --git a/play-services-base/src/main/java/com/google/android/gms/common/GooglePlayServicesUtil.java b/play-services-base/src/main/java/com/google/android/gms/common/GooglePlayServicesUtil.java index fd168573b2..ff299d1d0c 100644 --- a/play-services-base/src/main/java/com/google/android/gms/common/GooglePlayServicesUtil.java +++ b/play-services-base/src/main/java/com/google/android/gms/common/GooglePlayServicesUtil.java @@ -39,6 +39,7 @@ @PublicApi public class GooglePlayServicesUtil { private static final String TAG = "GooglePlayServicesUtil"; + private static final int PACKAGE_CONTEXT_FLAGS = Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY; public static final String GMS_ERROR_DIALOG = "GooglePlayServicesErrorDialog"; @@ -59,6 +60,13 @@ public class GooglePlayServicesUtil { */ public static final String GOOGLE_PLAY_STORE_PACKAGE = "com.android.vending"; + private static String[] getGmsPackageCandidates() { + return new String[] { + Constants.USER_MICROG_PACKAGE_NAME, + Constants.GMS_PACKAGE_NAME + }; + } + /** * Returns a dialog to address the provided errorCode. The returned dialog displays a localized * message about the error and upon user confirmation (by tapping on dialog) will direct them @@ -136,11 +144,13 @@ public static String getOpenSourceSoftwareLicenseInfo(Context context) { * @return The Context object of the Buddy APK or null if the Buddy APK is not installed on the device. */ public static Context getRemoteContext(Context context) { - try { - return context.createPackageContext(Constants.GMS_PACKAGE_NAME, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY); - } catch (PackageManager.NameNotFoundException unused) { - return null; + for (String packageName : getGmsPackageCandidates()) { + try { + return context.createPackageContext(packageName, PACKAGE_CONTEXT_FLAGS); + } catch (PackageManager.NameNotFoundException ignored) { + } } + return null; } /** @@ -149,11 +159,13 @@ public static Context getRemoteContext(Context context) { * @return The Resources object of the Buddy APK or null if the Buddy APK is not installed on the device. */ public static Resources getRemoteResources(Context context) { - try { - return context.getPackageManager().getResourcesForApplication(Constants.GMS_PACKAGE_NAME); - } catch (PackageManager.NameNotFoundException unused) { - return null; + for (String packageName : getGmsPackageCandidates()) { + try { + return context.getPackageManager().getResourcesForApplication(packageName); + } catch (PackageManager.NameNotFoundException ignored) { + } } + return null; } /** diff --git a/play-services-core/src/main/java/com/google/android/gms/chimera/DynamiteContextFactory.java b/play-services-core/src/main/java/com/google/android/gms/chimera/DynamiteContextFactory.java index 80580d8342..1697f6ca5d 100644 --- a/play-services-core/src/main/java/com/google/android/gms/chimera/DynamiteContextFactory.java +++ b/play-services-core/src/main/java/com/google/android/gms/chimera/DynamiteContextFactory.java @@ -15,6 +15,7 @@ import android.os.Process; import android.util.Log; +import com.google.android.gms.BuildConfig; import com.google.android.gms.chimera.container.DynamiteContext; import com.google.android.gms.chimera.container.DynamiteModuleInfo; import com.google.android.gms.chimera.container.FilteredClassLoader; @@ -34,6 +35,24 @@ public class DynamiteContextFactory { // WeakHashMap cannot be used, and there is a high probability that it will be recycled, causing ClassLoader to be rebuilt private static final Map sClassLoaderCache = new HashMap<>(); + private static Context createGmsPackageContext(Context context) throws PackageManager.NameNotFoundException { + String[] candidates = new String[] { + BuildConfig.APPLICATION_ID, + Constants.USER_MICROG_PACKAGE_NAME, + Constants.GMS_PACKAGE_NAME + }; + + for (String packageName : candidates) { + try { + return context.createPackageContext(packageName, 0); + } catch (PackageManager.NameNotFoundException e) { + Log.d(TAG, "Unable to create package context for " + packageName); + } + } + + throw new PackageManager.NameNotFoundException("No supported GMS package context found"); + } + public static DynamiteContext createDynamiteContext(String moduleId, Context originalContext) { if (originalContext == null) { Log.w(TAG, "create Original context is null"); @@ -49,7 +68,7 @@ public static DynamiteContext createDynamiteContext(String moduleId, Context ori } try { DynamiteModuleInfo moduleInfo = new DynamiteModuleInfo(moduleId); - Context gmsContext = originalContext.createPackageContext(Constants.GMS_PACKAGE_NAME, 0); + Context gmsContext = createGmsPackageContext(originalContext); Context originalAppContext = originalContext.getApplicationContext(); DynamiteContext dynamiteContext;