Skip to content

Commit cdd78ed

Browse files
xsterfiliph
authored andcommitted
add breaking change for FlutterMain.setIsRunningInRobolectricTest removal (flutter#4561)
1 parent c04679e commit cdd78ed

File tree

2 files changed

+75
-0
lines changed

2 files changed

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

src/docs/release/breaking-changes/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ release, and listed in alphabetical order:
1717
* [Network Policy on iOS and Android][]
1818
* [SnackBars managed by the ScaffoldMessenger][]
1919
* [TextSelectionTheme migration][]
20+
* [Android FlutterMain.setIsRunningInRobolectricTest testing API removed][]
2021
* [Clip behavior][]
2122

2223
[Android ActivityControlSurface attachToActivity signature change]: /docs/release/breaking-changes/android-activity-control-surface-attach
24+
[Android FlutterMain.setIsRunningInRobolectricTest testing API removed]: /docs/release/breaking-changes/android-setIsRunningInRobolectricTest-removed
2325
[Material Chip button semantics]: /docs/release/breaking-changes/material-chip-button-semantics
2426
[Network Policy on iOS and Android]: /docs/release/breaking-changes/network-policy-ios-android
2527
[SnackBars managed by the ScaffoldMessenger]: /docs/release/breaking-changes/scaffold-messenger

0 commit comments

Comments
 (0)