From 2299ec781f5941782caad6c9b826f214e339ffb2 Mon Sep 17 00:00:00 2001 From: Gustl22 Date: Thu, 8 Feb 2024 23:28:39 +0100 Subject: [PATCH] Set default flutter source directory for gradle builds (#142934) See [#142498](https://github.com/flutter/flutter/pull/142498#discussion_r1478602032) See this discussion: https://discord.com/channels/608014603317936148/1186378330178601000 --- .../gradle/src/main/groovy/flutter.groovy | 6 +- ...droid_gradle_flutter_source_path_test.dart | 70 +++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 packages/flutter_tools/test/integration.shard/android_gradle_flutter_source_path_test.dart diff --git a/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy b/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy index c87542574d..633e20a80b 100644 --- a/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy +++ b/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy @@ -67,7 +67,7 @@ class FlutterExtension { * Specifies the relative directory to the Flutter project directory. * In an app project, this is ../.. since the app's Gradle build file is under android/app. */ - String source + String source = "../.." /** Allows to override the target file. Otherwise, the target is lib/main.dart. */ String target @@ -834,7 +834,7 @@ class FlutterPlugin implements Plugin { } private Properties getPluginList() { - File pluginsFile = new File(project.projectDir.parentFile.parentFile, '.flutter-plugins') + File pluginsFile = new File(getFlutterSourceDirectory(), '.flutter-plugins') Properties allPlugins = readPropertiesIfExist(pluginsFile) Properties androidPlugins = new Properties() allPlugins.each { name, path -> @@ -871,7 +871,7 @@ class FlutterPlugin implements Plugin { // This means, `plugin-a` depends on `plugin-b` and `plugin-c`. // `plugin-b` depends on `plugin-c`. // `plugin-c` doesn't depend on anything. - File pluginsDependencyFile = new File(project.projectDir.parentFile.parentFile, '.flutter-plugins-dependencies') + File pluginsDependencyFile = new File(getFlutterSourceDirectory(), '.flutter-plugins-dependencies') if (pluginsDependencyFile.exists()) { def object = new JsonSlurper().parseText(pluginsDependencyFile.text) assert(object instanceof Map) diff --git a/packages/flutter_tools/test/integration.shard/android_gradle_flutter_source_path_test.dart b/packages/flutter_tools/test/integration.shard/android_gradle_flutter_source_path_test.dart new file mode 100644 index 0000000000..814cdfec9a --- /dev/null +++ b/packages/flutter_tools/test/integration.shard/android_gradle_flutter_source_path_test.dart @@ -0,0 +1,70 @@ +// 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: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_data/deferred_components_project.dart'; +import 'test_data/project.dart'; +import 'test_utils.dart'; + +void main() { + late Directory tempDir; + + setUp(() { + Cache.flutterRoot = getFlutterRoot(); + tempDir = + createResolvedTempDirectorySync('flutter_gradle_source_path_test.'); + }); + + tearDown(() async { + tryToDelete(tempDir); + }); + + test('gradle task builds without setting a source path in app/build.gradle', + () async { + final Project project = DeferredComponentsProject( + MissingFlutterSourcePathDeferredComponentsConfig(), + ); + final String flutterBin = fileSystem.path.join( + getFlutterRoot(), + 'bin', + 'flutter', + ); + + final Directory exampleAppDir = tempDir.childDirectory('example'); + await project.setUpIn(exampleAppDir); + + // Run flutter build apk to build example project. + final ProcessResult buildApkResult = processManager.runSync([ + flutterBin, + ...getLocalEngineArguments(), + 'build', + 'apk', + '--debug', + ], workingDirectory: exampleAppDir.path); + + expect(buildApkResult, const ProcessResultMatcher()); + }); +} + +class MissingFlutterSourcePathDeferredComponentsConfig + extends BasicDeferredComponentsConfig { + final String _flutterSourcePath = ''' + flutter { + source '../..' + } +'''; + + @override + String get appBuild { + if (!super.appBuild.contains(_flutterSourcePath)) { + throw Exception( + 'Flutter source path not found in original configuration!'); + } + return super.appBuild.replaceAll(_flutterSourcePath, ''); + } +}