Skip to content

Commit 21766a4

Browse files
Reverts "Support conditional bundling of assets based on --flavor" (#139787)
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 flutter/flutter#21682. Resolves flutter/flutter#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](flutter/flutter#132985 (comment)) 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
1 parent e31ac61 commit 21766a4

30 files changed

Lines changed: 215 additions & 874 deletions

‎dev/bots/pubspec.yaml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ dependencies:
5858
source_maps: 0.10.12 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
5959
source_span: 1.10.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
6060
stack_trace: 1.11.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
61-
standard_message_codec: 0.0.1+4 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
6261
stream_channel: 2.1.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
6362
string_scanner: 1.2.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
6463
term_glyph: 1.2.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
@@ -75,4 +74,4 @@ dependencies:
7574
dev_dependencies:
7675
test_api: 0.6.1
7776

78-
# PUBSPEC CHECKSUM: b875
77+
# PUBSPEC CHECKSUM: 29d2

‎dev/devicelab/bin/tasks/flavors_test.dart‎

Lines changed: 23 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,44 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:io' show File;
6-
import 'dart:typed_data';
7-
8-
import 'package:collection/collection.dart';
95
import 'package:flutter_devicelab/framework/devices.dart';
106
import 'package:flutter_devicelab/framework/framework.dart';
117
import 'package:flutter_devicelab/framework/task_result.dart';
128
import 'package:flutter_devicelab/framework/utils.dart';
139
import 'package:flutter_devicelab/tasks/integration_tests.dart';
1410
import 'package:path/path.dart' as path;
15-
import 'package:standard_message_codec/standard_message_codec.dart';
1611

1712
Future<void> main() async {
1813
deviceOperatingSystem = DeviceOperatingSystem.android;
1914
await task(() async {
2015
await createFlavorsTest().call();
2116
await createIntegrationTestFlavorsTest().call();
2217

23-
final String projectPath = '${flutterDirectory.path}/dev/integration_tests/flavors';
2418
final TaskResult installTestsResult = await inDirectory(
25-
projectPath,
19+
'${flutterDirectory.path}/dev/integration_tests/flavors',
2620
() async {
27-
final List<TaskResult> testResults = <TaskResult>[
28-
await _testInstallDebugPaidFlavor(projectPath),
29-
await _testInstallBogusFlavor(),
30-
];
31-
32-
final TaskResult? firstInstallFailure = testResults
33-
.firstWhereOrNull((TaskResult element) => element.failed);
34-
35-
if (firstInstallFailure != null) {
36-
return firstInstallFailure;
21+
await flutter(
22+
'install',
23+
options: <String>['--debug', '--flavor', 'paid'],
24+
);
25+
await flutter(
26+
'install',
27+
options: <String>['--debug', '--flavor', 'paid', '--uninstall-only'],
28+
);
29+
30+
final StringBuffer stderr = StringBuffer();
31+
await evalFlutter(
32+
'install',
33+
canFail: true,
34+
stderr: stderr,
35+
options: <String>['--flavor', 'bogus'],
36+
);
37+
38+
final String stderrString = stderr.toString();
39+
final String expectedApkPath = path.join('build', 'app', 'outputs', 'flutter-apk', 'app-bogus-release.apk');
40+
if (!stderrString.contains('"$expectedApkPath" does not exist.')) {
41+
print(stderrString);
42+
return TaskResult.failure('Should not succeed with bogus flavor');
3743
}
3844

3945
return TaskResult.success(null);
@@ -43,49 +49,3 @@ Future<void> main() async {
4349
return installTestsResult;
4450
});
4551
}
46-
47-
// Ensures installation works. Also tests asset bundling while we are at it.
48-
Future<TaskResult> _testInstallDebugPaidFlavor(String projectDir) async {
49-
await evalFlutter(
50-
'install',
51-
options: <String>['--debug', '--flavor', 'paid'],
52-
);
53-
54-
final Uint8List assetManifestFileData = File(
55-
path.join(projectDir, 'build', 'app', 'intermediates', 'assets', 'paidDebug', 'flutter_assets', 'AssetManifest.bin'),
56-
).readAsBytesSync();
57-
58-
final Map<Object?, Object?> assetManifest = const StandardMessageCodec()
59-
.decodeMessage(ByteData.sublistView(assetManifestFileData)) as Map<Object?, Object?>;
60-
61-
if (assetManifest.containsKey('assets/free/free.txt')) {
62-
return TaskResult.failure('Assets declared with a flavor not equal to the '
63-
'argued --flavor value should not be bundled.');
64-
}
65-
66-
await flutter(
67-
'install',
68-
options: <String>['--debug', '--flavor', 'paid', '--uninstall-only'],
69-
);
70-
71-
return TaskResult.success(null);
72-
}
73-
74-
Future<TaskResult> _testInstallBogusFlavor() async {
75-
final StringBuffer stderr = StringBuffer();
76-
await evalFlutter(
77-
'install',
78-
canFail: true,
79-
stderr: stderr,
80-
options: <String>['--flavor', 'bogus'],
81-
);
82-
83-
final String stderrString = stderr.toString();
84-
final String expectedApkPath = path.join('build', 'app', 'outputs', 'flutter-apk', 'app-bogus-release.apk');
85-
if (!stderrString.contains('"$expectedApkPath" does not exist.')) {
86-
print(stderrString);
87-
return TaskResult.failure('Should not succeed with bogus flavor');
88-
}
89-
90-
return TaskResult.success(null);
91-
}

‎dev/devicelab/bin/tasks/flavors_test_ios.dart‎

Lines changed: 21 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,41 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:io';
6-
import 'dart:typed_data';
7-
8-
import 'package:collection/collection.dart';
95
import 'package:flutter_devicelab/framework/devices.dart';
106
import 'package:flutter_devicelab/framework/framework.dart';
117
import 'package:flutter_devicelab/framework/task_result.dart';
128
import 'package:flutter_devicelab/framework/utils.dart';
139
import 'package:flutter_devicelab/tasks/integration_tests.dart';
14-
import 'package:path/path.dart' as path;
15-
import 'package:standard_message_codec/standard_message_codec.dart';
1610

1711
Future<void> main() async {
1812
deviceOperatingSystem = DeviceOperatingSystem.ios;
1913
await task(() async {
2014
await createFlavorsTest().call();
2115
await createIntegrationTestFlavorsTest().call();
2216
// test install and uninstall of flavors app
23-
final String projectDir = '${flutterDirectory.path}/dev/integration_tests/flavors';
2417
final TaskResult installTestsResult = await inDirectory(
25-
projectDir,
18+
'${flutterDirectory.path}/dev/integration_tests/flavors',
2619
() async {
27-
final List<TaskResult> testResults = <TaskResult>[
28-
await _testInstallDebugPaidFlavor(projectDir),
29-
await _testInstallBogusFlavor(),
30-
];
31-
32-
final TaskResult? firstInstallFailure = testResults
33-
.firstWhereOrNull((TaskResult element) => element.failed);
34-
35-
if (firstInstallFailure != null) {
36-
return firstInstallFailure;
20+
await flutter(
21+
'install',
22+
options: <String>['--flavor', 'paid'],
23+
);
24+
await flutter(
25+
'install',
26+
options: <String>['--flavor', 'paid', '--uninstall-only'],
27+
);
28+
final StringBuffer stderr = StringBuffer();
29+
await evalFlutter(
30+
'install',
31+
canFail: true,
32+
stderr: stderr,
33+
options: <String>['--flavor', 'bogus'],
34+
);
35+
36+
final String stderrString = stderr.toString();
37+
if (!stderrString.contains('The Xcode project defines schemes: free, paid')) {
38+
print(stderrString);
39+
return TaskResult.failure('Should not succeed with bogus flavor');
3740
}
3841

3942
return TaskResult.success(null);
@@ -43,47 +46,3 @@ Future<void> main() async {
4346
return installTestsResult;
4447
});
4548
}
46-
47-
Future<TaskResult> _testInstallDebugPaidFlavor(String projectDir) async {
48-
await evalFlutter(
49-
'install',
50-
options: <String>['--flavor', 'paid'],
51-
);
52-
53-
final Uint8List assetManifestFileData = File(
54-
path.join(projectDir, 'build', 'ios', 'Release-iphoneos', 'App.framework', 'flutter_assets', 'AssetManifest.bin'),
55-
).readAsBytesSync();
56-
57-
final Map<Object?, Object?> assetManifest = const StandardMessageCodec()
58-
.decodeMessage(ByteData.sublistView(assetManifestFileData)) as Map<Object?, Object?>;
59-
60-
if (assetManifest.containsKey('assets/free/free.txt')) {
61-
return TaskResult.failure('Assets declared with a flavor not equal to the '
62-
'argued --flavor value should not be bundled.');
63-
}
64-
65-
await flutter(
66-
'install',
67-
options: <String>['--flavor', 'paid', '--uninstall-only'],
68-
);
69-
70-
return TaskResult.success(null);
71-
}
72-
73-
Future<TaskResult> _testInstallBogusFlavor() async {
74-
final StringBuffer stderr = StringBuffer();
75-
await evalFlutter(
76-
'install',
77-
canFail: true,
78-
stderr: stderr,
79-
options: <String>['--flavor', 'bogus'],
80-
);
81-
82-
final String stderrString = stderr.toString();
83-
if (!stderrString.contains('The Xcode project defines schemes: free, paid')) {
84-
print(stderrString);
85-
return TaskResult.failure('Should not succeed with bogus flavor');
86-
}
87-
88-
return TaskResult.success(null);
89-
}

‎dev/devicelab/pubspec.yaml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ dependencies:
2424
web: 0.4.0
2525
webkit_inspection_protocol: 1.2.1
2626
xml: 6.5.0
27-
standard_message_codec: 0.0.1+4
2827

2928
_discoveryapis_commons: 1.0.6 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
3029
async: 2.11.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
@@ -73,4 +72,4 @@ dev_dependencies:
7372
watcher: 1.1.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
7473
web_socket_channel: 2.4.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
7574

76-
# PUBSPEC CHECKSUM: 70e2
75+
# PUBSPEC CHECKSUM: 6040

‎dev/integration_tests/flavors/assets/common/common.txt‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎dev/integration_tests/flavors/assets/free/free.txt‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎dev/integration_tests/flavors/assets/paid/paid.txt‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎dev/integration_tests/flavors/assets/premium/premium.txt‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎dev/integration_tests/flavors/pubspec.yaml‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,5 @@ dev_dependencies:
7575

7676
flutter:
7777
uses-material-design: true
78-
assets:
79-
- assets/common/common.txt
80-
- path: assets/paid/
81-
flavors:
82-
- paid
83-
- path: assets/free/
84-
flavors:
85-
- free
8678

8779
# PUBSPEC CHECKSUM: 6bd3

‎packages/flutter_tools/bin/xcode_backend.dart‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,6 @@ class Context {
401401
'-dTargetPlatform=ios',
402402
'-dTargetFile=$targetPath',
403403
'-dBuildMode=$buildMode',
404-
if (environment['FLAVOR'] != null) '-dFlavor=${environment['FLAVOR']}',
405404
'-dIosArchs=${environment['ARCHS'] ?? ''}',
406405
'-dSdkRoot=${environment['SDKROOT'] ?? ''}',
407406
'-dSplitDebugInfo=${environment['SPLIT_DEBUG_INFO'] ?? ''}',

0 commit comments

Comments
 (0)