1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 package com.google.protobuf; 9 10 final class Android { Android()11 private Android() { 12 } 13 14 // Set to true in lite_proguard_android.pgcfg. 15 @SuppressWarnings("ConstantField") 16 private static boolean ASSUME_ANDROID; 17 18 private static final Class<?> MEMORY_CLASS = getClassForName("libcore.io.Memory"); 19 20 private static final boolean IS_ROBOLECTRIC = 21 !ASSUME_ANDROID && getClassForName("org.robolectric.Robolectric") != null; 22 23 /** Returns {@code true} if running on an Android device. */ isOnAndroidDevice()24 static boolean isOnAndroidDevice() { 25 return ASSUME_ANDROID || (MEMORY_CLASS != null && !IS_ROBOLECTRIC); 26 } 27 28 /** Returns the memory class or {@code null} if not on Android device. */ getMemoryClass()29 static Class<?> getMemoryClass() { 30 return MEMORY_CLASS; 31 } 32 33 @SuppressWarnings("unchecked") getClassForName(String name)34 private static <T> Class<T> getClassForName(String name) { 35 try { 36 return (Class<T>) Class.forName(name); 37 } catch (Throwable e) { 38 return null; 39 } 40 } 41 } 42