diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java index 21c95edc6c..1c9f76ec85 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java @@ -215,7 +215,7 @@ public class FlutterEngine { boolean automaticallyRegisterPlugins ) { this.flutterJNI = flutterJNI; - flutterLoader.startInitialization(context); + flutterLoader.startInitialization(context.getApplicationContext()); flutterLoader.ensureInitializationComplete(context, dartVmArgs); flutterJNI.addEngineLifecycleListener(engineLifecycleListener); diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java index 35f4098597..1f70580c74 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java @@ -113,6 +113,9 @@ public class FlutterLoader { throw new IllegalStateException("startInitialization must be called on the main thread"); } + // Ensure that the context is actually the application context. + applicationContext = applicationContext.getApplicationContext(); + this.settings = settings; long initStartTimestampMillis = SystemClock.uptimeMillis(); diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineTest.java index 353fe648ce..3373ce82c7 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineTest.java @@ -1,5 +1,6 @@ package test.io.flutter.embedding.engine; +import android.content.Context; import io.flutter.plugin.platform.PlatformViewsController; import io.flutter.plugins.GeneratedPluginRegistrant; import java.util.List; @@ -21,6 +22,7 @@ import io.flutter.embedding.engine.loader.FlutterLoader; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -102,4 +104,19 @@ public class FlutterEngineTest { // AKA hot restart. verify(platformViewsController, times(1)).onPreEngineRestart(); } + + @Test + public void itUsesApplicationContext() { + Context context = mock(Context.class); + + new FlutterEngine( + context, + mock(FlutterLoader.class), + flutterJNI, + /*dartVmArgs=*/new String[] {}, + /*automaticallyRegisterPlugins=*/false + ); + + verify(context, atLeast(1)).getApplicationContext(); + } }