Skip to content

Commit 937965f

Browse files
authored
Add integration test for Gradle-initiated android builds with flavors (flutter#163737)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> This request is a subset of flutter#157871 and follows up flutter#162907. flutter#162907 actually fixed behaviour of building with native tooling not only for iOS/macOS, but also for Android builds too: now on master, if you start build with `gradle` instead of `flutter build ...` – you also get the correct behaviour and `appFlavor` won't be null in runtime. Tests in this PR will check exactly this behavior. Another diff is the change in the method of obtaining flavor at runtime inside test project. Before this change, method channels were used for this, after – `appFlavor` constant. Both methods do more or less the same thing right now, but they may diverge in future, so I guess this is the right way to check correctness here. Also, this change was requested and approved by Andrew in flutter#157871 (comment) issue for checklist: flutter#155951 New test was tested here: https://ci.chromium.org/ui/p/flutter/builders/try/Linux_pixel_7pro%20flavors_test/63/overview ## 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. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- 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 1064439 commit 937965f

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

dev/devicelab/bin/tasks/flavors_test.dart

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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;
5+
import 'dart:io' show File, Platform;
66
import 'dart:typed_data';
77

88
import 'package:collection/collection.dart';
@@ -34,6 +34,8 @@ Future<void> main() async {
3434
return firstInstallFailure ?? TaskResult.success(null);
3535
});
3636

37+
await _testFlavorsWhenBuildStartsWithGradle(projectPath);
38+
3739
return installTestsResult;
3840
});
3941
}
@@ -106,3 +108,40 @@ Future<TaskResult> _testInstallBogusFlavor() async {
106108

107109
return TaskResult.success(null);
108110
}
111+
112+
Future<TaskResult> _testFlavorsWhenBuildStartsWithGradle(String projectDir) async {
113+
final String gradlew = Platform.isWindows ? 'gradlew.bat' : 'gradlew';
114+
final String gradlewExecutable = Platform.isWindows ? '.\\$gradlew' : './$gradlew';
115+
116+
final String androidDirPath = '$projectDir/android';
117+
final StringBuffer stdout = StringBuffer();
118+
119+
// Prebuild the project to generate the Android gradle wrapper files.
120+
await inDirectory(projectDir, () async {
121+
await flutter('build', options: <String>['apk', '--config-only']);
122+
});
123+
124+
await inDirectory(androidDirPath, () async {
125+
await exec(gradlewExecutable, <String>['clean']);
126+
await exec(gradlewExecutable, <String>[':app:assemblePaidDebug', '--info'], output: stdout);
127+
});
128+
129+
final String stdoutString = stdout.toString();
130+
131+
if (!stdoutString.contains('-dFlavor=paid')) {
132+
return TaskResult.failure('Expected to see -dFlavor=paid in the gradle verbose output');
133+
}
134+
135+
final String appPath = path.join(
136+
projectDir,
137+
'build',
138+
'app',
139+
'outputs',
140+
'flutter-apk',
141+
'app-paid-debug.apk',
142+
);
143+
144+
return createFlavorsTest(
145+
extraOptions: <String>['--flavor', 'paid', '--use-application-binary=$appPath'],
146+
).call();
147+
}

dev/integration_tests/flavors/android/app/src/main/java/com/yourcompany/flavors/MainActivity.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,6 @@
44

55
package com.yourcompany.flavors;
66

7-
import android.os.Bundle;
8-
9-
import androidx.annotation.NonNull;
107
import io.flutter.embedding.android.FlutterActivity;
11-
import io.flutter.embedding.engine.FlutterEngine;
12-
import io.flutter.plugin.common.MethodCall;
13-
import io.flutter.plugin.common.MethodChannel;
14-
import io.flutter.plugins.GeneratedPluginRegistrant;
158

16-
public class MainActivity extends FlutterActivity {
17-
@Override
18-
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
19-
GeneratedPluginRegistrant.registerWith(flutterEngine);
20-
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), "flavor")
21-
.setMethodCallHandler(
22-
(call, result) -> {
23-
result.success(BuildConfig.FLAVOR);
24-
}
25-
);
26-
}
27-
}
9+
public class MainActivity extends FlutterActivity {}

dev/integration_tests/flavors/lib/main.dart

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,11 @@ class Flavor extends StatefulWidget {
2323
}
2424

2525
class _FlavorState extends State<Flavor> {
26-
String? _flavor;
27-
28-
@override
29-
void initState() {
30-
super.initState();
31-
const MethodChannel('flavor').invokeMethod<String>('getFlavor').then((String? flavor) {
32-
setState(() {
33-
_flavor = flavor;
34-
});
35-
});
36-
}
37-
3826
@override
3927
Widget build(BuildContext context) {
40-
return Directionality(
28+
return const Directionality(
4129
textDirection: TextDirection.ltr,
42-
child:
43-
_flavor == null
44-
? const Text('Awaiting flavor...')
45-
: Text(_flavor!, key: const ValueKey<String>('flavor')),
30+
child: Text(appFlavor ?? 'null', key: ValueKey<String>('flavor')),
4631
);
4732
}
4833
}

0 commit comments

Comments
 (0)