Skip to content

Commit e2dea95

Browse files
gmackallGray Mackall
andauthored
Fix warnings in FGP (#166727)
Fixes/suppresses all warnings: 1. Fixes flutter/flutter#162695 2. Suppresses warnings about the various `*Variant*` imports that we use being deprecated, with comments linking to flutter/flutter#166550 to migrate to the variant api. 3. Fixes some unused elvis operators and unneeded types provided for declarations that AGP complains about, e.g. `val fooString: String = "a string"`, and use of deprecated string related methods. 4. Follows up on flutter/flutter#166277, as we were getting a warning mentioned in this comment flutter/flutter#166277 (comment). 5. Suppresses some warnings about unused code where the analysis couldn't properly detect it being used (i.e., it isn't unused) (4) is more opinionated, let me know if you think it should be done in a follow up. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [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] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [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/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Gray Mackall <mackall@google.com>
1 parent a2cb910 commit e2dea95

14 files changed

+143
-87
lines changed

dev/devicelab/bin/tasks/gradle_plugin_fat_apk_test.dart

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,7 @@ Future<void> main() async {
157157

158158
final String defaultPath = path.join(project.rootPath, 'android', 'app', '.cxx');
159159

160-
final String modifiedPath = path.join(
161-
project.rootPath,
162-
'build',
163-
'app',
164-
'intermediates',
165-
'flutter',
166-
'.cxx',
167-
);
160+
final String modifiedPath = path.join(project.rootPath, 'build', '.cxx');
168161
if (Directory(defaultPath).existsSync()) {
169162
throw TaskResult.failure('Producing unexpected build artifacts in $defaultPath');
170163
}

packages/flutter_tools/gradle/build.gradle.kts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ tasks.validatePlugins {
1919
enableStricterValidation.set(true)
2020
}
2121

22-
// We need to compile Kotlin first so we can call it from Groovy. See https://stackoverflow.com/q/36214437/7009800
23-
tasks.withType<GroovyCompile> {
24-
dependsOn(tasks.compileKotlin)
25-
classpath += files(tasks.compileKotlin.get().destinationDirectory)
26-
}
27-
28-
tasks.classes {
29-
dependsOn(tasks.compileGroovy)
30-
}
31-
3222
gradlePlugin {
3323
plugins {
3424
// The "flutterPlugin" name isn't used anywhere.

packages/flutter_tools/gradle/src/main/kotlin/Deeplink.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import kotlinx.serialization.json.put
1010

1111
// TODO(gmackall): Identify which of these can be val instead of var.
1212
class Deeplink(
13-
var scheme: String?,
14-
var host: String?,
13+
private var scheme: String?,
14+
private var host: String?,
1515
var path: String?,
16-
var intentFilterCheck: IntentFilterCheck
16+
private var intentFilterCheck: IntentFilterCheck
1717
) {
1818
// TODO(gmackall): This behavior was kept identical to the original Groovy behavior as part of
1919
// the Groovy->Kotlin conversion, but should be changed once the conversion is complete.

packages/flutter_tools/gradle/src/main/kotlin/FlutterAppPluginLoaderPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ private const val FLUTTER_SDK_PATH = "flutterSdkPath"
2222
* This plugin applies the native plugin loader plugin (../scripts/native_plugin_loader.gradle.kts)
2323
* and then configures the main project to `include` each of the loaded flutter plugins.
2424
*/
25+
@Suppress("unused") // This class is used by packages/flutter_tools/gradle/build.gradle.kts.
2526
class FlutterAppPluginLoaderPlugin : Plugin<Settings> {
2627
override fun apply(settings: Settings) {
2728
val flutterProjectRoot: File = settings.settingsDir.parentFile

packages/flutter_tools/gradle/src/main/kotlin/FlutterExtension.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.gradle.api.GradleException
1717
* Learn more about extensions in Gradle:
1818
* * https://docs.gradle.org/8.0.2/userguide/custom_plugins.html#sec:getting_input_from_the_build
1919
*/
20+
@Suppress("unused") // The values in this class are used in Flutter developers app-level build.gradle file.
2021
open class FlutterExtension {
2122
/** Sets the compileSdkVersion used by default in Flutter app projects. */
2223
val compileSdkVersion: Int = 35

packages/flutter_tools/gradle/src/main/kotlin/FlutterPlugin.kt

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@
44

55
package com.flutter.gradle
66

7-
import com.android.build.VariantOutput
87
import com.android.build.api.dsl.ApplicationExtension
98
import com.android.build.gradle.AbstractAppExtension
109
import com.android.build.gradle.BaseExtension
1110
import com.android.build.gradle.LibraryExtension
12-
import com.android.build.gradle.api.ApkVariantOutput
13-
import com.android.build.gradle.api.BaseVariant
14-
import com.android.build.gradle.api.BaseVariantOutput
1511
import com.android.build.gradle.tasks.PackageAndroidArtifact
1612
import com.android.build.gradle.tasks.ProcessAndroidResources
1713
import com.flutter.gradle.FlutterPluginUtils.readPropertiesIfExist
@@ -27,6 +23,8 @@ import org.gradle.api.tasks.Copy
2723
import org.gradle.api.tasks.TaskProvider
2824
import org.gradle.api.tasks.bundling.Jar
2925
import org.gradle.internal.os.OperatingSystem
26+
import org.gradle.kotlin.dsl.support.serviceOf
27+
import org.gradle.process.ExecOperations
3028
import java.io.File
3129
import java.nio.charset.StandardCharsets
3230
import java.nio.file.Paths
@@ -54,7 +52,7 @@ class FlutterPlugin : Plugin<Project> {
5452

5553
val flutterRootSystemVal: String? = System.getenv("FLUTTER_ROOT")
5654
val flutterRootPath: String =
57-
resolveProperty("flutter.sdk", flutterRootSystemVal)
55+
resolveFlutterSdkProperty(flutterRootSystemVal)
5856
?: throw GradleException(
5957
"Flutter SDK not found. Define location with flutter.sdk in the " +
6058
"local.properties file or with a FLUTTER_ROOT environment variable."
@@ -145,7 +143,7 @@ class FlutterPlugin : Plugin<Project> {
145143
isUniversalApk = false
146144
}
147145
}
148-
val propDeferredComponentNames: String = "deferred-component-names"
146+
val propDeferredComponentNames = "deferred-component-names"
149147
val deferredComponentNamesValue: String? =
150148
project.findProperty(propDeferredComponentNames) as? String
151149
if (deferredComponentNamesValue != null) {
@@ -280,10 +278,8 @@ class FlutterPlugin : Plugin<Project> {
280278
private fun getExecutableNameForPlatform(baseExecutableName: String): String =
281279
if (OperatingSystem.current().isWindows) "$baseExecutableName.bat" else baseExecutableName
282280

283-
private fun resolveProperty(
284-
propertyName: String,
285-
defaultValue: String?
286-
): String? {
281+
private fun resolveFlutterSdkProperty(defaultValue: String?): String? {
282+
val propertyName = "flutter.sdk"
287283
if (localProperties == null) {
288284
localProperties =
289285
readPropertiesIfExist(File(project!!.projectDir.parentFile, "local.properties"))
@@ -300,7 +296,8 @@ class FlutterPlugin : Plugin<Project> {
300296
rootProject.subprojects.forEach { subproject ->
301297
val gradlew: String =
302298
getExecutableNameForPlatform("${rootProject.projectDir}/gradlew")
303-
rootProject.exec {
299+
val execOps = rootProject.serviceOf<ExecOperations>()
300+
execOps.exec {
304301
workingDir(rootProject.projectDir)
305302
executable(gradlew)
306303
args(":${subproject.name}:dependencies", "--write-locks")
@@ -342,11 +339,18 @@ class FlutterPlugin : Plugin<Project> {
342339
}
343340
val copyFlutterAssetsTask: Task =
344341
addFlutterDeps(variant, flutterPlugin, targetPlatforms)
345-
val variantOutput: BaseVariantOutput = variant.outputs.first()
342+
343+
// TODO(gmackall): Migrate to AGPs variant api.
344+
// https://github.com/flutter/flutter/issues/166550
345+
@Suppress("DEPRECATION")
346+
val variantOutput: com.android.build.gradle.api.BaseVariantOutput = variant.outputs.first()
346347
val processResources: ProcessAndroidResources =
347348
try {
348349
variantOutput.processResourcesProvider.get()
349350
} catch (e: UnknownTaskException) {
351+
// TODO(gmackall): Migrate to AGPs variant api.
352+
// https://github.com/flutter/flutter/issues/166550
353+
@Suppress("DEPRECATION")
350354
variantOutput.processResources
351355
}
352356
processResources.dependsOn(copyFlutterAssetsTask)
@@ -361,19 +365,26 @@ class FlutterPlugin : Plugin<Project> {
361365
// * `build-mode` can be `release|debug|profile`.
362366
variant.outputs.forEach { output ->
363367
assembleTask.doLast {
364-
output as ApkVariantOutput
368+
// TODO(gmackall): Migrate to AGPs variant api.
369+
// https://github.com/flutter/flutter/issues/166550
370+
@Suppress("DEPRECATION")
371+
output as com.android.build.gradle.api.ApkVariantOutput
365372
val packageApplicationProvider: PackageAndroidArtifact =
366373
variant.packageApplicationProvider.get()
367374
val outputDirectory: Directory =
368375
packageApplicationProvider.outputDirectory.get()
369376
val outputDirectoryStr: String = outputDirectory.toString()
370377
var filename = "app"
371-
val abi = output.getFilter(VariantOutput.FilterType.ABI)
378+
379+
// TODO(gmackall): Migrate to AGPs variant api.
380+
// https://github.com/flutter/flutter/issues/166550
381+
@Suppress("DEPRECATION")
382+
val abi = output.getFilter(com.android.build.VariantOutput.FilterType.ABI)
372383
if (abi != null && abi.isNotEmpty()) {
373384
filename += "-$abi"
374385
}
375386
if (variant.flavorName != null && variant.flavorName.isNotEmpty()) {
376-
filename += "-${variant.flavorName.toLowerCase()}"
387+
filename += "-${FlutterPluginUtils.lowercase(variant.flavorName)}"
377388
}
378389
filename += "-${FlutterPluginUtils.buildModeFor(variant.buildType)}"
379390
projectToAddTasksTo.copy {
@@ -395,13 +406,13 @@ class FlutterPlugin : Plugin<Project> {
395406
// This path is not flavor specific and must only be added once.
396407
// If support for flavors is added to native assets, then they must only be added
397408
// once per flavor; see https://github.com/dart-lang/native/issues/1359.
398-
val nativeAssetsDir: String =
409+
val nativeAssetsDir =
399410
"${projectToAddTasksTo.layout.buildDirectory.get()}/../native_assets/android/jniLibs/lib/"
400411
android.sourceSets
401412
.getByName("main")
402413
.jniLibs
403414
.srcDir(nativeAssetsDir)
404-
getPluginHandler(projectToAddTasksTo!!).configurePlugins(engineVersion!!)
415+
getPluginHandler(projectToAddTasksTo).configurePlugins(engineVersion!!)
405416
FlutterPluginUtils.detectLowCompileSdkVersionOrNdkVersion(
406417
projectToAddTasksTo,
407418
getPluginHandler(projectToAddTasksTo).getPluginList()
@@ -469,10 +480,12 @@ class FlutterPlugin : Plugin<Project> {
469480
flutterPlugin,
470481
targetPlatforms
471482
)
483+
// TODO(gmackall): Migrate to AGPs variant api.
484+
// https://github.com/flutter/flutter/issues/166550
472485
val mergeAssets =
473486
projectToAddTasksTo
474487
.tasks
475-
.findByPath(":$hostAppProjectName:merge${appProjectVariant.name.capitalize()}Assets")
488+
.findByPath(":$hostAppProjectName:merge${FlutterPluginUtils.capitalize(appProjectVariant.name)}Assets")
476489
check(mergeAssets != null)
477490
mergeAssets.dependsOn(copyFlutterAssetsTask)
478491
}
@@ -502,7 +515,7 @@ class FlutterPlugin : Plugin<Project> {
502515
* be sure to change any instances of this string in symbols in the code below
503516
* to match.
504517
*/
505-
const val FLUTTER_BUILD_PREFIX: String = "flutterBuild"
518+
private const val FLUTTER_BUILD_PREFIX: String = "flutterBuild"
506519

507520
/**
508521
* Finds a task by name, returning null if the task does not exist.
@@ -517,8 +530,10 @@ class FlutterPlugin : Plugin<Project> {
517530
null
518531
}
519532

533+
// TODO(gmackall): Migrate to AGPs variant api.
534+
// https://github.com/flutter/flutter/issues/166550
520535
private fun addFlutterDeps(
521-
variant: BaseVariant,
536+
@Suppress("DEPRECATION") variant: com.android.build.gradle.api.BaseVariant,
522537
flutterPlugin: FlutterPlugin,
523538
targetPlatforms: List<String>
524539
): Task {
@@ -559,9 +574,15 @@ class FlutterPlugin : Plugin<Project> {
559574
if (FlutterPluginUtils.shouldProjectSplitPerAbi(project)) {
560575
variant.outputs.forEach { output ->
561576
// need to force this as the API does not return the right thing for our use.
562-
output as ApkVariantOutput
577+
// TODO(gmackall): Migrate to AGPs variant api.
578+
// https://github.com/flutter/flutter/issues/166550
579+
@Suppress("DEPRECATION")
580+
output as com.android.build.gradle.api.ApkVariantOutput
581+
// TODO(gmackall): Migrate to AGPs variant api.
582+
// https://github.com/flutter/flutter/issues/166550
583+
@Suppress("DEPRECATION")
563584
val filterIdentifier: String =
564-
output.getFilter(VariantOutput.FilterType.ABI)
585+
output.getFilter(com.android.build.VariantOutput.FilterType.ABI)
565586
val abiVersionCode: Int? = FlutterPluginConstants.ABI_VERSION[filterIdentifier]
566587
if (abiVersionCode != null) {
567588
output.versionCodeOverride
@@ -647,8 +668,8 @@ class FlutterPlugin : Plugin<Project> {
647668
project.layout.buildDirectory.dir("${FlutterPluginConstants.INTERMEDIATES_DIR}/flutter/${variant.name}/libs.jar")
648669
)
649670
val packJniLibsTaskProvider: TaskProvider<Jar> =
650-
project.tasks.register<Jar>(
651-
"packJniLibs${FLUTTER_BUILD_PREFIX}${variant.name.capitalize()}",
671+
project.tasks.register(
672+
"packJniLibs${FLUTTER_BUILD_PREFIX}${FlutterPluginUtils.capitalize(variant.name)}",
652673
Jar::class.java
653674
) {
654675
destinationDirectory.set(libJar.parentFile)
@@ -663,9 +684,9 @@ class FlutterPlugin : Plugin<Project> {
663684
}
664685
// Copy the native assets created by build.dart and placed in build/native_assets by flutter assemble.
665686
// The `$project.layout.buildDirectory` is '.android/Flutter/build/' instead of 'build/'.
666-
val buildDir: String =
687+
val buildDir =
667688
"${FlutterPluginUtils.getFlutterSourceDirectory(project)}/build"
668-
val nativeAssetsDir: String =
689+
val nativeAssetsDir =
669690
"$buildDir/native_assets/android/jniLibs/lib"
670691
from("$nativeAssetsDir/$abi") {
671692
include("*.so")
@@ -683,13 +704,14 @@ class FlutterPlugin : Plugin<Project> {
683704
)
684705
val copyFlutterAssetsTaskProvider: TaskProvider<Copy> =
685706
project.tasks.register(
686-
"copyFlutterAssets${variant.name.capitalize()}",
707+
"copyFlutterAssets${FlutterPluginUtils.capitalize(variant.name)}",
687708
Copy::class.java
688709
) {
689710
dependsOn(compileTask)
690711
with(compileTask.assets)
691712
// TODO(gmackall): Replace with filePermissions.user.read/write = true once
692713
// minimum supported Gradle version is 8.3.
714+
@Suppress("DEPRECATION")
693715
fileMode = 420 // corresponds to unix 0644 in base 8
694716
if (isUsedAsSubproject) {
695717
// TODO(gmackall): above is always false, can delete
@@ -701,20 +723,29 @@ class FlutterPlugin : Plugin<Project> {
701723
try {
702724
variant.mergeAssetsProvider.get()
703725
} catch (e: IllegalStateException) {
726+
// TODO(gmackall): Migrate to AGPs variant api.
727+
// https://github.com/flutter/flutter/issues/166550
728+
@Suppress("DEPRECATION")
704729
variant.mergeAssets
705730
}
706731
dependsOn(mergeAssets)
707-
dependsOn("clean${mergeAssets.name.capitalize()}")
708-
mergeAssets.mustRunAfter("clean${mergeAssets.name.capitalize()}")
732+
dependsOn("clean${FlutterPluginUtils.capitalize(mergeAssets.name)}")
733+
mergeAssets.mustRunAfter("clean${FlutterPluginUtils.capitalize(mergeAssets.name)}")
709734
into(mergeAssets.outputDir)
710735
}
711736
val copyFlutterAssetsTask: Task = copyFlutterAssetsTaskProvider.get()
712737
if (!isUsedAsSubproject) {
713-
val variantOutput: BaseVariantOutput = variant.outputs.first()
738+
// TODO(gmackall): Migrate to AGPs variant api.
739+
// https://github.com/flutter/flutter/issues/166550
740+
@Suppress("DEPRECATION")
741+
val variantOutput: com.android.build.gradle.api.BaseVariantOutput = variant.outputs.first()
714742
val processResources =
715743
try {
716744
variantOutput.processResourcesProvider.get()
717745
} catch (e: IllegalStateException) {
746+
// TODO(gmackall): Migrate to AGPs variant api.
747+
// https://github.com/flutter/flutter/issues/166550
748+
@Suppress("DEPRECATION")
718749
variantOutput.processResources
719750
}
720751
processResources.dependsOn(copyFlutterAssetsTask)
@@ -724,9 +755,9 @@ class FlutterPlugin : Plugin<Project> {
724755
// See https://docs.gradle.org/8.1/userguide/validation_problems.html#implicit_dependency.
725756
val tasksToCheck =
726757
listOf(
727-
"compress${variant.name.capitalize()}Assets",
728-
"bundle${variant.name.capitalize()}Aar",
729-
"bundle${variant.name.capitalize()}LocalLintAar"
758+
"compress${FlutterPluginUtils.capitalize(variant.name)}Assets",
759+
"bundle${FlutterPluginUtils.capitalize(variant.name)}Aar",
760+
"bundle${FlutterPluginUtils.capitalize(variant.name)}LocalLintAar"
730761
)
731762
tasksToCheck.forEach { taskTocheck ->
732763
try {

packages/flutter_tools/gradle/src/main/kotlin/FlutterPluginConstants.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ package com.flutter.gradle
77
// TODO(gmackall): this should be collapsed back into the core FlutterPlugin once the Groovy to
88
// kotlin conversion is complete.
99
object FlutterPluginConstants {
10-
// Strings that define project properties
11-
const val PROP_PROCESS_RESOURCES_PROVIDER = "processResourcesProvider"
12-
1310
/** The platforms that can be passed to the `--Ptarget-platform` flag. */
1411
private const val PLATFORM_ARM32 = "android-arm"
1512
private const val PLATFORM_ARM64 = "android-arm64"
@@ -41,7 +38,7 @@ object FlutterPluginConstants {
4138
* Otherwise, the Play Store will complain that the APK variants have the same version.
4239
*/
4340
@JvmStatic val ABI_VERSION =
44-
mapOf<String, Int>(
41+
mapOf(
4542
ARCH_ARM32 to 1,
4643
ARCH_ARM64 to 2,
4744
ARCH_X86 to 3,

0 commit comments

Comments
 (0)