Add warning for bumping Android SDK version to match plugins (#92451)
This commit is contained in:
parent
fe8e882a48
commit
977c35307f
@ -397,12 +397,40 @@ class FlutterPlugin implements Plugin<Project> {
|
||||
"io.flutter:flutter_embedding_$flutterBuildMode:$engineVersion"
|
||||
)
|
||||
}
|
||||
|
||||
// Wait until the Android plugin loaded.
|
||||
pluginProject.afterEvaluate {
|
||||
if (pluginProject.android.compileSdkVersion > project.android.compileSdkVersion) {
|
||||
project.logger.quiet("Warning: The plugin ${pluginName} requires Android SDK version ${pluginProject.android.compileSdkVersion.substring(8)}.")
|
||||
}
|
||||
project.android.buildTypes.all addEmbeddingDependencyToPlugin
|
||||
}
|
||||
}
|
||||
|
||||
/** Prints error message and fix for any plugin compileSdkVersion that are higher than the project. */
|
||||
private void detectLowCompileSdkVersion() {
|
||||
project.afterEvaluate {
|
||||
int projectCompileSdkVersion = project.android.compileSdkVersion.substring(8) as int
|
||||
int maxPluginCompileSdkVersion = projectCompileSdkVersion
|
||||
int numProcessedPlugins = getPluginList().size()
|
||||
|
||||
getPluginList().each { plugin ->
|
||||
Project pluginProject = project.rootProject.findProject(plugin.key)
|
||||
pluginProject.afterEvaluate {
|
||||
int pluginCompileSdkVersion = pluginProject.android.compileSdkVersion.substring(8) as int
|
||||
maxPluginCompileSdkVersion = Math.max(pluginCompileSdkVersion, maxPluginCompileSdkVersion)
|
||||
|
||||
numProcessedPlugins--
|
||||
if (numProcessedPlugins == 0) {
|
||||
if (maxPluginCompileSdkVersion > projectCompileSdkVersion) {
|
||||
project.logger.error("One or more plugins require a higher Android SDK version.\nFix this issue by adding the following to ${project.projectDir}${File.separator}build.gradle:\nandroid {\n compileSdkVersion ${maxPluginCompileSdkVersion}\n ...\n}\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the given path contains an `android/build.gradle` file.
|
||||
*/
|
||||
@ -933,6 +961,7 @@ class FlutterPlugin implements Plugin<Project> {
|
||||
}
|
||||
}
|
||||
configurePlugins()
|
||||
detectLowCompileSdkVersion()
|
||||
return
|
||||
}
|
||||
// Flutter host module project (Add-to-app).
|
||||
@ -984,6 +1013,7 @@ class FlutterPlugin implements Plugin<Project> {
|
||||
}
|
||||
}
|
||||
configurePlugins()
|
||||
detectLowCompileSdkVersion()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,92 @@
|
||||
// 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_testing/file_testing.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
|
||||
import '../src/common.dart';
|
||||
import 'test_utils.dart';
|
||||
|
||||
void main() {
|
||||
|
||||
late Directory tempDir;
|
||||
|
||||
setUp(() {
|
||||
Cache.flutterRoot = getFlutterRoot();
|
||||
tempDir = createResolvedTempDirectorySync('flutter_plugin_test.');
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
tryToDelete(tempDir);
|
||||
});
|
||||
|
||||
test('error logged when plugin Android compileSdkVersion higher than project', () async {
|
||||
final String flutterBin = fileSystem.path.join(
|
||||
getFlutterRoot(),
|
||||
'bin',
|
||||
'flutter',
|
||||
);
|
||||
|
||||
// Create dummy plugin
|
||||
processManager.runSync(<String>[
|
||||
flutterBin,
|
||||
...getLocalEngineArguments(),
|
||||
'create',
|
||||
'--template=plugin',
|
||||
'--platforms=android',
|
||||
'test_plugin',
|
||||
], workingDirectory: tempDir.path);
|
||||
|
||||
final Directory pluginAppDir = tempDir.childDirectory('test_plugin');
|
||||
final File pluginGradleFile = pluginAppDir.childDirectory('android').childFile('build.gradle');
|
||||
expect(pluginGradleFile, exists);
|
||||
|
||||
final String pluginBuildGradle = pluginGradleFile.readAsStringSync();
|
||||
|
||||
// Bump up plugin compileSdkVersion to 31
|
||||
final RegExp androidCompileSdkVersionRegExp = RegExp(r'compileSdkVersion ([0-9]+|flutter.compileSdkVersion)');
|
||||
final String newPluginGradleFile = pluginBuildGradle.replaceAll(
|
||||
androidCompileSdkVersionRegExp, 'compileSdkVersion 31');
|
||||
pluginGradleFile.writeAsStringSync(newPluginGradleFile);
|
||||
|
||||
final Directory pluginExampleAppDir = pluginAppDir.childDirectory('example');
|
||||
|
||||
final File projectGradleFile = pluginExampleAppDir.childDirectory('android').childDirectory('app').childFile('build.gradle');
|
||||
expect(projectGradleFile, exists);
|
||||
|
||||
final String projectBuildGradle = projectGradleFile.readAsStringSync();
|
||||
|
||||
// Bump down plugin example app compileSdkVersion to 30
|
||||
final String newProjectGradleFile = projectBuildGradle.replaceAll(
|
||||
androidCompileSdkVersionRegExp, 'compileSdkVersion 30');
|
||||
projectGradleFile.writeAsStringSync(newProjectGradleFile);
|
||||
|
||||
// Run flutter build apk to build plugin example project
|
||||
final ProcessResult result = processManager.runSync(<String>[
|
||||
flutterBin,
|
||||
...getLocalEngineArguments(),
|
||||
'build',
|
||||
'apk',
|
||||
'--target-platform=android-arm',
|
||||
], workingDirectory: pluginExampleAppDir.path);
|
||||
|
||||
// Check error message is thrown
|
||||
expect(result.stdout,
|
||||
contains('Warning: The plugin test_plugin requires Android SDK version 31.')
|
||||
);
|
||||
expect(result.stderr,
|
||||
contains('''
|
||||
One or more plugins require a higher Android SDK version.
|
||||
Fix this issue by adding the following to ${projectGradleFile.path}:
|
||||
android {
|
||||
compileSdkVersion 31
|
||||
...
|
||||
}
|
||||
|
||||
'''
|
||||
));
|
||||
});
|
||||
}
|
@ -162,7 +162,7 @@ class MultidexProject extends Project {
|
||||
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
|
||||
|
||||
android {
|
||||
compileSdkVersion 30
|
||||
compileSdkVersion 31
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
|
Loading…
x
Reference in New Issue
Block a user