From cf0d0687e9d412c4f6ebea50a21ef02e36d8640d Mon Sep 17 00:00:00 2001 From: Ratakondala Arun Date: Sun, 2 Jan 2022 17:24:16 +0530 Subject: [PATCH 1/2] add: fallback to local.properties --- lib/android.dart | 22 ++++++++++++++++++---- lib/constants.dart | 10 +++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/android.dart b/lib/android.dart index 5025a6ee48..74a0b437f4 100644 --- a/lib/android.dart +++ b/lib/android.dart @@ -298,15 +298,29 @@ List transformAndroidManifestWithNewLauncherIcon( }).toList(); } -/// Retrieves the minSdk value from the Android build.gradle file +/// Retrieves the minSdk value from the Android build.gradle file or local.properties file int minSdk() { - final File androidGradleFile = File(constants.androidGradleFile); - final List lines = androidGradleFile.readAsLinesSync(); + final androidGradleFile = File(constants.androidGradleFile); + final androidLocalPropertiesFile = File(constants.androidLocalPropertiesFile); + + // look in build.gradle first + final minSdkValue = getMinSdkFromFile(androidGradleFile); + + // look in local.properties. Didn't find minSdk, assume the worst + return minSdkValue != 0 + ? minSdkValue + : getMinSdkFromFile(androidLocalPropertiesFile); +} + +/// Retrieves the minSdk value from [File] +int getMinSdkFromFile(File file) { + final List lines = file.readAsLinesSync(); for (String line in lines) { if (line.contains('minSdkVersion')) { // remove anything from the line that is not a digit final String minSdk = line.replaceAll(RegExp(r'[^\d]'), ''); - return int.parse(minSdk); + // when minSdkVersion value not found + return int.tryParse(minSdk) ?? 0; } } return 0; // Didn't find minSdk, assume the worst diff --git a/lib/constants.dart b/lib/constants.dart index f7916031fd..2588d56c6f 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -1,11 +1,15 @@ -String androidResFolder(String? flavor) => "android/app/src/${flavor ?? 'main'}/res/"; -String androidColorsFile(String? flavor) => "android/app/src/${flavor ?? 'main'}/res/values/colors.xml"; +String androidResFolder(String? flavor) => + "android/app/src/${flavor ?? 'main'}/res/"; +String androidColorsFile(String? flavor) => + "android/app/src/${flavor ?? 'main'}/res/values/colors.xml"; const String androidManifestFile = 'android/app/src/main/AndroidManifest.xml'; const String androidGradleFile = 'android/app/build.gradle'; +const String androidLocalPropertiesFile = 'android/local.properties'; const String androidFileName = 'ic_launcher.png'; const String androidAdaptiveForegroundFileName = 'ic_launcher_foreground.png'; const String androidAdaptiveBackgroundFileName = 'ic_launcher_background.png'; -String androidAdaptiveXmlFolder(String? flavor) => androidResFolder(flavor) + 'mipmap-anydpi-v26/'; +String androidAdaptiveXmlFolder(String? flavor) => + androidResFolder(flavor) + 'mipmap-anydpi-v26/'; const String androidDefaultIconName = 'ic_launcher'; const String iosDefaultIconFolder = From 6915e2fc76e936452af4d152276e2222a65c092e Mon Sep 17 00:00:00 2001 From: Ratakondala Arun Date: Sun, 2 Jan 2022 17:26:17 +0530 Subject: [PATCH 2/2] add: error message if minSdkValue is not found --- lib/constants.dart | 3 +++ lib/main.dart | 3 +++ 2 files changed, 6 insertions(+) diff --git a/lib/constants.dart b/lib/constants.dart index 2588d56c6f..cb22b10227 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -25,6 +25,9 @@ const String errorMissingPlatform = const String errorMissingRegularAndroid = 'Adaptive icon config found but no regular Android config. ' 'Below API 26 the regular Android config is required'; +const String errorMissingMinSdk = + 'Cannot not find minSdk from android/app/build.gradle or android/local.properties' + 'Specify minSdk in either android/app/build.gradle or android/local.properties'; const String errorIncorrectIconName = 'The icon name must contain only lowercase a-z, 0-9, or underscore: ' 'E.g. "ic_my_new_icon"'; diff --git a/lib/main.dart b/lib/main.dart index 1edea61726..d7176af4c6 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -92,6 +92,9 @@ Future createIconsFromConfig(Map config, if (isNeedingNewAndroidIcon(config) || hasAndroidAdaptiveConfig(config)) { final int minSdk = android_launcher_icons.minSdk(); + if (minSdk == 0) { + throw const InvalidConfigException(errorMissingMinSdk); + } if (minSdk < 26 && hasAndroidAdaptiveConfig(config) && !hasAndroidConfig(config)) {