[go: nahoru, domu]

Skip to content

Commit

Permalink
Reland: "Fix how Gradle resolves Android plugin" (#137115)
Browse files Browse the repository at this point in the history
Relands #97823

When the tool migrated to `.flutter-plugins-dependencies`, the Gradle plugin was never changed.
Until now, the plugin had the heuristic that a plugin with a `android/build.gradle` file supported the Android platform.

Also applies schema of `getPluginDependencies` to `getPluginList` which uses a `List` of Object instead of `Properties`.

Fixes #97729
Cause of the error: https://github.com/flutter/flutter/blob/5f105a6ca7a5ac7b8bc9b241f4c2d86f4188cf5c/packages/flutter_tools/gradle/flutter.gradle#L421C25-L421C25

Fixes #98048
The deprecated line `include ":$name"` in `settings.gradle` (pluginEach) in old projects causes the `project.rootProject.findProject` to also find the plugin "project", so it is not failing on the `afterEvaluate` method. But the plugin shouldn't be included in the first place as it fails with `Could not find method implementation() for arguments` error in special cases.

Related to #48918, see [_writeFlutterPluginsListLegacy](https://github.com/flutter/flutter/blob/27bc1cf61a5b54bf655062be63050123abb617e4/packages/flutter_tools/lib/src/flutter_plugins.dart#L248).

Co-authored-by: Emmanuel Garcia <egarciad@google.com>
  • Loading branch information
Gustl22 and Emmanuel Garcia committed Dec 21, 2023
1 parent 430254e commit f5ac225
Show file tree
Hide file tree
Showing 7 changed files with 619 additions and 128 deletions.
39 changes: 12 additions & 27 deletions packages/flutter_tools/gradle/module_plugin_loader.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,26 @@
// This file is included from `<module>/.android/include_flutter.groovy`,
// so it can be versioned with the Flutter SDK.

import groovy.json.JsonSlurper
import java.nio.file.Paths

File pathToThisDirectory = buildscript.sourceFile.parentFile
apply from: Paths.get(pathToThisDirectory.absolutePath, "src", "main", "groovy", "native_plugin_loader.groovy")

def moduleProjectRoot = project(':flutter').projectDir.parentFile.parentFile

def object = null;
String flutterModulePath = project(':flutter').projectDir.parentFile.getAbsolutePath()
// If this logic is changed, also change the logic in app_plugin_loader.gradle.
def pluginsFile = new File(moduleProjectRoot, '.flutter-plugins-dependencies')
if (pluginsFile.exists()) {
object = new JsonSlurper().parseText(pluginsFile.text)
assert object instanceof Map
assert object.plugins instanceof Map
assert object.plugins.android instanceof List
// Includes the Flutter plugins that support the Android platform.
object.plugins.android.each { androidPlugin ->
assert androidPlugin.name instanceof String
assert androidPlugin.path instanceof String
// Skip plugins that have no native build (such as a Dart-only
// implementation of a federated plugin).
def needsBuild = androidPlugin.containsKey('native_build') ? androidPlugin['native_build'] : true
if (!needsBuild) {
return
}
def pluginDirectory = new File(androidPlugin.path, 'android')
assert pluginDirectory.exists()
include ":${androidPlugin.name}"
project(":${androidPlugin.name}").projectDir = pluginDirectory
}
List<Map<String, Object>> nativePlugins = nativePluginLoader.getPlugins(moduleProjectRoot)
nativePlugins.each { androidPlugin ->
def pluginDirectory = new File(androidPlugin.path as String, 'android')
assert pluginDirectory.exists()
include ":${androidPlugin.name}"
project(":${androidPlugin.name}").projectDir = pluginDirectory
}

String flutterModulePath = project(':flutter').projectDir.parentFile.getAbsolutePath()
gradle.getGradle().projectsLoaded { g ->
g.rootProject.beforeEvaluate { p ->
p.subprojects { subproject ->
if (object != null && object.plugins != null && object.plugins.android != null
&& object.plugins.android.name.contains(subproject.name)) {
if (nativePlugins.name.contains(subproject.name)) {
File androidPluginBuildOutputDir = new File(flutterModulePath + File.separator
+ "plugins_build_output" + File.separator + subproject.name);
if (!androidPluginBuildOutputDir.exists()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
import groovy.json.JsonSlurper
import org.gradle.api.Plugin
import org.gradle.api.initialization.Settings

import java.nio.file.Paths

apply plugin: FlutterAppPluginLoaderPlugin

class FlutterAppPluginLoaderPlugin implements Plugin<Settings> {
// This string must match _kFlutterPluginsHasNativeBuildKey defined in
// packages/flutter_tools/lib/src/flutter_plugins.dart.
private final String nativeBuildKey = 'native_build'

@Override
void apply(Settings settings) {
def flutterProjectRoot = settings.settingsDir.parentFile

// If this logic is changed, also change the logic in module_plugin_loader.gradle.
def pluginsFile = new File(flutterProjectRoot, '.flutter-plugins-dependencies')
if (!pluginsFile.exists()) {
return
if(!settings.ext.hasProperty('flutterSdkPath')) {
def properties = new Properties()
def localPropertiesFile = new File(settings.rootProject.projectDir, "local.properties")
localPropertiesFile.withInputStream { properties.load(it) }
settings.ext.flutterSdkPath = properties.getProperty("flutter.sdk")
assert settings.ext.flutterSdkPath != null, "flutter.sdk not set in local.properties"
}

// Load shared gradle functions
settings.apply from: Paths.get(settings.ext.flutterSdkPath, "packages", "flutter_tools", "gradle", "src", "main", "groovy", "native_plugin_loader.groovy")

def object = new JsonSlurper().parseText(pluginsFile.text)
assert object instanceof Map
assert object.plugins instanceof Map
assert object.plugins.android instanceof List
// Includes the Flutter plugins that support the Android platform.
object.plugins.android.each { androidPlugin ->
assert androidPlugin.name instanceof String
assert androidPlugin.path instanceof String
// Skip plugins that have no native build (such as a Dart-only implementation
// of a federated plugin).
def needsBuild = androidPlugin.containsKey(nativeBuildKey) ? androidPlugin[nativeBuildKey] : true
if (!needsBuild) {
return
}
def pluginDirectory = new File(androidPlugin.path, 'android')
List<Map<String, Object>> nativePlugins = settings.ext.nativePluginLoader.getPlugins(flutterProjectRoot)
nativePlugins.each { androidPlugin ->
def pluginDirectory = new File(androidPlugin.path as String, 'android')
assert pluginDirectory.exists()
settings.include(":${androidPlugin.name}")
settings.project(":${androidPlugin.name}").projectDir = pluginDirectory
Expand Down
183 changes: 106 additions & 77 deletions packages/flutter_tools/gradle/src/main/groovy/flutter.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// found in the LICENSE file.

import com.android.build.OutputFile
import groovy.json.JsonSlurper
import groovy.json.JsonGenerator
import groovy.xml.QName
import java.nio.file.Paths
Expand Down Expand Up @@ -65,7 +64,7 @@ class FlutterExtension {
* Specifies the relative directory to the Flutter project directory.
* In an app project, this is ../.. since the app's build.gradle is under android/app.
*/
String source
String source = '../..'

/** Allows to override the target file. Otherwise, the target is lib/main.dart. */
String target
Expand Down Expand Up @@ -222,6 +221,9 @@ class FlutterPlugin implements Plugin<Project> {
}
}

// Load shared gradle functions
project.apply from: Paths.get(flutterRoot.absolutePath, "packages", "flutter_tools", "gradle", "src", "main", "groovy", "native_plugin_loader.groovy")

project.extensions.create("flutter", FlutterExtension)
this.addFlutterTasks(project)

Expand Down Expand Up @@ -357,7 +359,7 @@ class FlutterPlugin implements Plugin<Project> {
// This prevents duplicated classes when using custom build types. That is, a custom build
// type like profile is used, and the plugin and app projects have API dependencies on the
// embedding.
if (!isFlutterAppProject() || getPluginList().size() == 0) {
if (!isFlutterAppProject() || getPluginList(project).size() == 0) {
addApiDependencies(project, buildType.name,
"io.flutter:flutter_embedding_$flutterBuildMode:$engineVersion")
}
Expand Down Expand Up @@ -390,19 +392,77 @@ class FlutterPlugin implements Plugin<Project> {
* Configures the Flutter plugin dependencies.
*
* The plugins are added to pubspec.yaml. Then, upon running `flutter pub get`,
* the tool generates a `.flutter-plugins` file, which contains a 1:1 map to each plugin location.
* the tool generates a `.flutter-plugins-dependencies` file, which contains a map to each plugin location.
* Finally, the project's `settings.gradle` loads each plugin's android directory as a subproject.
*/
private void configurePlugins() {
getPluginList().each(this.&configurePluginProject)
getPluginDependencies().each(this.&configurePluginDependencies)
private void configurePlugins(Project project) {
configureLegacyPluginEachProjects(project)
getPluginList(project).each(this.&configurePluginProject)
getPluginList(project).each(this.&configurePluginDependencies)
}

// TODO(54566, 48918): Can remove once the issues are resolved.
// This means all references to `.flutter-plugins` are then removed and
// apps only depend exclusively on the `plugins` property in `.flutter-plugins-dependencies`.
/**
* Workaround to load non-native plugins for developers who may still use an
* old `settings.gradle` which includes all the plugins from the
* `.flutter-plugins` file, even if not made for Android.
* The settings.gradle then:
* 1) tries to add the android plugin implementation, which does not
* exist at all, but is also not included successfully
* (which does not throw an error and therefore isn't a problem), or
* 2) includes the plugin successfully as a valid android plugin
* directory exists, even if the surrounding flutter package does not
* support the android platform (see e.g. apple_maps_flutter: 1.0.1).
* So as it's included successfully it expects to be added as API.
* This is only possible by taking all plugins into account, which
* only appear on the `dependencyGraph` and in the `.flutter-plugins` file.
* So in summary the plugins are currently selected from the `dependencyGraph`
* and filtered then with the [doesSupportAndroidPlatform] method instead of
* just using the `plugins.android` list.
*/
private configureLegacyPluginEachProjects(Project project) {
File settingsGradle = new File(project.projectDir.parentFile, 'settings.gradle')
try {
if (!settingsGradle.text.contains("'.flutter-plugins'")) {
return
}
} catch (FileNotFoundException ignored) {
throw new GradleException("settings.gradle does not exist: ${settingsGradle.absolutePath}")
}
List<Map<String, Object>> deps = getPluginDependencies(project)
List<String> plugins = getPluginList(project).collect { it.name as String }
deps.removeIf { plugins.contains(it.name) }
deps.each {
Project pluginProject = project.rootProject.findProject(":${it.name}")
if (pluginProject == null) {
// Plugin was not included in `settings.gradle`, but is listed in `.flutter-plugins`.
project.logger.error("Plugin project :${it.name} listed, but not found. Please fix your settings.gradle.")
} else if (doesSupportAndroidPlatform(pluginProject.projectDir.parentFile.path as String)) {
// Plugin has a functioning `android` folder and is included successfully, although it's not supported.
// It must be configured nonetheless, to not throw an "Unresolved reference" exception.
configurePluginProject(it)
} else {
// Plugin has no or an empty `android` folder. No action required.
}
}
}

// TODO(54566): Can remove this function and its call sites once resolved.
/**
* Returns `true` if the given path contains an `android/build.gradle` file.
*/
private static Boolean doesSupportAndroidPlatform(String path) {
File editableAndroidProject = new File(path, 'android' + File.separator + 'build.gradle')
return editableAndroidProject.exists()
}

/** Adds the plugin project dependency to the app project. */
private void configurePluginProject(String pluginName, String _) {
Project pluginProject = project.rootProject.findProject(":$pluginName")
private void configurePluginProject(Map<String, Object> pluginObject) {
assert(pluginObject.name instanceof String)
Project pluginProject = project.rootProject.findProject(":${pluginObject.name}")
if (pluginProject == null) {
project.logger.error("Plugin project :$pluginName not found. Please update settings.gradle.")
return
}
// Add plugin dependency to the app project.
Expand Down Expand Up @@ -441,7 +501,7 @@ class FlutterPlugin implements Plugin<Project> {
pluginProject.afterEvaluate {
// Checks if there is a mismatch between the plugin compileSdkVersion and the project compileSdkVersion.
if (pluginProject.android.compileSdkVersion > project.android.compileSdkVersion) {
project.logger.quiet("Warning: The plugin ${pluginName} requires Android SDK version ${getCompileSdkFromProject(pluginProject)} or higher.")
project.logger.quiet("Warning: The plugin ${pluginObject.name} requires Android SDK version ${getCompileSdkFromProject(pluginProject)} or higher.")
project.logger.quiet("For more information about build configuration, see $kWebsiteDeploymentAndroidBuildConfig.")
}

Expand Down Expand Up @@ -493,10 +553,14 @@ class FlutterPlugin implements Plugin<Project> {
String ndkVersionIfUnspecified = "21.1.6352462" /* The default for AGP 4.1.0 used in old templates. */
String projectNdkVersion = project.android.ndkVersion ?: ndkVersionIfUnspecified
String maxPluginNdkVersion = projectNdkVersion
int numProcessedPlugins = getPluginList().size()
int numProcessedPlugins = getPluginList(project).size()

getPluginList().each { plugin ->
Project pluginProject = project.rootProject.findProject(plugin.key)
getPluginList(project).each { pluginObject ->
assert(pluginObject.name instanceof String)
Project pluginProject = project.rootProject.findProject(":${pluginObject.name}")
if (pluginProject == null) {
return
}
pluginProject.afterEvaluate {
// Default to int min if using a preview version to skip the sdk check.
int pluginCompileSdkVersion = Integer.MIN_VALUE;
Expand Down Expand Up @@ -530,35 +594,25 @@ class FlutterPlugin implements Plugin<Project> {
return gradleProject.android.compileSdkVersion.substring(8);
}

/**
* Returns `true` if the given path contains an `android/build.gradle` file.
*/
private Boolean doesSupportAndroidPlatform(String path) {
File editableAndroidProject = new File(path, 'android' + File.separator + 'build.gradle')
return editableAndroidProject.exists()
}

/**
* Add the dependencies on other plugin projects to the plugin project.
* A plugin A can depend on plugin B. As a result, this dependency must be surfaced by
* making the Gradle plugin project A depend on the Gradle plugin project B.
*/
private void configurePluginDependencies(Object dependencyObject) {
assert(dependencyObject.name instanceof String)
Project pluginProject = project.rootProject.findProject(":${dependencyObject.name}")
if (pluginProject == null ||
!doesSupportAndroidPlatform(pluginProject.projectDir.parentFile.path)) {
private void configurePluginDependencies(Map<String, Object> pluginObject) {
assert(pluginObject.name instanceof String)
Project pluginProject = project.rootProject.findProject(":${pluginObject.name}")
if (pluginProject == null) {
return
}
assert(dependencyObject.dependencies instanceof List)
dependencyObject.dependencies.each { pluginDependencyName ->
assert(pluginDependencyName instanceof String)
def dependencies = pluginObject.dependencies
assert(dependencies instanceof List<String>)
dependencies.each { pluginDependencyName ->
if (pluginDependencyName.empty) {
return
}
Project dependencyProject = project.rootProject.findProject(":$pluginDependencyName")
if (dependencyProject == null ||
!doesSupportAndroidPlatform(dependencyProject.projectDir.parentFile.path)) {
if (dependencyProject == null) {
return
}
// Wait for the Android plugin to load and add the dependency to the plugin project.
Expand All @@ -570,52 +624,27 @@ class FlutterPlugin implements Plugin<Project> {
}
}

private Properties getPluginList() {
File pluginsFile = new File(project.projectDir.parentFile.parentFile, '.flutter-plugins')
Properties allPlugins = readPropertiesIfExist(pluginsFile)
Properties androidPlugins = new Properties()
allPlugins.each { name, path ->
if (doesSupportAndroidPlatform(path)) {
androidPlugins.setProperty(name, path)
}
// TODO(amirh): log an error if this plugin was specified to be an Android
// plugin according to the new schema, and was missing a build.gradle file.
// https://github.com/flutter/flutter/issues/40784
}
return androidPlugins
/**
* Gets the list of plugins (as map) that support the Android platform.
*
* The map value contains either the plugins `name` (String),
* its `path` (String), or its `dependencies` (List<String>).
* See [NativePluginLoader#getPlugins] in packages/flutter_tools/gradle/src/main/groovy/native_plugin_loader.groovy
*/
private List<Map<String, Object>> getPluginList(Project project) {
return project.ext.nativePluginLoader.getPlugins(getFlutterSourceDirectory())
}

// TODO(54566, 48918): Remove in favor of [getPluginList] only, see also
// https://github.com/flutter/flutter/blob/1c90ed8b64d9ed8ce2431afad8bc6e6d9acc4556/packages/flutter_tools/lib/src/flutter_plugins.dart#L212
/** Gets the plugins dependencies from `.flutter-plugins-dependencies`. */
private List getPluginDependencies() {
// Consider a `.flutter-plugins-dependencies` file with the following content:
// {
// "dependencyGraph": [
// {
// "name": "plugin-a",
// "dependencies": ["plugin-b","plugin-c"]
// },
// {
// "name": "plugin-b",
// "dependencies": ["plugin-c"]
// },
// {
// "name": "plugin-c",
// "dependencies": []'
// }
// ]
// }
//
// This means, `plugin-a` depends on `plugin-b` and `plugin-c`.
// `plugin-b` depends on `plugin-c`.
// `plugin-c` doesn't depend on anything.
File pluginsDependencyFile = new File(project.projectDir.parentFile.parentFile, '.flutter-plugins-dependencies')
if (pluginsDependencyFile.exists()) {
def object = new JsonSlurper().parseText(pluginsDependencyFile.text)
assert(object instanceof Map)
assert(object.dependencyGraph instanceof List)
return object.dependencyGraph
}
return []
private List<Map<String, Object>> getPluginDependencies(Project project) {
Map meta = project.ext.nativePluginLoader.getDependenciesMetadata(getFlutterSourceDirectory())
if (meta == null) {
return []
}
assert(meta.dependencyGraph instanceof List<Map>)
return meta.dependencyGraph as List<Map<String, Object>>
}

private static String toCamelCase(List<String> parts) {
Expand Down Expand Up @@ -1226,7 +1255,7 @@ class FlutterPlugin implements Plugin<Project> {
def nativeAssetsDir = "${project.buildDir}/../native_assets/android/jniLibs/lib/"
project.android.sourceSets.main.jniLibs.srcDir(nativeAssetsDir)
}
configurePlugins()
configurePlugins(project)
detectLowCompileSdkVersionOrNdkVersion()
return
}
Expand Down Expand Up @@ -1278,7 +1307,7 @@ class FlutterPlugin implements Plugin<Project> {
}
}
}
configurePlugins()
configurePlugins(project)
detectLowCompileSdkVersionOrNdkVersion()
}

Expand Down
Loading

0 comments on commit f5ac225

Please sign in to comment.