core: Providers should consider Robolectric as Android

null class loader means to use the bootstrap class loader, which would
normally make sense. However, Robolectric tests run on OpenJDK and
provide the Android environment as part of the application, so "Android"
won't be present in the bootstrap class loader.
This commit is contained in:
Eric Anderson 2017-08-09 11:01:15 -07:00 committed by GitHub
parent f46280ae9f
commit c16edb3c90
2 changed files with 6 additions and 2 deletions

View File

@ -125,7 +125,9 @@ public abstract class ManagedChannelProvider {
*/
protected static boolean isAndroid() {
try {
Class.forName("android.app.Application", /*initialize=*/ false, null);
// Specify a class loader instead of null because we may be running under Robolectric
Class.forName("android.app.Application", /*initialize=*/ false,
ManagedChannelProvider.class.getClassLoader());
return true;
} catch (Exception e) {
// If Application isn't loaded, it might as well not be Android.

View File

@ -124,7 +124,9 @@ public abstract class NameResolverProvider extends NameResolver.Factory {
private static boolean isAndroid() {
try {
Class.forName("android.app.Application", /*initialize=*/ false, null);
// Specify a class loader instead of null because we may be running under Robolectric
Class.forName("android.app.Application", /*initialize=*/ false,
NameResolverProvider.class.getClassLoader());
return true;
} catch (Exception e) {
// If Application isn't loaded, it might as well not be Android.