Unverified Commit c40dd276 authored by Bartek Pacia's avatar Bartek Pacia Committed by GitHub

Fix Gradle warning in a freshly `flutter create`ed Android project (#122290)

Fix Gradle warning in a freshly `flutter create`ed Android project
parent 47dd9922
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -45,6 +45,6 @@ subprojects { ...@@ -45,6 +45,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -25,6 +25,6 @@ allprojects { ...@@ -25,6 +25,6 @@ allprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -19,6 +19,6 @@ allprojects { ...@@ -19,6 +19,6 @@ allprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -19,6 +19,6 @@ allprojects { ...@@ -19,6 +19,6 @@ allprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -30,6 +30,6 @@ subprojects { ...@@ -30,6 +30,6 @@ subprojects {
project.evaluationDependsOn(':app') project.evaluationDependsOn(':app')
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -45,6 +45,6 @@ subprojects { ...@@ -45,6 +45,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -19,6 +19,6 @@ allprojects { ...@@ -19,6 +19,6 @@ allprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -30,6 +30,6 @@ subprojects { ...@@ -30,6 +30,6 @@ subprojects {
project.evaluationDependsOn(':app') project.evaluationDependsOn(':app')
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -181,7 +181,7 @@ subprojects { ...@@ -181,7 +181,7 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
'''; ''';
......
...@@ -30,6 +30,6 @@ subprojects { ...@@ -30,6 +30,6 @@ subprojects {
project.evaluationDependsOn(':app') project.evaluationDependsOn(':app')
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -28,6 +28,6 @@ subprojects { ...@@ -28,6 +28,6 @@ subprojects {
project.evaluationDependsOn(':app') project.evaluationDependsOn(':app')
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -19,6 +19,7 @@ import '../base/logger.dart'; ...@@ -19,6 +19,7 @@ import '../base/logger.dart';
import '../base/net.dart'; import '../base/net.dart';
import '../base/platform.dart'; import '../base/platform.dart';
import '../base/process.dart'; import '../base/process.dart';
import '../base/project_migrator.dart';
import '../base/terminal.dart'; import '../base/terminal.dart';
import '../base/utils.dart'; import '../base/utils.dart';
import '../build_info.dart'; import '../build_info.dart';
...@@ -31,6 +32,7 @@ import 'android_builder.dart'; ...@@ -31,6 +32,7 @@ import 'android_builder.dart';
import 'android_studio.dart'; import 'android_studio.dart';
import 'gradle_errors.dart'; import 'gradle_errors.dart';
import 'gradle_utils.dart'; import 'gradle_utils.dart';
import 'migrations/top_level_gradle_build_file_migration.dart';
import 'multidex.dart'; import 'multidex.dart';
/// The directory where the APK artifact is generated. /// The directory where the APK artifact is generated.
...@@ -235,11 +237,17 @@ class AndroidGradleBuilder implements AndroidBuilder { ...@@ -235,11 +237,17 @@ class AndroidGradleBuilder implements AndroidBuilder {
int retry = 0, int retry = 0,
@visibleForTesting int? maxRetries, @visibleForTesting int? maxRetries,
}) async { }) async {
if (!project.android.isSupportedVersion) { if (!project.android.isSupportedVersion) {
_exitWithUnsupportedProjectMessage(_usage, _logger.terminal); _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); final bool usesAndroidX = isAppUsingAndroidX(project.android.hostAppGradleRoot);
if (usesAndroidX) { if (usesAndroidX) {
BuildEvent('app-using-android-x', type: 'gradle', flutterUsage: _usage).send(); BuildEvent('app-using-android-x', type: 'gradle', flutterUsage: _usage).send();
......
// 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;
}
}
...@@ -26,6 +26,6 @@ subprojects { ...@@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app') project.evaluationDependsOn(':app')
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
...@@ -26,6 +26,6 @@ subprojects { ...@@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app') project.evaluationDependsOn(':app')
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
// 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:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/android/migrations/top_level_gradle_build_file_migration.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/project.dart';
import 'package:test/fake.dart';
import '../../src/common.dart';
import '../../src/context.dart';
void main() {
group('Android migration', () {
group('migrate the Gradle "clean" task to lazy declaration', () {
late MemoryFileSystem memoryFileSystem;
late BufferLogger bufferLogger;
late FakeAndroidProject project;
late File topLevelGradleBuildFile;
setUp(() {
memoryFileSystem = MemoryFileSystem.test();
bufferLogger = BufferLogger.test();
project = FakeAndroidProject(
root: memoryFileSystem.currentDirectory.childDirectory('android')..createSync(),
);
topLevelGradleBuildFile = project.hostAppGradleRoot.childFile('build.gradle');
});
testUsingContext('skipped if files are missing', () {
final TopLevelGradleBuildFileMigration androidProjectMigration = TopLevelGradleBuildFileMigration(
project,
bufferLogger,
);
androidProjectMigration.migrate();
expect(topLevelGradleBuildFile.existsSync(), isFalse);
expect(bufferLogger.traceText, contains('Top-level Gradle build file not found, skipping migration of task "clean".'));
});
testUsingContext('skipped if nothing to upgrade', () {
topLevelGradleBuildFile.writeAsStringSync('''
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
''');
final TopLevelGradleBuildFileMigration androidProjectMigration = TopLevelGradleBuildFileMigration(
project,
bufferLogger,
);
final DateTime previousLastModified = topLevelGradleBuildFile.lastModifiedSync();
androidProjectMigration.migrate();
expect(topLevelGradleBuildFile.lastModifiedSync(), previousLastModified);
});
testUsingContext('top-level build.gradle is migrated', () {
topLevelGradleBuildFile.writeAsStringSync('''
task clean(type: Delete) {
delete rootProject.buildDir
}
''');
final TopLevelGradleBuildFileMigration androidProjectMigration = TopLevelGradleBuildFileMigration(
project,
bufferLogger,
);
androidProjectMigration.migrate();
expect(bufferLogger.traceText, contains('Migrating "clean" Gradle task to lazy declaration style.'));
expect(topLevelGradleBuildFile.readAsStringSync(), equals('''
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
'''));
});
});
});
}
class FakeAndroidProject extends Fake implements AndroidProject {
FakeAndroidProject({required Directory root}) : hostAppGradleRoot = root;
@override
Directory hostAppGradleRoot;
}
...@@ -134,7 +134,7 @@ class BasicDeferredComponentsConfig extends DeferredComponentsConfig { ...@@ -134,7 +134,7 @@ class BasicDeferredComponentsConfig extends DeferredComponentsConfig {
project.evaluationDependsOn(':app') project.evaluationDependsOn(':app')
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
'''; ''';
......
...@@ -110,7 +110,7 @@ class MultidexProject extends Project { ...@@ -110,7 +110,7 @@ class MultidexProject extends Project {
project.evaluationDependsOn(':app') project.evaluationDependsOn(':app')
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
'''; ''';
......
...@@ -46,6 +46,6 @@ subprojects { ...@@ -46,6 +46,6 @@ subprojects {
} }
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment