From cf0d0687e9d412c4f6ebea50a21ef02e36d8640d Mon Sep 17 00:00:00 2001 From: Ratakondala Arun Date: Sun, 2 Jan 2022 17:24:16 +0530 Subject: [PATCH] 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 =