mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
![auto-submit[bot]](/assets/img/avatar_default.png)
Reverts flutter/flutter#132985 Initiated by: christopherfujino This change reverts the following previous change: Original Description: Provides support for conditional bundling of assets through the existing `--flavor` option for `flutter build` and `flutter run`. Closes https://github.com/flutter/flutter/issues/21682. Resolves https://github.com/flutter/flutter/issues/136092 ## Change Within the `assets` section pubspec.yaml, the user can now specify one or more `flavors` that an asset belongs to. Consider this example: ```yaml # pubspec.yaml flutter: assets: - assets/normal-asset.png - path: assets/vanilla/ice-cream.png flavors: - vanilla - path: assets/strawberry/ice-cream.png flavors: - strawberry ``` With this pubspec, * `flutter run --flavor vanilla` will not include `assets/strawberry/ice-cream.png` in the build output. * `flutter run --flavor strawberry` will not include `assets/vanilla/ice-cream.png`. * `flutter run` will only include `assets/normal-asset.png`. ## Open questions * Should this be supported for all platforms, or should this change be limited to ones with documented `--flavor` support (Android, iOS, and (implicitly) MacOS)? This PR currently only enables this feature for officially supported platforms. ## Design thoughts, what this PR does not do, etc. ### This does not provide an automatic mapping/resolution of asset keys/paths to others based on flavor at runtime. The implementation in this PR represents a simplest approach. Notably, it does not give Flutter the ability to dynamically choose an asset based on flavor using a single asset key. For example, one can't use `Image.asset('config.json')` to dynamically choose between different "flavors" of `config.json` (such as `dev-flavor/config.json` or `prod-flavor/config.json`). However, a user could always implement such a mechanism in their project or in a library by examining the flavor at runtime. ### When multiple entries affect the same file and 1) at least one of these entries have a `flavors` list provided and 2) these lists are not equivalent, we always consider the manifest to be ambiguous and will throw a `ToolExit`. <details> For example, these manifests would all be considered ambiguous: ```yaml assets: - assets/ - path: assets/vanilla.png flavors: - vanilla assets: - path: assets/vanilla/ flavors: - vanilla - path: assets/vanilla/cherry.png flavor: - cherry # Thinking towards the future where we might add glob/regex support and more conditions other than flavor: assets: - path: assets/vanilla/** flavors: - vanilla - path: assets/**/ios/** platforms: - ios # Ambiguous in the case of assets like "assets/vanilla/ios/icon.svg" since we # don't know if flavor `vanilla` and platform `ios` should be combined using or-logic or and-logic. ``` See [this review comment thread](https://github.com/flutter/flutter/pull/132985#discussion_r1381909942) for the full story on how I arrived at this decision. </details> ### This does not support Android's multidimensional flavors feature (in an intuitive way) <details> Conder this excerpt from a Flutter project's android/app/build.gradle file: ```groovy android { // ... flavorDimensions "mode", "api" productFlavors { free { dimension "mode" applicationIdSuffix ".free" } premium { dimension "mode" applicationIdSuffix ".premium" } minApi23 { dimension "api" versionNameSuffix "-minApi23" } minApi21 { dimension "api" versionNameSuffix "-minApi21" } } } ``` In this setup, the following values are valid `--flavor` are valid `freeMinApi21`, `freeMinApi23`, `premiumMinApi21`, and `premiumMinApi23`. We call these values "flavor combinations". Consider the following from the Android documentation[^1]: > In addition to the source set directories you can create for each individual product flavor and build variant, you can also create source set directories for each combination of product flavors. For example, you can create and add Java sources to the src/demoMinApi24/java/ directory, and Gradle uses those sources only when building a variant that combines those two product flavors. > > Source sets you create for product flavor combinations have a higher priority than source sets that belong to each individual product flavor. To learn more about source sets and how Gradle merges resources, read the section about how to [create source sets](https://developer.android.com/build/build-variants#sourcesets). This feature will not behave in this way. If a user utilizes this feature and also Android's multidimensional flavors feature, they will have to list out all flavor combinations that contain the flavor they want to limit an asset to: ```yaml assets: - assets/free/ flavors: - freeMinApi21 - freeMinApi23 ``` This is mostly due to a technical limitation in the hot-reload feature of `flutter run`. During a hot reload, the tool will try to update the asset bundle on the device, but the tool does not know the flavors contained within the flavor combination (that the user passes to `--flavor`). Gradle is the source of truth of what flavors were involved in the build, and `flutter run` currently does not access to that information since it's an implementation detail of the build process. We could bubble up this information, but it would require a nontrivial amount of engineering work, and it's unclear how desired this functionality is. It might not be worth implementing. </details> See https://flutter.dev/go/flavor-specific-assets for the (outdated) design document. <summary>Pre-launch Checklist</summary> </details> [^1]: https://developer.android.com/build/build-variants#flavor-dimensions
349 lines
13 KiB
Dart
349 lines
13 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'package:file/memory.dart';
|
|
import 'package:flutter_tools/src/base/deferred_component.dart';
|
|
import 'package:flutter_tools/src/base/file_system.dart';
|
|
import 'package:flutter_tools/src/base/logger.dart';
|
|
|
|
import '../../src/common.dart';
|
|
|
|
void main() {
|
|
group('DeferredComponent basics', () {
|
|
testWithoutContext('constructor sets values', () {
|
|
final DeferredComponent component = DeferredComponent(
|
|
name: 'bestcomponent',
|
|
libraries: <String>['lib1', 'lib2'],
|
|
assets: <Uri>[Uri.file('asset1'), Uri.file('asset2')],
|
|
);
|
|
expect(component.name, 'bestcomponent');
|
|
expect(component.libraries, <String>['lib1', 'lib2']);
|
|
expect(component.assets, <Uri>[Uri.file('asset1'), Uri.file('asset2')]);
|
|
});
|
|
|
|
testWithoutContext('assignLoadingUnits selects the needed loading units and sets assigned', () {
|
|
final DeferredComponent component = DeferredComponent(
|
|
name: 'bestcomponent',
|
|
libraries: <String>['lib1', 'lib2'],
|
|
assets: <Uri>[Uri.file('asset1'), Uri.file('asset2')],
|
|
);
|
|
expect(component.libraries, <String>['lib1', 'lib2']);
|
|
expect(component.assigned, false);
|
|
expect(component.loadingUnits, null);
|
|
|
|
final List<LoadingUnit> loadingUnits1 = <LoadingUnit>[
|
|
LoadingUnit(
|
|
id: 2,
|
|
path: 'path/to/so.so',
|
|
libraries: <String>['lib1', 'lib4'],
|
|
),
|
|
LoadingUnit(
|
|
id: 3,
|
|
path: 'path/to/so.so',
|
|
libraries: <String>['lib2', 'lib5'],
|
|
),
|
|
LoadingUnit(
|
|
id: 4,
|
|
path: 'path/to/so.so',
|
|
libraries: <String>['lib6', 'lib7'],
|
|
),
|
|
];
|
|
|
|
component.assignLoadingUnits(loadingUnits1);
|
|
|
|
expect(component.assigned, true);
|
|
expect(component.loadingUnits, hasLength(2));
|
|
expect(component.loadingUnits, contains(loadingUnits1[0]));
|
|
expect(component.loadingUnits, contains(loadingUnits1[1]));
|
|
expect(component.loadingUnits, isNot(contains(loadingUnits1[2])));
|
|
|
|
final List<LoadingUnit> loadingUnits2 = <LoadingUnit>[
|
|
LoadingUnit(
|
|
id: 2,
|
|
path: 'path/to/so.so',
|
|
libraries: <String>['lib1', 'lib2'],
|
|
),
|
|
LoadingUnit(
|
|
id: 3,
|
|
path: 'path/to/so.so',
|
|
libraries: <String>['lib5', 'lib6'],
|
|
),
|
|
LoadingUnit(
|
|
id: 4,
|
|
path: 'path/to/so.so',
|
|
libraries: <String>['lib7', 'lib8'],
|
|
),
|
|
];
|
|
// Can reassign loading units.
|
|
component.assignLoadingUnits(loadingUnits2);
|
|
|
|
expect(component.assigned, true);
|
|
expect(component.loadingUnits, hasLength(1));
|
|
expect(component.loadingUnits, contains(loadingUnits2[0]));
|
|
expect(component.loadingUnits, isNot(contains(loadingUnits2[1])));
|
|
expect(component.loadingUnits, isNot(contains(loadingUnits2[2])));
|
|
|
|
component.assignLoadingUnits(<LoadingUnit>[]);
|
|
|
|
expect(component.assigned, true);
|
|
expect(component.loadingUnits, hasLength(0));
|
|
});
|
|
|
|
testWithoutContext('toString produces correct string for unassigned component', () {
|
|
final DeferredComponent component = DeferredComponent(
|
|
name: 'bestcomponent',
|
|
libraries: <String>['lib1', 'lib2'],
|
|
assets: <Uri>[Uri.file('asset1'), Uri.file('asset2')],
|
|
);
|
|
expect(component.toString(), '\nDeferredComponent: bestcomponent\n Libraries:\n - lib1\n - lib2\n Assets:\n - asset1\n - asset2');
|
|
});
|
|
|
|
testWithoutContext('toString produces correct string for assigned component', () {
|
|
final DeferredComponent component = DeferredComponent(
|
|
name: 'bestcomponent',
|
|
libraries: <String>['lib1', 'lib2'],
|
|
assets: <Uri>[Uri.file('asset1'), Uri.file('asset2')],
|
|
);
|
|
component.assignLoadingUnits(<LoadingUnit>[LoadingUnit(id: 2, libraries: <String>['lib1'])]);
|
|
expect(component.toString(), '\nDeferredComponent: bestcomponent\n Libraries:\n - lib1\n - lib2\n LoadingUnits:\n - 2\n Assets:\n - asset1\n - asset2');
|
|
});
|
|
});
|
|
|
|
group('LoadingUnit basics', () {
|
|
testWithoutContext('constructor sets values', () {
|
|
final LoadingUnit unit = LoadingUnit(
|
|
id: 2,
|
|
path: 'path/to/so.so',
|
|
libraries: <String>['lib1', 'lib4'],
|
|
);
|
|
expect(unit.id, 2);
|
|
expect(unit.path, 'path/to/so.so');
|
|
expect(unit.libraries, <String>['lib1', 'lib4']);
|
|
});
|
|
|
|
testWithoutContext('toString produces correct string', () {
|
|
final LoadingUnit unit = LoadingUnit(
|
|
id: 2,
|
|
path: 'path/to/so.so',
|
|
libraries: <String>['lib1', 'lib4'],
|
|
);
|
|
expect(unit.toString(),'\nLoadingUnit 2\n Libraries:\n - lib1\n - lib4');
|
|
});
|
|
|
|
testWithoutContext('equalsIgnoringPath works for various input', () {
|
|
final LoadingUnit unit1 = LoadingUnit(
|
|
id: 2,
|
|
path: 'path/to/so.so',
|
|
libraries: <String>['lib1', 'lib4'],
|
|
);
|
|
final LoadingUnit unit2 = LoadingUnit(
|
|
id: 2,
|
|
path: 'path/to/other/so.so',
|
|
libraries: <String>['lib1', 'lib4'],
|
|
);
|
|
final LoadingUnit unit3 = LoadingUnit(
|
|
id: 1,
|
|
path: 'path/to/other/so.so',
|
|
libraries: <String>['lib1', 'lib4'],
|
|
);
|
|
final LoadingUnit unit4 = LoadingUnit(
|
|
id: 1,
|
|
path: 'path/to/other/so.so',
|
|
libraries: <String>['lib1'],
|
|
);
|
|
final LoadingUnit unit5 = LoadingUnit(
|
|
id: 1,
|
|
path: 'path/to/other/so.so',
|
|
libraries: <String>['lib2'],
|
|
);
|
|
final LoadingUnit unit6 = LoadingUnit(
|
|
id: 1,
|
|
path: 'path/to/other/so.so',
|
|
libraries: <String>['lib1', 'lib5'],
|
|
);
|
|
expect(unit1.equalsIgnoringPath(unit2), true);
|
|
expect(unit2.equalsIgnoringPath(unit3), false);
|
|
expect(unit3.equalsIgnoringPath(unit4), false);
|
|
expect(unit4.equalsIgnoringPath(unit5), false);
|
|
expect(unit5.equalsIgnoringPath(unit6), false);
|
|
});
|
|
|
|
testWithoutContext('parseLoadingUnitManifest parses single manifest file', () {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final File manifest = fileSystem.file('/manifest.json');
|
|
manifest.createSync(recursive: true);
|
|
manifest.writeAsStringSync(r'''
|
|
{ "loadingUnits": [
|
|
{ "id": 1, "path": "\/arm64-v8a\/app.so", "libraries": [
|
|
"dart:core"]}
|
|
,
|
|
{ "id": 2, "path": "\/arm64-v8a\/app.so-2.part.so", "libraries": [
|
|
"lib2"]}
|
|
,
|
|
{ "id": 3, "path": "\/arm64-v8a\/app.so-3.part.so", "libraries": [
|
|
"lib3", "lib4"]}
|
|
] }
|
|
|
|
''', flush: true);
|
|
final List<LoadingUnit> loadingUnits = LoadingUnit.parseLoadingUnitManifest(manifest, BufferLogger.test());
|
|
expect(loadingUnits.length, 2); // base module (id 1) is not parsed.
|
|
expect(loadingUnits[0].id, 2);
|
|
expect(loadingUnits[0].path, '/arm64-v8a/app.so-2.part.so');
|
|
expect(loadingUnits[0].libraries.length, 1);
|
|
expect(loadingUnits[0].libraries[0], 'lib2');
|
|
expect(loadingUnits[1].id, 3);
|
|
expect(loadingUnits[1].path, '/arm64-v8a/app.so-3.part.so');
|
|
expect(loadingUnits[1].libraries.length, 2);
|
|
expect(loadingUnits[1].libraries[0], 'lib3');
|
|
expect(loadingUnits[1].libraries[1], 'lib4');
|
|
});
|
|
|
|
testWithoutContext('parseLoadingUnitManifest returns empty when manifest is invalid JSON', () {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final File manifest = fileSystem.file('/manifest.json');
|
|
manifest.createSync(recursive: true);
|
|
// invalid due to missing closing brace `}`
|
|
manifest.writeAsStringSync(r'''
|
|
{ "loadingUnits": [
|
|
{ "id": 1, "path": "\/arm64-v8a\/app.so", "libraries": [
|
|
"dart:core"]}
|
|
,
|
|
{ "id": 2, "path": "\/arm64-v8a\/app.so-2.part.so", "libraries": [
|
|
"lib2"]}
|
|
,
|
|
{ "id": 3, "path": "\/arm64-v8a\/app.so-3.part.so", "libraries": [
|
|
"lib3", "lib4"]}
|
|
]
|
|
|
|
''', flush: true);
|
|
final List<LoadingUnit> loadingUnits = LoadingUnit.parseLoadingUnitManifest(manifest, BufferLogger.test());
|
|
expect(loadingUnits.length, 0);
|
|
expect(loadingUnits.isEmpty, true);
|
|
});
|
|
|
|
testWithoutContext('parseLoadingUnitManifest does not exist', () {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final File manifest = fileSystem.file('/manifest.json');
|
|
if (manifest.existsSync()) {
|
|
manifest.deleteSync(recursive: true);
|
|
}
|
|
final List<LoadingUnit> loadingUnits = LoadingUnit.parseLoadingUnitManifest(manifest, BufferLogger.test());
|
|
expect(loadingUnits.length, 0);
|
|
expect(loadingUnits.isEmpty, true);
|
|
});
|
|
|
|
testWithoutContext('parseGeneratedLoadingUnits parses all abis if no abis provided', () {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final File manifest1 = fileSystem.file('/test-abi1/manifest.json');
|
|
manifest1.createSync(recursive: true);
|
|
manifest1.writeAsStringSync(r'''
|
|
{ "loadingUnits": [
|
|
{ "id": 1, "path": "\/test-abi1\/app.so", "libraries": [
|
|
"dart:core"]}
|
|
,
|
|
{ "id": 2, "path": "\/test-abi1\/app.so-2.part.so", "libraries": [
|
|
"lib2"]}
|
|
,
|
|
{ "id": 3, "path": "\/test-abi1\/app.so-3.part.so", "libraries": [
|
|
"lib3", "lib4"]}
|
|
] }
|
|
|
|
''', flush: true);
|
|
final File manifest2 = fileSystem.file('/test-abi2/manifest.json');
|
|
manifest2.createSync(recursive: true);
|
|
manifest2.writeAsStringSync(r'''
|
|
{ "loadingUnits": [
|
|
{ "id": 1, "path": "\/test-abi2\/app.so", "libraries": [
|
|
"dart:core"]}
|
|
,
|
|
{ "id": 2, "path": "\/test-abi2\/app.so-2.part.so", "libraries": [
|
|
"lib2"]}
|
|
,
|
|
{ "id": 3, "path": "\/test-abi2\/app.so-3.part.so", "libraries": [
|
|
"lib3", "lib4"]}
|
|
] }
|
|
|
|
''', flush: true);
|
|
final List<LoadingUnit> loadingUnits =
|
|
LoadingUnit.parseGeneratedLoadingUnits(fileSystem.directory('/'), BufferLogger.test());
|
|
expect(loadingUnits.length, 4); // base module (id 1) is not parsed.
|
|
|
|
expect(loadingUnits[0].id, 2);
|
|
expect(loadingUnits[0].path, '/test-abi2/app.so-2.part.so');
|
|
expect(loadingUnits[0].libraries.length, 1);
|
|
expect(loadingUnits[0].libraries[0], 'lib2');
|
|
expect(loadingUnits[1].id, 3);
|
|
expect(loadingUnits[1].path, '/test-abi2/app.so-3.part.so');
|
|
expect(loadingUnits[1].libraries.length, 2);
|
|
expect(loadingUnits[1].libraries[0], 'lib3');
|
|
expect(loadingUnits[1].libraries[1], 'lib4');
|
|
|
|
expect(loadingUnits[2].id, 2);
|
|
expect(loadingUnits[2].path, '/test-abi1/app.so-2.part.so');
|
|
expect(loadingUnits[2].libraries.length, 1);
|
|
expect(loadingUnits[2].libraries[0], 'lib2');
|
|
expect(loadingUnits[3].id, 3);
|
|
expect(loadingUnits[3].path, '/test-abi1/app.so-3.part.so');
|
|
expect(loadingUnits[3].libraries.length, 2);
|
|
expect(loadingUnits[3].libraries[0], 'lib3');
|
|
expect(loadingUnits[3].libraries[1], 'lib4');
|
|
});
|
|
|
|
testWithoutContext('parseGeneratedLoadingUnits only parses provided abis', () {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final File manifest1 = fileSystem.file('/test-abi1/manifest.json');
|
|
manifest1.createSync(recursive: true);
|
|
manifest1.writeAsStringSync(r'''
|
|
{ "loadingUnits": [
|
|
{ "id": 1, "path": "\/test-abi1\/app.so", "libraries": [
|
|
"dart:core"]}
|
|
,
|
|
{ "id": 2, "path": "\/test-abi1\/app.so-2.part.so", "libraries": [
|
|
"lib2"]}
|
|
,
|
|
{ "id": 3, "path": "\/test-abi1\/app.so-3.part.so", "libraries": [
|
|
"lib3", "lib4"]}
|
|
] }
|
|
|
|
''', flush: true);
|
|
final File manifest2 = fileSystem.file('/test-abi2/manifest.json');
|
|
manifest2.createSync(recursive: true);
|
|
manifest2.writeAsStringSync(r'''
|
|
{ "loadingUnits": [
|
|
{ "id": 1, "path": "\/test-abi2\/app.so", "libraries": [
|
|
"dart:core"]}
|
|
,
|
|
{ "id": 2, "path": "\/test-abi2\/app.so-2.part.so", "libraries": [
|
|
"lib2"]}
|
|
,
|
|
{ "id": 3, "path": "\/test-abi2\/app.so-3.part.so", "libraries": [
|
|
"lib3", "lib4"]}
|
|
] }
|
|
|
|
''', flush: true);
|
|
final List<LoadingUnit> loadingUnits =
|
|
LoadingUnit.parseGeneratedLoadingUnits(fileSystem.directory('/'), BufferLogger.test(), abis: <String>['test-abi2']);
|
|
expect(loadingUnits.length, 2); // base module (id 1) is not parsed.
|
|
|
|
expect(loadingUnits[0].id, 2);
|
|
expect(loadingUnits[0].path, '/test-abi2/app.so-2.part.so');
|
|
expect(loadingUnits[0].libraries.length, 1);
|
|
expect(loadingUnits[0].libraries[0], 'lib2');
|
|
expect(loadingUnits[1].id, 3);
|
|
expect(loadingUnits[1].path, '/test-abi2/app.so-3.part.so');
|
|
expect(loadingUnits[1].libraries.length, 2);
|
|
expect(loadingUnits[1].libraries[0], 'lib3');
|
|
expect(loadingUnits[1].libraries[1], 'lib4');
|
|
});
|
|
|
|
testWithoutContext('parseGeneratedLoadingUnits returns empty when no manifest files exist', () {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final List<LoadingUnit> loadingUnits =
|
|
LoadingUnit.parseGeneratedLoadingUnits(fileSystem.directory('/'), BufferLogger.test());
|
|
expect(loadingUnits.isEmpty, true);
|
|
expect(loadingUnits.length, 0);
|
|
});
|
|
});
|
|
}
|