From 148a0b264b95088a566d7b6c44d35b1c4e0a7f4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E6=98=A5=E9=94=8B?= Date: Wed, 14 Sep 2022 23:47:16 +0800 Subject: [PATCH] let the plugin's output generated in flutter/.android/plugins_build_output/${androidPlugin.name} (#94645) --- .../gradle/module_plugin_loader.gradle | 15 +++- .../android_plugin_new_output_dir_test.dart | 73 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 packages/flutter_tools/test/integration.shard/android_plugin_new_output_dir_test.dart diff --git a/packages/flutter_tools/gradle/module_plugin_loader.gradle b/packages/flutter_tools/gradle/module_plugin_loader.gradle index d0b7287522..7f7bebe468 100644 --- a/packages/flutter_tools/gradle/module_plugin_loader.gradle +++ b/packages/flutter_tools/gradle/module_plugin_loader.gradle @@ -9,10 +9,12 @@ import groovy.json.JsonSlurper def moduleProjectRoot = project(':flutter').projectDir.parentFile.parentFile +def object = null; +String flutterModulePath = project(':flutter').projectDir.parentFile.getAbsolutePath() // Note: if this logic is changed, also change the logic in app_plugin_loader.gradle. def pluginsFile = new File(moduleProjectRoot, '.flutter-plugins-dependencies') if (pluginsFile.exists()) { - def object = new JsonSlurper().parseText(pluginsFile.text) + object = new JsonSlurper().parseText(pluginsFile.text) assert object instanceof Map assert object.plugins instanceof Map assert object.plugins.android instanceof List @@ -35,6 +37,17 @@ if (pluginsFile.exists()) { gradle.getGradle().projectsLoaded { g -> g.rootProject.beforeEvaluate { p -> + p.subprojects { subproject -> + if (object != null && object.plugins != null && object.plugins.android != null + && object.plugins.android.name.contains(subproject.name)) { + File androidPluginBuildOutputDir = new File(flutterModulePath + File.separator + + "plugins_build_output" + File.separator + subproject.name); + if (!androidPluginBuildOutputDir.exists()) { + androidPluginBuildOutputDir.mkdirs() + } + subproject.buildDir = androidPluginBuildOutputDir + } + } def _mainModuleName = binding.variables['mainModuleName'] if (_mainModuleName != null && !_mainModuleName.empty) { p.ext.mainModuleName = _mainModuleName diff --git a/packages/flutter_tools/test/integration.shard/android_plugin_new_output_dir_test.dart b/packages/flutter_tools/test/integration.shard/android_plugin_new_output_dir_test.dart new file mode 100644 index 0000000000..872e9c0cbf --- /dev/null +++ b/packages/flutter_tools/test/integration.shard/android_plugin_new_output_dir_test.dart @@ -0,0 +1,73 @@ +// 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 'dart:math'; + +import 'package:file_testing/file_testing.dart'; +import 'package:flutter_tools/src/base/file_system.dart'; +import 'package:flutter_tools/src/base/io.dart'; +import 'package:flutter_tools/src/cache.dart'; + +import '../src/common.dart'; +import 'test_utils.dart'; + +void main() { + + late Directory tempDir; + + setUp(() { + Cache.flutterRoot = getFlutterRoot(); + tempDir = createResolvedTempDirectorySync('android_plugin_new_output_dir_test.'); + }); + + tearDown(() async { + tryToDelete(tempDir); + }); + + test("error logged when plugin's build output dir was not private.", () async { + final String flutterBin = fileSystem.path.join( + getFlutterRoot(), + 'bin', + 'flutter', + ); + // create flutter module project + ProcessResult result = processManager.runSync([ + flutterBin, + ...getLocalEngineArguments(), + 'create', + '--template=module', + 'flutter_project' + ], workingDirectory: tempDir.path); + + final String projectPath = fileSystem.path.join(tempDir.path, 'flutter_project'); + + final File modulePubspec = fileSystem.file(fileSystem.path.join(projectPath, 'pubspec.yaml')); + String pubspecContent = modulePubspec.readAsStringSync(); + pubspecContent = pubspecContent.replaceFirst( + 'dependencies:', + ''' +dependencies: + image_picker: 0.8.5+3''', + ); + modulePubspec.writeAsStringSync(pubspecContent); + + // Run flutter build apk to build module example project + result = processManager.runSync([ + flutterBin, + ...getLocalEngineArguments(), + 'build', + 'aar', + '--target-platform=android-arm', + ], workingDirectory: projectPath); + + log(result.exitCode); + + // Check outputDir existed + final Directory outputDir = fileSystem.directory(fileSystem.path.join( + projectPath, '.android', 'plugins_build_output', 'image_picker_android' + )); + expect(outputDir, exists); + + }); +}