Skip to content

Commit

Permalink
reads min/target sdk versions from localproperties (#98450)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunotacca committed Feb 24, 2022
1 parent 320d2cf commit 37a1aaf
Show file tree
Hide file tree
Showing 16 changed files with 181 additions and 45 deletions.
14 changes: 12 additions & 2 deletions examples/api/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def flutterMinSdkVersion = localProperties.getProperty('flutter.minSdkVersion')
if (flutterMinSdkVersion == null) {
flutterMinSdkVersion = flutter.minSdkVersion
}

def flutterTargetSdkVersion = localProperties.getProperty('flutter.targetSdkVersion')
if (flutterTargetSdkVersion == null) {
flutterTargetSdkVersion = flutter.targetSdkVersion
}

android {
compileSdkVersion flutter.compileSdkVersion

Expand All @@ -48,8 +58,8 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "dev.flutter.flutter_api_samples"
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
minSdkVersion flutterMinSdkVersion
targetSdkVersion flutterTargetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
14 changes: 12 additions & 2 deletions examples/flutter_view/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ if (flutterVersionName == null) {
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def flutterMinSdkVersion = localProperties.getProperty('flutter.minSdkVersion')
if (flutterMinSdkVersion == null) {
flutterMinSdkVersion = flutter.minSdkVersion
}

def flutterTargetSdkVersion = localProperties.getProperty('flutter.targetSdkVersion')
if (flutterTargetSdkVersion == null) {
flutterTargetSdkVersion = flutter.targetSdkVersion
}

android {
compileSdkVersion flutter.compileSdkVersion

Expand All @@ -38,8 +48,8 @@ android {

defaultConfig {
applicationId "io.flutter.examples.flutter_view"
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
minSdkVersion flutterMinSdkVersion
targetSdkVersion flutterTargetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
14 changes: 12 additions & 2 deletions examples/hello_world/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ if (flutterVersionName == null) {
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def flutterMinSdkVersion = localProperties.getProperty('flutter.minSdkVersion')
if (flutterMinSdkVersion == null) {
flutterMinSdkVersion = flutter.minSdkVersion
}

def flutterTargetSdkVersion = localProperties.getProperty('flutter.targetSdkVersion')
if (flutterTargetSdkVersion == null) {
flutterTargetSdkVersion = flutter.targetSdkVersion
}

android {
compileSdkVersion flutter.compileSdkVersion

Expand All @@ -38,8 +48,8 @@ android {

defaultConfig {
applicationId "io.flutter.examples.hello_world"
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
minSdkVersion flutterMinSdkVersion
targetSdkVersion flutterTargetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
14 changes: 12 additions & 2 deletions examples/image_list/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ if (flutterVersionName == null) {
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def flutterMinSdkVersion = localProperties.getProperty('flutter.minSdkVersion')
if (flutterMinSdkVersion == null) {
flutterMinSdkVersion = flutter.minSdkVersion
}

def flutterTargetSdkVersion = localProperties.getProperty('flutter.targetSdkVersion')
if (flutterTargetSdkVersion == null) {
flutterTargetSdkVersion = flutter.targetSdkVersion
}

android {
compileSdkVersion flutter.compileSdkVersion

Expand All @@ -39,8 +49,8 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.image_list"
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
minSdkVersion flutterMinSdkVersion
targetSdkVersion flutterTargetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
14 changes: 12 additions & 2 deletions examples/layers/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ if (flutterVersionName == null) {
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def flutterMinSdkVersion = localProperties.getProperty('flutter.minSdkVersion')
if (flutterMinSdkVersion == null) {
flutterMinSdkVersion = flutter.minSdkVersion
}

def flutterTargetSdkVersion = localProperties.getProperty('flutter.targetSdkVersion')
if (flutterTargetSdkVersion == null) {
flutterTargetSdkVersion = flutter.targetSdkVersion
}

android {
compileSdkVersion flutter.compileSdkVersion

Expand All @@ -38,8 +48,8 @@ android {

defaultConfig {
applicationId "io.flutter.examples.layers"
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
minSdkVersion flutterMinSdkVersion
targetSdkVersion flutterTargetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
14 changes: 12 additions & 2 deletions examples/platform_channel/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ if (flutterVersionName == null) {
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def flutterMinSdkVersion = localProperties.getProperty('flutter.minSdkVersion')
if (flutterMinSdkVersion == null) {
flutterMinSdkVersion = flutter.minSdkVersion
}

def flutterTargetSdkVersion = localProperties.getProperty('flutter.targetSdkVersion')
if (flutterTargetSdkVersion == null) {
flutterTargetSdkVersion = flutter.targetSdkVersion
}

android {
compileSdkVersion flutter.compileSdkVersion

Expand All @@ -38,8 +48,8 @@ android {

defaultConfig {
applicationId "io.flutter.examples.platform_channel"
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
minSdkVersion flutterMinSdkVersion
targetSdkVersion flutterTargetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
14 changes: 12 additions & 2 deletions examples/platform_view/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ if (flutterVersionName == null) {
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def flutterMinSdkVersion = localProperties.getProperty('flutter.minSdkVersion')
if (flutterMinSdkVersion == null) {
flutterMinSdkVersion = flutter.minSdkVersion
}

def flutterTargetSdkVersion = localProperties.getProperty('flutter.targetSdkVersion')
if (flutterTargetSdkVersion == null) {
flutterTargetSdkVersion = flutter.targetSdkVersion
}

android {
compileSdkVersion flutter.compileSdkVersion

Expand All @@ -38,8 +48,8 @@ android {

defaultConfig {
applicationId "io.flutter.examples.platform_view"
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
minSdkVersion flutterMinSdkVersion
targetSdkVersion flutterTargetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ variable to be set. The full invocation to run everything might
therefore look something like:

```shell
$ FLUTTER_ROOT=~/path/to/flutter-sdk
$ export FLUTTER_ROOT=~/path/to/flutter-sdk
$ flutter test --concurrency 1
```

Expand Down
31 changes: 30 additions & 1 deletion packages/flutter_tools/gradle/flutter.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ class FlutterPlugin implements Plugin<Project> {
private Properties localProperties
private String engineVersion

/**
* Flutter Docs Website references URLs for help messages.
*/
private final String kWebsiteDeploymentAndroidBuildConfig = 'https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration'

@Override
void apply(Project project) {
this.project = project
Expand Down Expand Up @@ -300,7 +305,7 @@ class FlutterPlugin implements Plugin<Project> {
localEngine = engineOut.name
localEngineSrcPath = engineOut.parentFile.parent
}
project.android.buildTypes.all this.&addFlutterDependencies
project.android.buildTypes.all this.&addFlutterDependencies
}

private static Boolean shouldShrinkResources(Project project) {
Expand Down Expand Up @@ -409,9 +414,33 @@ class FlutterPlugin implements Plugin<Project> {

// Wait until the Android plugin loaded.
pluginProject.afterEvaluate {
Boolean showBuildConfigurationMessage = false

// 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 ${pluginProject.android.compileSdkVersion.substring(8)}.")
showBuildConfigurationMessage = true
}

// Checks if there is a mismatch between the plugin minSdkVersion and the project minSdkVersion.
if(pluginProject.android.defaultConfig?.minSdkVersion?.apiLevel > project.android.defaultConfig?.minSdkVersion?.apiLevel) {
project.logger.quiet("Warning: The plugin ${pluginName} requires minSdkVersion ${pluginProject.android.defaultConfig?.minSdkVersion?.apiLevel}.")
showBuildConfigurationMessage = true

if (!resolveProperty("flutter.minSdkVersion", null)) {
project.logger.quiet("flutter.minSdkVersion not found on ${project.projectDir.parentFile}${File.separator}local.properties, consider adding it. Using ${project.android.defaultConfig.minSdkVersion.apiLevel} as default.")
}

// Additionally checks if the targetSdkVersion was set.
if (!resolveProperty("flutter.targetSdkVersion", null)) {
project.logger.quiet("flutter.targetSdkVersion not found on ${project.projectDir.parentFile}${File.separator}local.properties, consider adding it. Using ${project.android.defaultConfig.targetSdkVersion.apiLevel} as default.")
}
}

if (showBuildConfigurationMessage) {
project.logger.quiet("For more information about build configuration, see $kWebsiteDeploymentAndroidBuildConfig.")
}

project.android.buildTypes.all addEmbeddingDependencyToPlugin
}
}
Expand Down
23 changes: 10 additions & 13 deletions packages/flutter_tools/lib/src/android/gradle_errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -390,27 +390,24 @@ final GradleHandledError minSdkVersion = GradleHandledError(
required bool usesAndroidX,
required bool multidexEnabled,
}) async {
final File gradleFile = project.directory
final File localPropertiesFile = project.directory
.childDirectory('android')
.childDirectory('app')
.childFile('build.gradle');
.childFile('local.properties');

final Match? minSdkVersionMatch = _minSdkVersionPattern.firstMatch(line);
assert(minSdkVersionMatch?.groupCount == 3);

final String textInBold = globals.logger.terminal.bolden(
'Fix this issue by adding the following to the file ${gradleFile.path}:\n'
'android {\n'
' defaultConfig {\n'
' minSdkVersion ${minSdkVersionMatch?.group(2)}\n'
' }\n'
'}\n'
'Fix this issue by adding the following to the file ${localPropertiesFile.path}:\n'
'\n'
'flutter.minSdkVersion=${minSdkVersionMatch?.group(2)}\n'
);
globals.printBox(
'The plugin ${minSdkVersionMatch?.group(3)} requires a higher Android SDK version.\n'
'$textInBold\n'
"Note that your app won't be available to users running Android SDKs below ${minSdkVersionMatch?.group(2)}.\n"
'Alternatively, try to find a version of this plugin that supports these lower versions of the Android SDK.',
'Alternatively, try to find a version of this plugin that supports these lower versions of the Android SDK.\n'
'For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration',
title: _boxTitle,
);
return GradleBuildStatus.exit;
Expand Down Expand Up @@ -518,8 +515,8 @@ final GradleHandledError minCompileSdkVersionHandler = GradleHandledError(
required bool usesAndroidX,
required bool multidexEnabled,
}) async {
final Match? minSdkVersionMatch = _minCompileSdkVersionPattern.firstMatch(line);
assert(minSdkVersionMatch?.groupCount == 1);
final Match? minCompileSdkVersionMatch = _minCompileSdkVersionPattern.firstMatch(line);
assert(minCompileSdkVersionMatch?.groupCount == 1);

final File gradleFile = project.directory
.childDirectory('android')
Expand All @@ -529,7 +526,7 @@ final GradleHandledError minCompileSdkVersionHandler = GradleHandledError(
'${globals.logger.terminal.warningMark} Your project requires a higher compileSdkVersion.\n'
'Fix this issue by bumping the compileSdkVersion in ${gradleFile.path}:\n'
'android {\n'
' compileSdkVersion ${minSdkVersionMatch?.group(1)}\n'
' compileSdkVersion ${minCompileSdkVersionMatch?.group(1)}\n'
'}',
title: _boxTitle,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def flutterMinSdkVersion = localProperties.getProperty('flutter.minSdkVersion')
if (flutterMinSdkVersion == null) {
flutterMinSdkVersion = flutter.minSdkVersion
}

def flutterTargetSdkVersion = localProperties.getProperty('flutter.targetSdkVersion')
if (flutterTargetSdkVersion == null) {
flutterTargetSdkVersion = flutter.targetSdkVersion
}

android {
compileSdkVersion flutter.compileSdkVersion
ndkVersion flutter.ndkVersion
Expand All @@ -37,8 +47,8 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "{{androidIdentifier}}"
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
minSdkVersion flutterMinSdkVersion
targetSdkVersion flutterTargetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def flutterMinSdkVersion = localProperties.getProperty('flutter.minSdkVersion')
if (flutterMinSdkVersion == null) {
flutterMinSdkVersion = flutter.minSdkVersion
}

def flutterTargetSdkVersion = localProperties.getProperty('flutter.targetSdkVersion')
if (flutterTargetSdkVersion == null) {
flutterTargetSdkVersion = flutter.targetSdkVersion
}

android {
compileSdkVersion flutter.compileSdkVersion
ndkVersion flutter.ndkVersion
Expand All @@ -45,8 +55,8 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "{{androidIdentifier}}"
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
minSdkVersion flutterMinSdkVersion
targetSdkVersion flutterTargetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ if (flutterVersionName == null) {
apply plugin: 'com.android.library'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def flutterMinSdkVersion = localProperties.getProperty('flutter.minSdkVersion')
if (flutterMinSdkVersion == null) {
flutterMinSdkVersion = flutter.minSdkVersion
}

def flutterTargetSdkVersion = localProperties.getProperty('flutter.targetSdkVersion')
if (flutterTargetSdkVersion == null) {
flutterTargetSdkVersion = flutter.targetSdkVersion
}

group '{{androidIdentifier}}'
version '1.0'

Expand All @@ -39,8 +49,8 @@ android {
}

defaultConfig {
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
minSdkVersion flutterMinSdkVersion
targetSdkVersion flutterTargetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down

0 comments on commit 37a1aaf

Please sign in to comment.