Skip to content

Commit 9fe9267

Browse files
camsim99maheshj01
authored andcommitted
[Android] Actually remove dev dependencies from release builds (flutter#161343)
Revises flutter#158026 to fix flutter#160407. Makes a number of fixes: - Fixes Groovy logic that attempted to remove dev dependencies from release builds to use `<buildType>Api` versus `api` and loop to configure the project dependencies per build type - Adds back dependency on embedding to plugin projects since this is needed regardless (the plugin may not be included in a particularly typed build but it still needs it for where it's included) - Fixes integration test to throw and error upon failure, check right APK for the release build it's testing, and configure the flag need for `.flutter-plugin-dependencies` to mark plugins as dev dependencies as expected - Uses @matanlurey's [patch](flutter#160407 (comment)) to remove dev dependency from the `GeneratedPluginRegistrant` in release mode ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 6e3fb58 commit 9fe9267

File tree

5 files changed

+37
-20
lines changed

5 files changed

+37
-20
lines changed

dev/devicelab/bin/tasks/android_release_builds_exclude_dev_dependencies_test.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Future<void> main() async {
1515
await task(() async {
1616
try {
1717
await runProjectTest((FlutterProject flutterProject) async {
18+
// Enable plugins being marked as dev dependncies in the .flutter-plugins-dependencies file.
19+
await flutter('config', options: <String>['--explicit-package-dependencies']);
20+
1821
// Create dev_dependency plugin to use for test.
1922
final Directory tempDir = Directory.systemTemp.createTempSync(
2023
'android_release_builds_exclude_dev_dependencies_test.',
@@ -50,7 +53,7 @@ Future<void> main() async {
5053
'app',
5154
'outputs',
5255
'flutter-apk',
53-
'app-debug.apk',
56+
'app-$buildMode.apk',
5457
),
5558
);
5659
if (!apk.existsSync()) {
@@ -66,7 +69,7 @@ Future<void> main() async {
6669
final bool apkIncludesDevDependencyAsExpected =
6770
isTestingReleaseMode ? !apkIncludesDevDependency : apkIncludesDevDependency;
6871
if (!apkIncludesDevDependencyAsExpected) {
69-
return TaskResult.failure(
72+
throw TaskResult.failure(
7073
'Expected to${isTestingReleaseMode ? ' not' : ''} find dev_dependency_plugin in APK built with debug mode but did${isTestingReleaseMode ? '' : ' not'}.',
7174
);
7275
}

packages/flutter_tools/gradle/src/main/groovy/flutter.groovy

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -738,13 +738,12 @@ class FlutterPlugin implements Plugin<Project> {
738738
// compile/target/min sdk values.
739739
pluginProject.extensions.create("flutter", FlutterExtension)
740740

741-
// Add plugin dependency to the app project.
742-
project.android.buildTypes.each { buildType ->
743-
String flutterBuildMode = buildModeFor(buildType)
744-
if (flutterBuildMode != "release" || !pluginObject.dev_dependency) {
745-
// Only add dependency on dev dependencies in non-release builds.
746-
project.dependencies {
747-
api(pluginProject)
741+
// Add plugin dependency to the app project. We only want to add dependency
742+
// for dev dependencies in non-release builds.
743+
project.afterEvaluate {
744+
project.android.buildTypes.all { buildType ->
745+
if (!pluginObject.dev_dependency || buildType.name != 'release') {
746+
project.dependencies.add("${buildType.name}Api", pluginProject)
748747
}
749748
}
750749
}
@@ -760,12 +759,6 @@ class FlutterPlugin implements Plugin<Project> {
760759
if (!pluginProject.hasProperty("android")) {
761760
return
762761
}
763-
if (flutterBuildMode == "release" && pluginObject.dev_dependency) {
764-
// This plugin is a dev dependency and will not be included in
765-
// the release build, so no need to add the embedding
766-
// dependency to it.
767-
return
768-
}
769762
// Copy build types from the app to the plugin.
770763
// This allows to build apps with plugins and custom build types or flavors.
771764
pluginProject.android.buildTypes {

packages/flutter_tools/lib/src/flutter_plugins.dart

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,18 +356,27 @@ public final class GeneratedPluginRegistrant {
356356
}
357357
''';
358358

359-
List<Map<String, Object?>> _extractPlatformMaps(List<Plugin> plugins, String type) {
359+
List<Map<String, Object?>> _extractPlatformMaps(Iterable<Plugin> plugins, String type) {
360360
return <Map<String, Object?>>[
361361
for (final Plugin plugin in plugins)
362362
if (plugin.platforms[type] case final PluginPlatform platformPlugin) platformPlugin.toMap(),
363363
];
364364
}
365365

366-
Future<void> _writeAndroidPluginRegistrant(FlutterProject project, List<Plugin> plugins) async {
367-
final List<Plugin> methodChannelPlugins = _filterMethodChannelPlugins(
366+
Future<void> _writeAndroidPluginRegistrant(
367+
FlutterProject project,
368+
List<Plugin> plugins, {
369+
required bool releaseMode,
370+
}) async {
371+
Iterable<Plugin> methodChannelPlugins = _filterMethodChannelPlugins(
368372
plugins,
369373
AndroidPlugin.kConfigKey,
370374
);
375+
// TODO(camsim99): Remove dev dependencies from release builds for all platforms. See https://github.com/flutter/flutter/issues/161348.
376+
if (releaseMode) {
377+
methodChannelPlugins = methodChannelPlugins.where((Plugin p) => !p.isDevDependency);
378+
}
379+
371380
final List<Map<String, Object?>> androidPlugins = _extractPlatformMaps(
372381
methodChannelPlugins,
373382
AndroidPlugin.kConfigKey,
@@ -1214,6 +1223,7 @@ Future<void> injectPlugins(
12141223
bool windowsPlatform = false,
12151224
Iterable<String>? allowedPlugins,
12161225
DarwinDependencyManagement? darwinDependencyManagement,
1226+
bool? releaseMode,
12171227
}) async {
12181228
final List<Plugin> plugins = await findPlugins(project);
12191229
final Map<String, List<Plugin>> pluginsByPlatform = _resolvePluginImplementations(
@@ -1222,7 +1232,11 @@ Future<void> injectPlugins(
12221232
);
12231233

12241234
if (androidPlatform) {
1225-
await _writeAndroidPluginRegistrant(project, pluginsByPlatform[AndroidPlugin.kConfigKey]!);
1235+
await _writeAndroidPluginRegistrant(
1236+
project,
1237+
pluginsByPlatform[AndroidPlugin.kConfigKey]!,
1238+
releaseMode: releaseMode ?? false,
1239+
);
12261240
}
12271241
if (iosPlatform) {
12281242
await _writeIOSPluginRegistrant(project, pluginsByPlatform[IOSPlugin.kConfigKey]!);

packages/flutter_tools/lib/src/project.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ class FlutterProject {
332332
Future<void> regeneratePlatformSpecificTooling({
333333
DeprecationBehavior deprecationBehavior = DeprecationBehavior.none,
334334
Iterable<String>? allowedPlugins,
335+
bool? releaseMode,
335336
}) async {
336337
return ensureReadyForPlatformSpecificTooling(
337338
androidPlatform: android.existsSync(),
@@ -344,6 +345,7 @@ class FlutterProject {
344345
webPlatform: featureFlags.isWebEnabled && web.existsSync(),
345346
deprecationBehavior: deprecationBehavior,
346347
allowedPlugins: allowedPlugins,
348+
releaseMode: releaseMode,
347349
);
348350
}
349351

@@ -358,6 +360,7 @@ class FlutterProject {
358360
bool webPlatform = false,
359361
DeprecationBehavior deprecationBehavior = DeprecationBehavior.none,
360362
Iterable<String>? allowedPlugins,
363+
bool? releaseMode,
361364
}) async {
362365
if (!directory.existsSync() || isPlugin) {
363366
return;
@@ -389,6 +392,7 @@ class FlutterProject {
389392
macOSPlatform: macOSPlatform,
390393
windowsPlatform: windowsPlatform,
391394
allowedPlugins: allowedPlugins,
395+
releaseMode: releaseMode,
392396
);
393397
}
394398

packages/flutter_tools/lib/src/runner/flutter_command.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1907,7 +1907,10 @@ Run 'flutter -h' (or 'flutter <command> -h') for available flutter commands and
19071907
// The preview device does not currently support any plugins.
19081908
allowedPlugins = PreviewDevice.supportedPubPlugins;
19091909
}
1910-
await project.regeneratePlatformSpecificTooling(allowedPlugins: allowedPlugins);
1910+
await project.regeneratePlatformSpecificTooling(
1911+
allowedPlugins: allowedPlugins,
1912+
releaseMode: featureFlags.isExplicitPackageDependenciesEnabled && getBuildMode().isRelease,
1913+
);
19111914
if (reportNullSafety) {
19121915
await _sendNullSafetyAnalyticsEvents(project);
19131916
}

0 commit comments

Comments
 (0)