From 937965ffae4bdc7e484dac1174d8bec48b3c7de3 Mon Sep 17 00:00:00 2001 From: Mikhail Novoseltsev <51940183+Sameri11@users.noreply.github.com> Date: Mon, 24 Feb 2025 09:40:29 +0500 Subject: [PATCH] Add integration test for Gradle-initiated android builds with flavors (#163737) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This request is a subset of #157871 and follows up #162907. #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 https://github.com/flutter/flutter/pull/157871#discussion_r1931153339 issue for checklist: #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]. [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 --- dev/devicelab/bin/tasks/flavors_test.dart | 41 ++++++++++++++++++- .../com/yourcompany/flavors/MainActivity.java | 20 +-------- dev/integration_tests/flavors/lib/main.dart | 19 +-------- 3 files changed, 43 insertions(+), 37 deletions(-) diff --git a/dev/devicelab/bin/tasks/flavors_test.dart b/dev/devicelab/bin/tasks/flavors_test.dart index 3286b1e944..8da0ea0f58 100644 --- a/dev/devicelab/bin/tasks/flavors_test.dart +++ b/dev/devicelab/bin/tasks/flavors_test.dart @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:io' show File; +import 'dart:io' show File, Platform; import 'dart:typed_data'; import 'package:collection/collection.dart'; @@ -34,6 +34,8 @@ Future main() async { return firstInstallFailure ?? TaskResult.success(null); }); + await _testFlavorsWhenBuildStartsWithGradle(projectPath); + return installTestsResult; }); } @@ -106,3 +108,40 @@ Future _testInstallBogusFlavor() async { return TaskResult.success(null); } + +Future _testFlavorsWhenBuildStartsWithGradle(String projectDir) async { + final String gradlew = Platform.isWindows ? 'gradlew.bat' : 'gradlew'; + final String gradlewExecutable = Platform.isWindows ? '.\\$gradlew' : './$gradlew'; + + final String androidDirPath = '$projectDir/android'; + final StringBuffer stdout = StringBuffer(); + + // Prebuild the project to generate the Android gradle wrapper files. + await inDirectory(projectDir, () async { + await flutter('build', options: ['apk', '--config-only']); + }); + + await inDirectory(androidDirPath, () async { + await exec(gradlewExecutable, ['clean']); + await exec(gradlewExecutable, [':app:assemblePaidDebug', '--info'], output: stdout); + }); + + final String stdoutString = stdout.toString(); + + if (!stdoutString.contains('-dFlavor=paid')) { + return TaskResult.failure('Expected to see -dFlavor=paid in the gradle verbose output'); + } + + final String appPath = path.join( + projectDir, + 'build', + 'app', + 'outputs', + 'flutter-apk', + 'app-paid-debug.apk', + ); + + return createFlavorsTest( + extraOptions: ['--flavor', 'paid', '--use-application-binary=$appPath'], + ).call(); +} diff --git a/dev/integration_tests/flavors/android/app/src/main/java/com/yourcompany/flavors/MainActivity.java b/dev/integration_tests/flavors/android/app/src/main/java/com/yourcompany/flavors/MainActivity.java index 57566001ce..488c7fb7e6 100644 --- a/dev/integration_tests/flavors/android/app/src/main/java/com/yourcompany/flavors/MainActivity.java +++ b/dev/integration_tests/flavors/android/app/src/main/java/com/yourcompany/flavors/MainActivity.java @@ -4,24 +4,6 @@ package com.yourcompany.flavors; -import android.os.Bundle; - -import androidx.annotation.NonNull; import io.flutter.embedding.android.FlutterActivity; -import io.flutter.embedding.engine.FlutterEngine; -import io.flutter.plugin.common.MethodCall; -import io.flutter.plugin.common.MethodChannel; -import io.flutter.plugins.GeneratedPluginRegistrant; -public class MainActivity extends FlutterActivity { - @Override - public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { - GeneratedPluginRegistrant.registerWith(flutterEngine); - new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), "flavor") - .setMethodCallHandler( - (call, result) -> { - result.success(BuildConfig.FLAVOR); - } - ); - } -} +public class MainActivity extends FlutterActivity {} diff --git a/dev/integration_tests/flavors/lib/main.dart b/dev/integration_tests/flavors/lib/main.dart index a3a4e0b2dd..6441bee1a4 100644 --- a/dev/integration_tests/flavors/lib/main.dart +++ b/dev/integration_tests/flavors/lib/main.dart @@ -23,26 +23,11 @@ class Flavor extends StatefulWidget { } class _FlavorState extends State { - String? _flavor; - - @override - void initState() { - super.initState(); - const MethodChannel('flavor').invokeMethod('getFlavor').then((String? flavor) { - setState(() { - _flavor = flavor; - }); - }); - } - @override Widget build(BuildContext context) { - return Directionality( + return const Directionality( textDirection: TextDirection.ltr, - child: - _flavor == null - ? const Text('Awaiting flavor...') - : Text(_flavor!, key: const ValueKey('flavor')), + child: Text(appFlavor ?? 'null', key: ValueKey('flavor')), ); } }