Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 3a21a38

Browse files
author
Chris Yang
committed
fix
1 parent ee31bf1 commit 3a21a38

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

shell/platform/darwin/common/framework/Source/FlutterNSBundleUtils.mm

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
FLUTTER_ASSERT_ARC
1010

11-
const NSString* kDefaultAssetPath = @"Frameworks/App.framework/flutter_assets";
12-
static NSString* GetFlutterAssetPathFromBundle(NSBundle* bundle);
11+
NSString* kDefaultAssetPath = @"Frameworks/App.framework/flutter_assets";
12+
static NSString* GetFlutterAssetsPathFromBundle(NSBundle* bundle, NSString* relativeAssetsPath);
1313

1414
NSBundle* FLTFrameworkBundleInternal(NSString* flutterFrameworkBundleID, NSURL* searchURL) {
1515
NSDirectoryEnumerator<NSURL*>* frameworkEnumerator = [NSFileManager.defaultManager
@@ -55,22 +55,30 @@
5555
}
5656

5757
NSString* FLTAssetPath(NSBundle* bundle) {
58-
return [bundle objectForInfoDictionaryKey:@"FLTAssetsPath"] ?: kDefaultAssetPath;
58+
NSString* relativeAssetsPath = [bundle objectForInfoDictionaryKey:@"FLTAssetsPath"];
59+
if (relativeAssetsPath.length == 0) {
60+
// Try main bundle
61+
relativeAssetsPath = [NSBundle.mainBundle objectForInfoDictionaryKey:@"FLTAssetsPath"];
62+
}
63+
if (relativeAssetsPath.length == 0) {
64+
relativeAssetsPath = kDefaultAssetPath;
65+
}
66+
return relativeAssetsPath;
5967
}
6068

6169
NSString* FLTAssetsPathFromBundle(NSBundle* bundle) {
62-
NSString* flutterAssetsPath = GetFlutterAssetPathFromBundle(bundle);
70+
NSString* relativeAssetsPath = FLTAssetPath(bundle);
71+
NSString* flutterAssetsPath = GetFlutterAssetsPathFromBundle(bundle, relativeAssetsPath);
6372
if (flutterAssetsPath.length == 0) {
64-
flutterAssetsPath = GetFlutterAssetPathFromBundle(NSBundle.mainBundle);
73+
flutterAssetsPath = GetFlutterAssetsPathFromBundle(NSBundle.mainBundle, relativeAssetsPath);
6574
}
6675
return flutterAssetsPath;
6776
}
6877

69-
static NSString* GetFlutterAssetPathFromBundle(NSBundle* bundle) {
70-
NSString* flutterAssetsPath = FLTAssetPath(bundle);
78+
static NSString* GetFlutterAssetsPathFromBundle(NSBundle* bundle, NSString* relativeAssetsPath) {
7179
// Use the raw path solution so that asset path can be returned from unloaded bundles.
7280
// See https://github.com/flutter/engine/pull/46073
73-
NSString* assetsPath = [bundle pathForResource:flutterAssetsPath ofType:nil];
81+
NSString* assetsPath = [bundle pathForResource:relativeAssetsPath ofType:nil];
7482
if (assetsPath.length == 0) {
7583
// In app extension, using full relative path (kDefaultAssetPath)
7684
// returns nil when the app bundle is not loaded. Try to use

shell/platform/darwin/ios/framework/Source/FlutterDartProjectTest.mm

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,26 @@ - (void)testLookUpForAssetsFromBundle {
157157
XCTAssertEqualObjects(assetsPath, @"foo/assets/bar");
158158
}
159159
{
160-
// No assets path in info.plist, use default value
160+
// Found assets path in info.plist in main bundle
161161
id mockBundle = OCMClassMock([NSBundle class]);
162+
id mockMainBundle = OCMPartialMock(NSBundle.mainBundle);
162163
OCMStub([mockBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(nil);
164+
OCMStub([mockMainBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(@"foo/assets");
165+
NSString* assetsPath = [FlutterDartProject lookupKeyForAsset:@"bar" fromBundle:mockBundle];
166+
// This is testing public API, changing this assert is likely to break plugins.
167+
XCTAssertEqualObjects(assetsPath, @"foo/assets/bar");
168+
[mockBundle stopMocking];
169+
}
170+
{
171+
// No assets path in both info.plist, use default value
172+
id mockBundle = OCMClassMock([NSBundle class]);
173+
id mockMainBundle = OCMPartialMock(NSBundle.mainBundle);
174+
OCMStub([mockBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(nil);
175+
OCMStub([mockMainBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(nil);
163176
NSString* assetsPath = [FlutterDartProject lookupKeyForAsset:@"bar" fromBundle:mockBundle];
164177
// This is testing public API, changing this assert is likely to break plugins.
165178
XCTAssertEqualObjects(assetsPath, @"Frameworks/App.framework/flutter_assets/bar");
179+
[mockBundle stopMocking];
166180
}
167181
}
168182

@@ -200,15 +214,31 @@ - (void)testLookUpForAssetsFromPackageFromBundle {
200214
XCTAssertEqualObjects(assetsPath, @"foo/assets/packages/bar_package/bar");
201215
}
202216
{
217+
// Found assets path in info.plist in main bundle
203218
id mockBundle = OCMClassMock([NSBundle class]);
219+
id mockMainBundle = OCMPartialMock(NSBundle.mainBundle);
220+
OCMStub([mockBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(nil);
221+
OCMStub([mockMainBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(@"foo/assets");
222+
NSString* assetsPath = [FlutterDartProject lookupKeyForAsset:@"bar"
223+
fromPackage:@"bar_package"
224+
fromBundle:mockBundle];
225+
// This is testing public API, changing this assert is likely to break plugins.
226+
XCTAssertEqualObjects(assetsPath, @"foo/assets/packages/bar_package/bar");
227+
[mockBundle stopMocking];
228+
}
229+
{
230+
id mockBundle = OCMClassMock([NSBundle class]);
231+
id mockMainBundle = OCMPartialMock(NSBundle.mainBundle);
204232
// No assets path in info.plist, use default value
205233
OCMStub([mockBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(nil);
234+
OCMStub([mockMainBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(nil);
206235
NSString* assetsPath = [FlutterDartProject lookupKeyForAsset:@"bar"
207236
fromPackage:@"bar_package"
208237
fromBundle:mockBundle];
209238
// This is testing public API, changing this assert is likely to break plugins.
210239
XCTAssertEqualObjects(assetsPath,
211240
@"Frameworks/App.framework/flutter_assets/packages/bar_package/bar");
241+
[mockBundle stopMocking];
212242
}
213243
}
214244

0 commit comments

Comments
 (0)