Skip to content

[tool] Add initial gradle validation command #3715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ task:
# run with --require-excerpts and no exclusions.
- ./script/tool_runner.sh readme-check --require-excerpts --exclude=script/configs/temp_exclude_excerpt.yaml
dependabot_script: $PLUGIN_TOOL_COMMAND dependabot-check
gradle_script: $PLUGIN_TOOL_COMMAND gradle-check
version_script:
# For pre-submit, pass the PR labels to the script to allow for
# check overrides.
Expand Down
4 changes: 4 additions & 0 deletions packages/espresso/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.0+1

* Sets an explicit Java compatibility version.

## 0.3.0

* **BREAKING CHANGE**: Migrates uses of the deprecated `@Beta` annotation to the new `@ExperimentalApi` annotation.
Expand Down
5 changes: 5 additions & 0 deletions packages/espresso/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ android {
minSdkVersion 16
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
}

lintOptions {
checkAllWarnings true
warningsAsErrors true
Expand Down
2 changes: 1 addition & 1 deletion packages/espresso/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Java classes for testing Flutter apps using Espresso.
Allows driving Flutter widgets from a native Espresso test.
repository: https://github.com/flutter/packages/tree/main/packages/espresso
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+espresso%22
version: 0.3.0
version: 0.3.0+1

environment:
sdk: ">=2.17.0 <4.0.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/flutter_plugin_android_lifecycle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 2.0.10

* Sets an explicit Java compatibility version.
* Aligns Dart and Flutter SDK constraints.

## 2.0.9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'proguard.txt'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
}

lintOptions {
checkAllWarnings true
warningsAsErrors true
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_plugin_android_lifecycle/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: flutter_plugin_android_lifecycle
description: Flutter plugin for accessing an Android Lifecycle within other plugins.
repository: https://github.com/flutter/packages/tree/main/packages/flutter_plugin_android_lifecycle
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_plugin_android_lifecycle%22
version: 2.0.9
version: 2.0.10

environment:
sdk: ">=2.17.0 <4.0.0"
Expand Down
4 changes: 4 additions & 0 deletions packages/google_sign_in/google_sign_in_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 6.1.10

* Sets an explicit Java compatibility version.

## 6.1.9

* Updates play-services-auth version to 20.5.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ android {
minSdkVersion 16
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
}

lintOptions {
checkAllWarnings true
warningsAsErrors true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: google_sign_in_android
description: Android implementation of the google_sign_in plugin.
repository: https://github.com/flutter/packages/tree/main/packages/google_sign_in/google_sign_in_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22
version: 6.1.9
version: 6.1.10

environment:
sdk: ">=2.17.0 <4.0.0"
Expand Down
4 changes: 4 additions & 0 deletions packages/local_auth/local_auth_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.22

* Sets an explicit Java compatibility version.

## 1.0.21

* Clarifies explanation of endorsement in README.
Expand Down
5 changes: 5 additions & 0 deletions packages/local_auth/local_auth_android/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ android {
minSdkVersion 16
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
}

lintOptions {
checkAllWarnings true
warningsAsErrors true
Expand Down
2 changes: 1 addition & 1 deletion packages/local_auth/local_auth_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: local_auth_android
description: Android implementation of the local_auth plugin.
repository: https://github.com/flutter/packages/tree/main/packages/local_auth/local_auth_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+local_auth%22
version: 1.0.21
version: 1.0.22

environment:
sdk: ">=2.17.0 <4.0.0"
Expand Down
4 changes: 4 additions & 0 deletions packages/url_launcher/url_launcher_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 6.0.28

* Sets an explicit Java compatibility version.

## 6.0.27

* Fixes Java warnings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ android {
minSdkVersion 16
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
}

lintOptions {
checkAllWarnings true
warningsAsErrors true
Expand Down
2 changes: 1 addition & 1 deletion packages/url_launcher/url_launcher_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: url_launcher_android
description: Android implementation of the url_launcher plugin.
repository: https://github.com/flutter/packages/tree/main/packages/url_launcher/url_launcher_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+url_launcher%22
version: 6.0.27
version: 6.0.28

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
92 changes: 92 additions & 0 deletions script/tool/lib/src/gradle_check_command.dart
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know changing file names can be a pain but can we name this something more specific. I think we will have more gradle checks moving forward. Maybe gradle_conventions_check_command

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was assuming that future gradle checks would be added here; is there a reason we'd want new commands for those instead of having them all here? If you look at things like pubspec-check, we do a bunch of different checks related to the file in the one command.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I had assumed that this command was only for the gradle files at the top of packages since that was what was parsed. Basically the implementation was the behavior you desired but the documentation was incorrect. Based on this update if all checks will live in this file then then the name makes sense.

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2013 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 'common/core.dart';
import 'common/package_looping_command.dart';
import 'common/repository_package.dart';

/// A command to enforce gradle file conventions and best practices.
Copy link
Contributor

@reidbaker reidbaker Apr 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not true based on the implementation. I think this will only impact topline gradle files and does not check gradle files in app.

Also from a structural perspective I think you might want to logically separate example gradle files from gradle files of plugins that are published.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will only impact topline gradle files and does not check gradle files in app.

This is currently true, because the check I've added didn't seem important for apps. If that's not the case we can definitely change that.

Also from a structural perspective I think you might want to logically separate example gradle files from gradle files of plugins that are published.

Yes, that's why I have it only iterating over top-level packages, rather than setting the command to iterate over everything. My thought was that we could add example checks later, but I can definitely implement the structure for that now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It now iterates over everything, and just skips the current check for examples, so it will be easy to add things for all gradle files later.

(Let me know if you want this specific check to run for examples; currently it's not even in the flutter template for apps.)

class GradleCheckCommand extends PackageLoopingCommand {
/// Creates an instance of the gradle check command.
GradleCheckCommand(Directory packagesDir) : super(packagesDir);

@override
final String name = 'gradle-check';

@override
final String description =
'Checks that gradle files follow repository conventions.';

@override
bool get hasLongOutput => false;

@override
PackageLoopingType get packageLoopingType =>
PackageLoopingType.includeAllSubpackages;

@override
Future<PackageResult> runForPackage(RepositoryPackage package) async {
if (!package.platformDirectory(FlutterPlatform.android).existsSync()) {
return PackageResult.skip('No android/ directory.');
}

const String exampleDirName = 'example';
final bool isExample = package.directory.basename == exampleDirName ||
package.directory.parent.basename == exampleDirName;
if (!_validateBuildGradle(package, isExample: isExample)) {
return PackageResult.fail();
}
return PackageResult.success();
}

bool _validateBuildGradle(RepositoryPackage package,
{required bool isExample}) {
// Currently the only check is not relevant to examples; checks that apply
// to both plugins and examples should go above here.
if (!isExample) {
print('${indentation}Validating android/build.gradle.');
final String contents = package
.platformDirectory(FlutterPlatform.android)
.childFile('build.gradle')
.readAsStringSync();
final List<String> lines = contents.split('\n');

if (!lines.any((String line) =>
line.contains('languageVersion') &&
!line.trim().startsWith('//')) &&
!lines.any((String line) =>
line.contains('sourceCompatibility') &&
!line.trim().startsWith('//'))) {
const String errorMessage = '''
build.gradle must set an explicit Java compatibility version.

This can be done either via "sourceCompatibility":
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
}
}

or "toolchain":
java {
toolchain {
languageVersion = JavaLanguageVersion.of(8)
}
}

See:
https://docs.gradle.org/current/userguide/java_plugin.html#toolchain_and_compatibility
for more details.''';

printError(
'$indentation${errorMessage.split('\n').join('\n$indentation')}');
return false;
}
}

return true;
}
}
2 changes: 2 additions & 0 deletions script/tool/lib/src/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'federation_safety_check_command.dart';
import 'firebase_test_lab_command.dart';
import 'fix_command.dart';
import 'format_command.dart';
import 'gradle_check_command.dart';
import 'license_check_command.dart';
import 'lint_android_command.dart';
import 'list_command.dart';
Expand Down Expand Up @@ -66,6 +67,7 @@ void main(List<String> args) {
..addCommand(FirebaseTestLabCommand(packagesDir))
..addCommand(FixCommand(packagesDir))
..addCommand(FormatCommand(packagesDir))
..addCommand(GradleCheckCommand(packagesDir))
..addCommand(LicenseCheckCommand(packagesDir))
..addCommand(LintAndroidCommand(packagesDir))
..addCommand(PodspecCheckCommand(packagesDir))
Expand Down
Loading