Fix Gradle warning in a freshly flutter createed Android project (#122290)
Fix Gradle warning in a freshly `flutter create`ed Android project
This commit is contained in:
@@ -19,6 +19,7 @@ import '../base/logger.dart';
|
||||
import '../base/net.dart';
|
||||
import '../base/platform.dart';
|
||||
import '../base/process.dart';
|
||||
import '../base/project_migrator.dart';
|
||||
import '../base/terminal.dart';
|
||||
import '../base/utils.dart';
|
||||
import '../build_info.dart';
|
||||
@@ -31,6 +32,7 @@ import 'android_builder.dart';
|
||||
import 'android_studio.dart';
|
||||
import 'gradle_errors.dart';
|
||||
import 'gradle_utils.dart';
|
||||
import 'migrations/top_level_gradle_build_file_migration.dart';
|
||||
import 'multidex.dart';
|
||||
|
||||
/// The directory where the APK artifact is generated.
|
||||
@@ -235,11 +237,17 @@ class AndroidGradleBuilder implements AndroidBuilder {
|
||||
int retry = 0,
|
||||
@visibleForTesting int? maxRetries,
|
||||
}) async {
|
||||
|
||||
if (!project.android.isSupportedVersion) {
|
||||
_exitWithUnsupportedProjectMessage(_usage, _logger.terminal);
|
||||
}
|
||||
|
||||
final List<ProjectMigrator> migrators = <ProjectMigrator>[
|
||||
TopLevelGradleBuildFileMigration(project.android, _logger),
|
||||
];
|
||||
|
||||
final ProjectMigration migration = ProjectMigration(migrators);
|
||||
migration.run();
|
||||
|
||||
final bool usesAndroidX = isAppUsingAndroidX(project.android.hostAppGradleRoot);
|
||||
if (usesAndroidX) {
|
||||
BuildEvent('app-using-android-x', type: 'gradle', flutterUsage: _usage).send();
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
// 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 '../../base/file_system.dart';
|
||||
import '../../base/project_migrator.dart';
|
||||
import '../../project.dart';
|
||||
|
||||
const String _eagerCleanTaskDeclaration = '''
|
||||
task clean(type: Delete) {
|
||||
delete rootProject.buildDir
|
||||
}
|
||||
''';
|
||||
|
||||
const String _lazyCleanTaskDeclaration = '''
|
||||
tasks.register("clean", Delete) {
|
||||
delete rootProject.buildDir
|
||||
}
|
||||
''';
|
||||
|
||||
/// Migrate the Gradle "clean" task to use modern, lazy declaration style.
|
||||
class TopLevelGradleBuildFileMigration extends ProjectMigrator {
|
||||
TopLevelGradleBuildFileMigration(
|
||||
AndroidProject project,
|
||||
super.logger,
|
||||
) : _topLevelGradleBuildFile = project.hostAppGradleRoot.childFile('build.gradle');
|
||||
|
||||
final File _topLevelGradleBuildFile;
|
||||
|
||||
@override
|
||||
void migrate() {
|
||||
if (!_topLevelGradleBuildFile.existsSync()) {
|
||||
logger.printTrace('Top-level Gradle build file not found, skipping migration of task "clean".');
|
||||
return;
|
||||
}
|
||||
|
||||
processFileLines(_topLevelGradleBuildFile);
|
||||
}
|
||||
|
||||
@override
|
||||
String migrateFileContents(String fileContents) {
|
||||
final String newContents = fileContents.replaceAll(
|
||||
_eagerCleanTaskDeclaration,
|
||||
_lazyCleanTaskDeclaration,
|
||||
);
|
||||
|
||||
if (newContents != fileContents) {
|
||||
logger.printTrace('Migrating "clean" Gradle task to lazy declaration style.');
|
||||
}
|
||||
|
||||
return newContents;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user