|
| 1 | +--- |
| 2 | +title: FlutterMain.setIsRunningInRobolectricTest on Android removed |
| 3 | +description: The test-only FlutterMain.setIsRunningInRobolectricTest API on |
| 4 | +the Android engine is consolidated into the FlutterInjector. |
| 5 | +--- |
| 6 | + |
| 7 | +## Summary |
| 8 | + |
| 9 | +If you write Java JUnit tests (such as Robolectric tests) against the Flutter |
| 10 | +engine's Java embedding and used the |
| 11 | +`FlutterMain.setIsRunningInRobolectricTest(true)` API, replace it with |
| 12 | +`FlutterInjector.setInstance(new FlutterInjector.Builder().setShouldLoadNative(false).build())`. |
| 13 | + |
| 14 | +This should be very uncommon. |
| 15 | + |
| 16 | +## Context |
| 17 | + |
| 18 | +The `FlutterMain` class itself is being deprecated and replaced with the |
| 19 | +`FlutterInjector` class. The `FlutterMain` class itself used a number of |
| 20 | +static variables and functions than make it difficult to test. The |
| 21 | +`FlutterMain.setIsRunningInRobolectricTest()` is one ad-hoc static |
| 22 | +mechanism to allow tests to run on the host machine on JVM without |
| 23 | +loading the libflutter.so native library (which can't be done on the host |
| 24 | +machine). |
| 25 | + |
| 26 | +Rather than one-off solutions, all dependency injections needed for tests |
| 27 | +in Flutter's Android/Java engine embedding are now moved to the |
| 28 | +[`FlutterInjector`](https://cs.opensource.google/flutter/engine/+/master:shell/platform/android/io/flutter/FlutterInjector.java) |
| 29 | +class. |
| 30 | + |
| 31 | +Within the `FlutterInjector` class, the `setShouldLoadNative()` Builder |
| 32 | +function allows for control of whether to load the native library. |
| 33 | + |
| 34 | +## Description of change |
| 35 | + |
| 36 | +https://github.com/flutter/engine/commit/15f5696c4139a21e1fc54014ce17d01f6ad1737c#diff-599e1d64442183ead768757cca6805c3L154 |
| 37 | +removes the `FlutterMain.setIsRunningInRobolectricTest()` testing function. |
| 38 | + |
| 39 | +https://github.com/flutter/engine/commit/15f5696c4139a21e1fc54014ce17d01f6ad1737c#diff-f928557f2d60773a8435366400fa42ed |
| 40 | +adds a `FlutterInjector` class to assist testing. |
| 41 | + |
| 42 | +## Migration guide |
| 43 | + |
| 44 | +Code before migration: |
| 45 | + |
| 46 | +<!-- skip --> |
| 47 | +```java |
| 48 | +FlutterMain.setIsRunningInRobolectricTest(true); |
| 49 | +``` |
| 50 | + |
| 51 | +Code after migration: |
| 52 | + |
| 53 | +```java |
| 54 | +FlutterInjector.setInstance(new FlutterInjector.Builder().setShouldLoadNative(false).build()); |
| 55 | +``` |
| 56 | + |
| 57 | +## Timeline |
| 58 | + |
| 59 | +Landed in version: 1.22.0-2.0.pre.133<br> |
| 60 | +In stable release: not yet |
0 commit comments