Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package io.flutter.embedding.engine;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.FlutterInjector;
Expand Down Expand Up @@ -264,7 +266,13 @@ public FlutterEngine(
@Nullable String[] dartVmArgs,
boolean automaticallyRegisterPlugins,
boolean waitForRestorationData) {
this.dartExecutor = new DartExecutor(flutterJNI, context.getAssets());
AssetManager assetManager;
try {
assetManager = context.createPackageContext(context.getPackageName(), 0).getAssets();
} catch (NameNotFoundException e) {
assetManager = context.getAssets();
}
this.dartExecutor = new DartExecutor(flutterJNI, assetManager);
Comment on lines +274 to +275
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be inside a finally block, as DartExecutor is always initialized with the assetManager value, no matter there's an exception.

Suggested change
}
this.dartExecutor = new DartExecutor(flutterJNI, assetManager);
} finally {
this.dartExecutor = new DartExecutor(flutterJNI, assetManager);
}

this.dartExecutor.onAttachedToJNI();

accessibilityChannel = new AccessibilityChannel(dartExecutor, flutterJNI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import io.flutter.FlutterInjector;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterJNI;
Expand Down Expand Up @@ -127,8 +129,11 @@ public void itNotifiesPlatformViewsControllerAboutJNILifecycle() {
}

@Test
public void itUsesApplicationContext() {
public void itUsesApplicationContext() throws NameNotFoundException {
Context context = mock(Context.class);
Context packageContext = mock(Context.class);

when(context.createPackageContext(any(), anyInt())).thenReturn(packageContext);

new FlutterEngine(
context,
Expand All @@ -141,12 +146,32 @@ public void itUsesApplicationContext() {
}

@Test
public void itCanUseFlutterLoaderInjectionViaFlutterInjector() {
public void itUsesPackageContextForAssetManager() throws NameNotFoundException {
Context context = mock(Context.class);
Context packageContext = mock(Context.class);
when(context.createPackageContext(any(), anyInt())).thenReturn(packageContext);

new FlutterEngine(
context,
mock(FlutterLoader.class),
flutterJNI,
/*dartVmArgs=*/ new String[] {},
/*automaticallyRegisterPlugins=*/ false);

verify(packageContext, atLeast(1)).getAssets();
verify(context, times(0)).getAssets();
}

@Test
public void itCanUseFlutterLoaderInjectionViaFlutterInjector() throws NameNotFoundException {
FlutterInjector.reset();
FlutterLoader mockFlutterLoader = mock(FlutterLoader.class);
FlutterInjector.setInstance(
new FlutterInjector.Builder().setFlutterLoader(mockFlutterLoader).build());
Context mockContext = mock(Context.class);
Context packageContext = mock(Context.class);

when(mockContext.createPackageContext(any(), anyInt())).thenReturn(packageContext);

new FlutterEngine(mockContext, null, flutterJNI);

Expand Down