Unified analytics events migration for BuildEvent (#137562)

Related to tracker issue:
- https://github.com/flutter/flutter/issues/128251
This commit is contained in:
Elias Yishak
2023-11-01 15:56:50 -04:00
committed by GitHub
parent 01b8854011
commit 70456f5b2d
18 changed files with 313 additions and 33 deletions

View File

@@ -7,6 +7,7 @@ import 'dart:math';
import 'package:crypto/crypto.dart';
import 'package:meta/meta.dart';
import 'package:process/process.dart';
import 'package:unified_analytics/unified_analytics.dart';
import 'package:xml/xml.dart';
import '../artifacts.dart';
@@ -149,6 +150,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
required FileSystem fileSystem,
required Artifacts artifacts,
required Usage usage,
required Analytics analytics,
required GradleUtils gradleUtils,
required Platform platform,
required AndroidStudio? androidStudio,
@@ -157,6 +159,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
_fileSystem = fileSystem,
_artifacts = artifacts,
_usage = usage,
_analytics = analytics,
_gradleUtils = gradleUtils,
_androidStudio = androidStudio,
_fileSystemUtils = FileSystemUtils(fileSystem: fileSystem, platform: platform),
@@ -168,6 +171,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
final FileSystem _fileSystem;
final Artifacts _artifacts;
final Usage _usage;
final Analytics _analytics;
final GradleUtils _gradleUtils;
final FileSystemUtils _fileSystemUtils;
final AndroidStudio? _androidStudio;
@@ -300,7 +304,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
@visibleForTesting int? maxRetries,
}) async {
if (!project.android.isSupportedVersion) {
_exitWithUnsupportedProjectMessage(_usage, _logger.terminal);
_exitWithUnsupportedProjectMessage(_usage, _logger.terminal, analytics: _analytics);
}
final List<ProjectMigrator> migrators = <ProjectMigrator>[
@@ -318,8 +322,11 @@ class AndroidGradleBuilder implements AndroidBuilder {
final bool usesAndroidX = isAppUsingAndroidX(project.android.hostAppGradleRoot);
if (usesAndroidX) {
BuildEvent('app-using-android-x', type: 'gradle', flutterUsage: _usage).send();
_analytics.send(Event.flutterBuildInfo(label: 'app-using-android-x', buildType: 'gradle'));
} else if (!usesAndroidX) {
BuildEvent('app-not-using-android-x', type: 'gradle', flutterUsage: _usage).send();
_analytics.send(Event.flutterBuildInfo(label: 'app-not-using-android-x', buildType: 'gradle'));
_logger.printStatus("${_logger.terminal.warningMark} Your app isn't using AndroidX.", emphasis: true);
_logger.printStatus(
'To avoid potential build failures, you can quickly migrate your app '
@@ -488,6 +495,8 @@ class AndroidGradleBuilder implements AndroidBuilder {
if (exitCode != 0) {
if (detectedGradleError == null) {
BuildEvent('gradle-unknown-failure', type: 'gradle', flutterUsage: _usage).send();
_analytics.send(Event.flutterBuildInfo(label: 'gradle-unknown-failure', buildType: 'gradle'));
throwToolExit(
'Gradle task $assembleTask failed with exit code $exitCode',
exitCode: exitCode,
@@ -520,13 +529,19 @@ class AndroidGradleBuilder implements AndroidBuilder {
configOnly: configOnly,
);
final String successEventLabel = 'gradle-${detectedGradleError!.eventLabel}-success';
BuildEvent(successEventLabel, type: 'gradle', flutterUsage: _usage).send();
_analytics.send(Event.flutterBuildInfo(label: successEventLabel, buildType: 'gradle'));
return;
case GradleBuildStatus.exit:
// Continue and throw tool exit.
}
}
BuildEvent('gradle-${detectedGradleError?.eventLabel}-failure', type: 'gradle', flutterUsage: _usage).send();
final String usageLabel = 'gradle-${detectedGradleError?.eventLabel}-failure';
BuildEvent(usageLabel, type: 'gradle', flutterUsage: _usage).send();
_analytics.send(Event.flutterBuildInfo(label: usageLabel, buildType: 'gradle'));
throwToolExit(
'Gradle task $assembleTask failed with exit code $exitCode',
exitCode: exitCode,
@@ -534,7 +549,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
}
if (isBuildingBundle) {
final File bundleFile = findBundleFile(project, buildInfo, _logger, _usage);
final File bundleFile = findBundleFile(project, buildInfo, _logger, _usage, _analytics);
final String appSize = (buildInfo.mode == BuildMode.debug)
? '' // Don't display the size when building a debug variant.
: ' (${getSizeAsMB(bundleFile.lengthSync())})';
@@ -551,7 +566,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
}
// Gradle produced APKs.
final Iterable<String> apkFilesPaths = project.isModule
? findApkFilesModule(project, androidBuildInfo, _logger, _usage)
? findApkFilesModule(project, androidBuildInfo, _logger, _usage, _analytics)
: listApkPaths(androidBuildInfo);
final Directory apkDirectory = getApkDirectory(project);
@@ -563,6 +578,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
fileExtension: '.apk',
logger: _logger,
usage: _usage,
analytics: _analytics,
);
}
@@ -886,8 +902,14 @@ String _calculateSha(File file) {
return _hex(sha1.convert(bytes).bytes);
}
void _exitWithUnsupportedProjectMessage(Usage usage, Terminal terminal) {
void _exitWithUnsupportedProjectMessage(Usage usage, Terminal terminal, {required Analytics analytics}) {
BuildEvent('unsupported-project', type: 'gradle', eventError: 'gradle-plugin', flutterUsage: usage).send();
analytics.send(Event.flutterBuildInfo(
label: 'unsupported-project',
buildType: 'gradle',
error: 'gradle-plugin',
));
throwToolExit(
'${terminal.warningMark} Your app is using an unsupported Gradle project. '
'To fix this problem, create a new project by running `flutter create -t app <app-directory>` '
@@ -913,6 +935,7 @@ Iterable<String> findApkFilesModule(
AndroidBuildInfo androidBuildInfo,
Logger logger,
Usage usage,
Analytics analytics,
) {
final Iterable<String> apkFileNames = _apkFilesFor(androidBuildInfo);
final Directory apkDirectory = getApkDirectory(project);
@@ -948,6 +971,7 @@ Iterable<String> findApkFilesModule(
fileExtension: '.apk',
logger: logger,
usage: usage,
analytics: analytics,
);
}
return apks.map((File file) => file.path);
@@ -986,7 +1010,7 @@ Iterable<String> listApkPaths(
}
@visibleForTesting
File findBundleFile(FlutterProject project, BuildInfo buildInfo, Logger logger, Usage usage) {
File findBundleFile(FlutterProject project, BuildInfo buildInfo, Logger logger, Usage usage, Analytics analytics) {
final List<File> fileCandidates = <File>[
getBundleDirectory(project)
.childDirectory(camelCase(buildInfo.modeName))
@@ -1043,6 +1067,7 @@ File findBundleFile(FlutterProject project, BuildInfo buildInfo, Logger logger,
fileExtension: '.aab',
logger: logger,
usage: usage,
analytics: analytics,
);
}
@@ -1052,17 +1077,25 @@ Never _exitWithExpectedFileNotFound({
required String fileExtension,
required Logger logger,
required Usage usage,
required Analytics analytics,
}) {
final String androidGradlePluginVersion =
getGradleVersionForAndroidPlugin(project.android.hostAppGradleRoot, logger);
final String gradleBuildSettings = 'androidGradlePluginVersion: $androidGradlePluginVersion, '
'fileExtension: $fileExtension';
BuildEvent('gradle-expected-file-not-found',
type: 'gradle',
settings:
'androidGradlePluginVersion: $androidGradlePluginVersion, '
'fileExtension: $fileExtension',
settings: gradleBuildSettings,
flutterUsage: usage,
).send();
analytics.send(Event.flutterBuildInfo(
label: 'gradle-expected-file-not-found',
buildType: 'gradle',
settings: gradleBuildSettings,
));
throwToolExit(
'Gradle build failed to produce an $fileExtension file. '
"It's likely that this file was generated under ${project.android.buildDirectory.path}, "

View File

@@ -4,6 +4,7 @@
import 'package:meta/meta.dart';
import 'package:process/process.dart';
import 'package:unified_analytics/unified_analytics.dart';
import '../base/common.dart';
import '../base/file_system.dart';
@@ -741,6 +742,11 @@ void exitWithNoSdkMessage() {
eventError: 'android-sdk-not-found',
flutterUsage: globals.flutterUsage)
.send();
globals.analytics.send(Event.flutterBuildInfo(
label: 'unsupported-project',
buildType: 'gradle',
error: 'android-sdk-not-found',
));
throwToolExit('${globals.logger.terminal.warningMark} No Android SDK found. '
'Try setting the ANDROID_HOME environment variable.');
}