Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save OleksandrKucherenko/a9e9360c7e1e27ece796a25d1bdc5587 to your computer and use it in GitHub Desktop.
Save OleksandrKucherenko/a9e9360c7e1e27ece796a25d1bdc5587 to your computer and use it in GitHub Desktop.
Workaround for Robolectric Unit Tests with Android Dev Metrics tool attached
public class TheApp extends Application {
@Override
public void onCreate() {
// now give the app code do the rest: launch activities, etc.
super.onCreate();
// Use it only in debug builds
if (BuildConfig.DEBUG) {
initializeAndroidDevMetrics(this);
}
}
/** Is detected robolectric test environment. */
public static boolean isRobolectric() {
return "robolectric".equals(Build.FINGERPRINT);
}
/** Initialize dev metrics, but disable them for Robolectric tests. */
public static void initializeAndroidDevMetrics(@NonNull final Context context) {
// https://stackoverflow.com/questions/36479391/roboelectric-unit-tests-are-unresponsive
final boolean enabled = !isRobolectric();
final AndroidDevMetrics.Builder builder = new AndroidDevMetrics.Builder(context)
.enableActivityMetrics(enabled)
.enableDagger2Metrics(enabled)
.showNotification(enabled);
AndroidDevMetrics.initWith(builder);
// important line! resolve issue of initialization
MethodsTracingManager.getInstance().init(context);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment