Skip to content

Commit 2299ec7

Browse files
authored
Set default flutter source directory for gradle builds (#142934)
See [#142498](flutter/flutter#142498 (comment)) See this discussion: https://discord.com/channels/608014603317936148/1186378330178601000
1 parent 4b0abc7 commit 2299ec7

2 files changed

Lines changed: 73 additions & 3 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class FlutterExtension {
6767
* Specifies the relative directory to the Flutter project directory.
6868
* In an app project, this is ../.. since the app's Gradle build file is under android/app.
6969
*/
70-
String source
70+
String source = "../.."
7171

7272
/** Allows to override the target file. Otherwise, the target is lib/main.dart. */
7373
String target
@@ -834,7 +834,7 @@ class FlutterPlugin implements Plugin<Project> {
834834
}
835835

836836
private Properties getPluginList() {
837-
File pluginsFile = new File(project.projectDir.parentFile.parentFile, '.flutter-plugins')
837+
File pluginsFile = new File(getFlutterSourceDirectory(), '.flutter-plugins')
838838
Properties allPlugins = readPropertiesIfExist(pluginsFile)
839839
Properties androidPlugins = new Properties()
840840
allPlugins.each { name, path ->
@@ -871,7 +871,7 @@ class FlutterPlugin implements Plugin<Project> {
871871
// This means, `plugin-a` depends on `plugin-b` and `plugin-c`.
872872
// `plugin-b` depends on `plugin-c`.
873873
// `plugin-c` doesn't depend on anything.
874-
File pluginsDependencyFile = new File(project.projectDir.parentFile.parentFile, '.flutter-plugins-dependencies')
874+
File pluginsDependencyFile = new File(getFlutterSourceDirectory(), '.flutter-plugins-dependencies')
875875
if (pluginsDependencyFile.exists()) {
876876
def object = new JsonSlurper().parseText(pluginsDependencyFile.text)
877877
assert(object instanceof Map)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_tools/src/base/file_system.dart';
6+
import 'package:flutter_tools/src/base/io.dart';
7+
import 'package:flutter_tools/src/cache.dart';
8+
9+
import '../src/common.dart';
10+
import 'test_data/deferred_components_project.dart';
11+
import 'test_data/project.dart';
12+
import 'test_utils.dart';
13+
14+
void main() {
15+
late Directory tempDir;
16+
17+
setUp(() {
18+
Cache.flutterRoot = getFlutterRoot();
19+
tempDir =
20+
createResolvedTempDirectorySync('flutter_gradle_source_path_test.');
21+
});
22+
23+
tearDown(() async {
24+
tryToDelete(tempDir);
25+
});
26+
27+
test('gradle task builds without setting a source path in app/build.gradle',
28+
() async {
29+
final Project project = DeferredComponentsProject(
30+
MissingFlutterSourcePathDeferredComponentsConfig(),
31+
);
32+
final String flutterBin = fileSystem.path.join(
33+
getFlutterRoot(),
34+
'bin',
35+
'flutter',
36+
);
37+
38+
final Directory exampleAppDir = tempDir.childDirectory('example');
39+
await project.setUpIn(exampleAppDir);
40+
41+
// Run flutter build apk to build example project.
42+
final ProcessResult buildApkResult = processManager.runSync(<String>[
43+
flutterBin,
44+
...getLocalEngineArguments(),
45+
'build',
46+
'apk',
47+
'--debug',
48+
], workingDirectory: exampleAppDir.path);
49+
50+
expect(buildApkResult, const ProcessResultMatcher());
51+
});
52+
}
53+
54+
class MissingFlutterSourcePathDeferredComponentsConfig
55+
extends BasicDeferredComponentsConfig {
56+
final String _flutterSourcePath = '''
57+
flutter {
58+
source '../..'
59+
}
60+
''';
61+
62+
@override
63+
String get appBuild {
64+
if (!super.appBuild.contains(_flutterSourcePath)) {
65+
throw Exception(
66+
'Flutter source path not found in original configuration!');
67+
}
68+
return super.appBuild.replaceAll(_flutterSourcePath, '');
69+
}
70+
}

0 commit comments

Comments
 (0)