Adds vmservices to retrieve android applink settings (#125998)

fixes https://github.com/flutter/flutter/issues/120408

Added two gradle tasks, one for grabing the application id, one for grabbing app link domains.

Added a new vmservices to call these two gradle tasks and return the result.

The expected work flow is that the devtool will first call a vmservices to grab all avaliable build variants. It will then choose one of the build variant and call this new services to get application id and app link domains.
This commit is contained in:
chunhtai
2023-06-07 15:43:11 -07:00
committed by GitHub
parent 2754514ce2
commit 5328bd9ae0
11 changed files with 672 additions and 24 deletions

View File

@@ -0,0 +1,22 @@
// 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:meta/meta.dart';
/// A data class for app links related project settings.
///
/// See https://developer.android.com/training/app-links.
@immutable
class AndroidAppLinkSettings {
const AndroidAppLinkSettings({
required this.applicationId,
required this.domains,
});
/// The application id of the android sub-project.
final String applicationId;
/// The associated web domains of the android sub-project.
final List<String> domains;
}

View File

@@ -42,4 +42,16 @@ abstract class AndroidBuilder {
/// Returns a list of available build variant from the Android project.
Future<List<String>> getBuildVariants({required FlutterProject project});
/// Returns the application id for the given build variant.
Future<String> getApplicationIdForVariant(
String buildVariant, {
required FlutterProject project,
});
/// Returns a list of app link domains for the given build variant.
Future<List<String>> getAppLinkDomainsForVariant(
String buildVariant, {
required FlutterProject project,
});
}

View File

@@ -38,9 +38,9 @@ import 'migrations/android_studio_java_gradle_conflict_migration.dart';
import 'migrations/top_level_gradle_build_file_migration.dart';
import 'multidex.dart';
/// The regex to grab variant names from printVariants gradle task
/// The regex to grab variant names from printBuildVariants gradle task
///
/// The task is defined in flutter/packages/flutter_tools/gradle/flutter.gradle.
/// The task is defined in flutter/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy
///
/// The expected output from the task should be similar to:
///
@@ -49,6 +49,34 @@ import 'multidex.dart';
/// BuildVariant: profile
final RegExp _kBuildVariantRegex = RegExp('^BuildVariant: (?<$_kBuildVariantRegexGroupName>.*)\$');
const String _kBuildVariantRegexGroupName = 'variant';
const String _kBuildVariantTaskName = 'printBuildVariants';
/// The regex to grab variant names from print${BuildVariant}ApplicationId gradle task
///
/// The task is defined in flutter/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy
///
/// The expected output from the task should be similar to:
///
/// ApplicationId: com.example.my_id
final RegExp _kApplicationIdRegex = RegExp('^ApplicationId: (?<$_kApplicationIdRegexGroupName>.*)\$');
const String _kApplicationIdRegexGroupName = 'applicationId';
String _getPrintApplicationIdTaskFor(String buildVariant) {
return _taskForBuildVariant('print', buildVariant, 'ApplicationId');
}
/// The regex to grab app link domains from print${BuildVariant}AppLinkDomains gradle task
///
/// The task is defined in flutter/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy
///
/// The expected output from the task should be similar to:
///
/// Domain: domain.com
/// Domain: another-domain.dev
final RegExp _kAppLinkDomainsRegex = RegExp('^Domain: (?<$_kAppLinkDomainsGroupName>.*)\$');
const String _kAppLinkDomainsGroupName = 'domain';
String _getPrintAppLinkDomainsTaskFor(String buildVariant) {
return _taskForBuildVariant('print', buildVariant, 'AppLinkDomains');
}
/// The directory where the APK artifact is generated.
Directory getApkDirectory(FlutterProject project) {
@@ -89,9 +117,14 @@ Directory getRepoDirectory(Directory buildDirectory) {
String _taskFor(String prefix, BuildInfo buildInfo) {
final String buildType = camelCase(buildInfo.modeName);
final String productFlavor = buildInfo.flavor ?? '';
return '$prefix${sentenceCase(productFlavor)}${sentenceCase(buildType)}';
return _taskForBuildVariant(prefix, '$productFlavor${sentenceCase(buildType)}');
}
String _taskForBuildVariant(String prefix, String buildVariant, [String suffix = '']) {
return '$prefix${sentenceCase(buildVariant)}$suffix';
}
/// Returns the task to build an APK.
@visibleForTesting
String getAssembleTaskFor(BuildInfo buildInfo) {
@@ -240,6 +273,34 @@ class AndroidGradleBuilder implements AndroidBuilder {
);
}
Future<RunResult> _runGradleTask(
String taskName, {
List<String> options = const <String>[],
required FlutterProject project
}) async {
final Status status = _logger.startProgress(
"Running Gradle task '$taskName'...",
);
final List<String> command = <String>[
_gradleUtils.getExecutable(project),
...options, // suppresses gradle output.
taskName,
];
RunResult result;
try {
result = await _processUtils.run(
command,
workingDirectory: project.android.hostAppGradleRoot.path,
allowReentrantFlutter: true,
environment: _java?.environment,
);
} finally {
status.stop();
}
return result;
}
/// Builds an app.
///
/// * [project] is typically [FlutterProject.current()].
@@ -727,28 +788,14 @@ class AndroidGradleBuilder implements AndroidBuilder {
@override
Future<List<String>> getBuildVariants({required FlutterProject project}) async {
final Status status = _logger.startProgress(
"Running Gradle task 'printBuildVariants'...",
);
final List<String> command = <String>[
_gradleUtils.getExecutable(project),
'-q', // suppresses gradle output.
'printBuildVariants',
];
final Stopwatch sw = Stopwatch()
..start();
RunResult result;
try {
result = await _processUtils.run(
command,
workingDirectory: project.android.hostAppGradleRoot.path,
allowReentrantFlutter: true,
environment: _java?.environment,
);
} finally {
status.stop();
}
final RunResult result = await _runGradleTask(
_kBuildVariantTaskName,
options: const <String>['-q'],
project: project,
);
_usage.sendTiming('print', 'android build variants', sw.elapsed);
if (result.exitCode != 0) {
@@ -765,6 +812,65 @@ class AndroidGradleBuilder implements AndroidBuilder {
}
return options;
}
@override
Future<String> getApplicationIdForVariant(
String buildVariant, {
required FlutterProject project,
}) async {
final String taskName = _getPrintApplicationIdTaskFor(buildVariant);
final Stopwatch sw = Stopwatch()
..start();
final RunResult result = await _runGradleTask(
taskName,
options: const <String>['-q'],
project: project,
);
_usage.sendTiming('print', 'application id', sw.elapsed);
if (result.exitCode != 0) {
_logger.printStatus(result.stdout, wrap: false);
_logger.printError(result.stderr, wrap: false);
return '';
}
for (final String line in LineSplitter.split(result.stdout)) {
final RegExpMatch? match = _kApplicationIdRegex.firstMatch(line);
if (match != null) {
return match.namedGroup(_kApplicationIdRegexGroupName)!;
}
}
return '';
}
@override
Future<List<String>> getAppLinkDomainsForVariant(
String buildVariant, {
required FlutterProject project,
}) async {
final String taskName = _getPrintAppLinkDomainsTaskFor(buildVariant);
final Stopwatch sw = Stopwatch()
..start();
final RunResult result = await _runGradleTask(
taskName,
options: const <String>['-q'],
project: project,
);
_usage.sendTiming('print', 'application id', sw.elapsed);
if (result.exitCode != 0) {
_logger.printStatus(result.stdout, wrap: false);
_logger.printError(result.stderr, wrap: false);
return const <String>[];
}
final List<String> domains = <String>[];
for (final String line in LineSplitter.split(result.stdout)) {
final RegExpMatch? match = _kAppLinkDomainsRegex.firstMatch(line);
if (match != null) {
domains.add(match.namedGroup(_kAppLinkDomainsGroupName)!);
}
}
return domains;
}
}
/// Prints how to consume the AAR from a host app.