Skip to content

Commit 4feddff

Browse files
jesswrdreidbaker
andauthored
Applied Gradle Plugins Declaratively for path_provider (#7822)
Updated applying gradle plugins from usage of imperative apply to usage of declarative blocks {} apply. Intending on updating all android example apps under packages. Did one more as a proof of concept before doing more. More information on Flutter Gradle Plugin Apply [here](https://docs.flutter.dev/release/breaking-changes/flutter-gradle-plugin-apply) Partially addresses [#152656](flutter/flutter#152656) ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] page, which explains my responsibilities. - [x] I read and followed the [relevant style guides] and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use `dart format`.) - [x] I signed the [CLA]. - [x] The title of the PR starts with the name of the package surrounded by square brackets, e.g. `[shared_preferences]` - [x] I [linked to at least one issue that this PR fixes] in the description above. - [x] I updated `pubspec.yaml` with an appropriate new version according to the [pub versioning philosophy], or this PR is [exempt from version changes]. - [x] I updated `CHANGELOG.md` to add a description of the change, [following repository CHANGELOG style], or this PR is [exempt from CHANGELOG changes]. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md [relevant style guides]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style [CLA]: https://cla.developers.google.com/ [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md [linked to at least one issue that this PR fixes]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview [pub versioning philosophy]: https://dart.dev/tools/pub/versioning [exempt from version changes]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#version [following repository CHANGELOG style]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog-style [exempt from CHANGELOG changes]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog [test-exempt]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests --------- Co-authored-by: Reid Baker <reidbaker@google.com>
1 parent bce9735 commit 4feddff

File tree

7 files changed

+226
-62
lines changed

7 files changed

+226
-62
lines changed

packages/path_provider/path_provider/example/android/app/build.gradle

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
plugins {
2+
id "com.android.application"
3+
id "org.jetbrains.kotlin.android"
4+
id "dev.flutter.flutter-gradle-plugin"
5+
}
6+
17
def localProperties = new Properties()
28
def localPropertiesFile = rootProject.file('local.properties')
39
if (localPropertiesFile.exists()) {
@@ -6,11 +12,6 @@ if (localPropertiesFile.exists()) {
612
}
713
}
814

9-
def flutterRoot = localProperties.getProperty('flutter.sdk')
10-
if (flutterRoot == null) {
11-
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
12-
}
13-
1415
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
1516
if (flutterVersionCode == null) {
1617
flutterVersionCode = '1'
@@ -21,9 +22,6 @@ if (flutterVersionName == null) {
2122
flutterVersionName = '1.0'
2223
}
2324

24-
apply plugin: 'com.android.application'
25-
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
26-
2725
android {
2826
namespace 'io.flutter.plugins.pathproviderexample'
2927
compileSdk flutter.compileSdkVersion
@@ -32,7 +30,7 @@ android {
3230
defaultConfig {
3331
applicationId "io.flutter.plugins.pathproviderexample"
3432
minSdkVersion flutter.minSdkVersion
35-
targetSdkVersion 28
33+
targetSdkVersion 35
3634
versionCode flutterVersionCode.toInteger()
3735
versionName flutterVersionName
3836
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

packages/path_provider/path_provider/example/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<application android:label="path_provider_example" android:icon="@mipmap/ic_launcher">
77
<activity android:name="io.flutter.embedding.android.FlutterActivity"
88
android:launchMode="singleTop"
9+
android:exported="true"
910
android:theme="@android:style/Theme.Black.NoTitleBar"
1011
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
1112
android:hardwareAccelerated="true"

packages/path_provider/path_provider/example/android/build.gradle

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
buildscript {
2-
repositories {
3-
google()
4-
mavenCentral()
5-
}
6-
7-
dependencies {
8-
classpath 'com.android.tools.build:gradle:8.5.2'
9-
}
10-
}
11-
121
allprojects {
132
repositories {
143
// See https://github.com/flutter/flutter/blob/master/docs/ecosystem/Plugins-and-Packages-repository-structure.md#gradle-structure for more info.
Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
include ':app'
1+
pluginManagement {
2+
def flutterSdkPath = {
3+
def properties = new Properties()
4+
file("local.properties").withInputStream { properties.load(it) }
5+
def flutterSdkPath = properties.getProperty("flutter.sdk")
6+
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
7+
return flutterSdkPath
8+
}()
29

3-
def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()
10+
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")
411

5-
def plugins = new Properties()
6-
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
7-
if (pluginsFile.exists()) {
8-
pluginsFile.withInputStream { stream -> plugins.load(stream) }
9-
}
10-
11-
plugins.each { name, path ->
12-
def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
13-
include ":$name"
14-
project(":$name").projectDir = pluginDirectory
12+
repositories {
13+
google()
14+
mavenCentral()
15+
gradlePluginPortal()
16+
}
1517
}
1618

1719
// See https://github.com/flutter/flutter/blob/master/docs/ecosystem/Plugins-and-Packages-repository-structure.md#gradle-structure for more info.
18-
buildscript {
19-
repositories {
20-
maven {
21-
url "https://plugins.gradle.org/m2/"
22-
}
23-
}
24-
dependencies {
25-
classpath "gradle.plugin.com.google.cloud.artifactregistry:artifactregistry-gradle-plugin:2.2.1"
26-
}
20+
plugins {
21+
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
22+
id "com.android.application" version "8.6.0" apply false
23+
id "org.jetbrains.kotlin.android" version "1.7.10" apply false
24+
id "com.google.cloud.artifactregistry.gradle-plugin" version "2.2.1"
2725
}
28-
apply plugin: "com.google.cloud.artifactregistry.gradle-plugin"
26+
27+
include ":app"

script/tool/lib/src/gradle_check_command.dart

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ class GradleCheckCommand extends PackageLoopingCommand {
202202
/// configuration that enables artifact hub env variable.
203203
@visibleForTesting
204204
static String exampleRootSettingsArtifactHubString = '''
205-
// See $artifactHubDocumentationString for more info.
206205
buildscript {
207206
repositories {
208207
maven {
@@ -216,6 +215,18 @@ buildscript {
216215
apply plugin: "com.google.cloud.artifactregistry.gradle-plugin"
217216
''';
218217

218+
/// String printed as a valid example of settings.gradle repository
219+
/// configuration that enables artifact hub env variable.
220+
/// GP stands for the gradle plugin method of flutter tooling inclusion.
221+
@visibleForTesting
222+
static String exampleSettingsArtifactHubStringGP = '''
223+
plugins {
224+
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
225+
// ...other plugins
226+
id "com.google.cloud.artifactregistry.gradle-plugin" version "2.2.1"
227+
}
228+
''';
229+
219230
/// Validates that [gradleLines] reads and uses a artifiact hub repository
220231
/// when ARTIFACT_HUB_REPOSITORY is set.
221232
///
@@ -228,24 +239,47 @@ apply plugin: "com.google.cloud.artifactregistry.gradle-plugin"
228239
r'classpath.*gradle\.plugin\.com\.google\.cloud\.artifactregistry:artifactregistry-gradle-plugin');
229240
final RegExp artifactRegistryPluginApplyRegex = RegExp(
230241
r'apply.*plugin.*com\.google\.cloud\.artifactregistry\.gradle-plugin');
242+
final RegExp artifactRegistryPluginApplyRegexGP = RegExp(
243+
r'id.*com\.google\.cloud\.artifactregistry\.gradle-plugin.*version.*\b\d+\.\d+\.\d+\b');
244+
final RegExp artifactRegistryPluginApplyDeclarativeRegex =
245+
RegExp(r'\bpluginManagement\b');
231246

232247
final bool documentationPresent = gradleLines
233248
.any((String line) => documentationPresentRegex.hasMatch(line));
234249
final bool artifactRegistryDefined = gradleLines
235250
.any((String line) => artifactRegistryDefinitionRegex.hasMatch(line));
236251
final bool artifactRegistryPluginApplied = gradleLines
237252
.any((String line) => artifactRegistryPluginApplyRegex.hasMatch(line));
238-
239-
if (!(documentationPresent &&
240-
artifactRegistryDefined &&
241-
artifactRegistryPluginApplied)) {
242-
printError('Failed Artifact Hub validation. Include the following in '
243-
'example root settings.gradle:\n$exampleRootSettingsArtifactHubString');
253+
final bool declarativeArtifactRegistryApplied = gradleLines.any(
254+
(String line) => artifactRegistryPluginApplyRegexGP.hasMatch(line));
255+
final bool declarativePluginBlockApplied = gradleLines.any((String line) =>
256+
artifactRegistryPluginApplyDeclarativeRegex.hasMatch(line));
257+
258+
final bool imperativeArtifactRegistryApplied =
259+
artifactRegistryDefined && artifactRegistryPluginApplied;
260+
261+
final bool validArtifactConfiguration = documentationPresent &&
262+
(imperativeArtifactRegistryApplied ||
263+
declarativeArtifactRegistryApplied);
264+
265+
if (!validArtifactConfiguration) {
266+
printError('Failed Artifact Hub validation.');
267+
if (!documentationPresent) {
268+
printError(
269+
'The link to the Artifact Hub documentation is missing. Include the following in '
270+
'example root settings.gradle:\n// See $artifactHubDocumentationString for more info.');
271+
}
272+
if (artifactRegistryDefined ||
273+
artifactRegistryPluginApplied ||
274+
!declarativePluginBlockApplied) {
275+
printError('Include the following in '
276+
'example root settings.gradle:\n$exampleRootSettingsArtifactHubString');
277+
} else if (!declarativeArtifactRegistryApplied) {
278+
printError('Include the following in '
279+
'example root settings.gradle:\n$exampleSettingsArtifactHubStringGP');
280+
}
244281
}
245-
246-
return documentationPresent &&
247-
artifactRegistryDefined &&
248-
artifactRegistryPluginApplied;
282+
return validArtifactConfiguration;
249283
}
250284

251285
/// Validates the top-level build.gradle for an example app (e.g.,

0 commit comments

Comments
 (0)