diff --git a/packages/flutter_tools/lib/src/android/android_studio.dart b/packages/flutter_tools/lib/src/android/android_studio.dart index c22ddaad641..b93d8913d94 100644 --- a/packages/flutter_tools/lib/src/android/android_studio.dart +++ b/packages/flutter_tools/lib/src/android/android_studio.dart @@ -56,7 +56,7 @@ class AndroidStudio { Version? version; if (versionString != null) { - version = Version.parse(versionString); + version = _parseVersion(versionString); } String? pathsSelectorValue; @@ -565,6 +565,35 @@ the configured path by running this command: flutter config --android-studio-dir } } + static Version? _parseVersion(String text) { + // Matches the version string for Preview builds on macOS. + // Example match: EAP AI-242.21829.142.2422.12358220 + // We try to capture "2422" here, which can be translated to a + // more human-friendly "24.2.2". + final RegExp eapVersionPattern = RegExp(r'EAP\s+[A-Z]{2}-\d+\.\d+\.\d+\.(\d+)\.\d+'); + final Match? eapVersionMatch = eapVersionPattern.firstMatch(text); + + if (eapVersionMatch == null) { + return Version.parse(text); + } + + final String? rawVersionMatch = eapVersionMatch.group(1); + + // length of 4 is because that how version is encrypted: first two digits + // for year (part of major version), third for minor version, fourth for patch. + if (rawVersionMatch == null || rawVersionMatch.length != 4) { + return null; + } + + final int? major = int.tryParse('20${rawVersionMatch[0]}${rawVersionMatch[1]}'); + final int? minor = int.tryParse(rawVersionMatch[2]); + final int? patch = int.tryParse(rawVersionMatch[3]); + if (major == null || minor == null || patch == null) { + return null; + } + return Version(major, minor, patch); + } + @override String toString() => 'Android Studio ($version)'; } diff --git a/packages/flutter_tools/test/general.shard/android/android_studio_test.dart b/packages/flutter_tools/test/general.shard/android/android_studio_test.dart index b703dbf0c03..8a8a6b823e2 100644 --- a/packages/flutter_tools/test/general.shard/android/android_studio_test.dart +++ b/packages/flutter_tools/test/general.shard/android/android_studio_test.dart @@ -131,6 +131,7 @@ void main() { fileSystem.directory(studioInApplicationPlistFolder).parent.path, )!; + expect(studio.version, equals(Version(4, 1, null))); expect(studio, isNotNull); expect(studio.pluginsPath, equals(fileSystem.path.join( homeMac, @@ -173,6 +174,7 @@ void main() { fileSystem.directory(studioInApplicationPlistFolder).parent.path, )!; + expect(studio.version, equals(Version(2020, 3, null))); expect(studio, isNotNull); expect(studio.pluginsPath, equals(fileSystem.path.join( homeMac, @@ -215,6 +217,7 @@ void main() { fileSystem.directory(studioInApplicationPlistFolder).parent.path, )!; + expect(studio.version, equals(Version(3, 3, null))); expect(studio, isNotNull); expect(studio.pluginsPath, equals(fileSystem.path.join( homeMac, @@ -256,11 +259,13 @@ void main() { fileSystem.directory(studioInApplicationPlistFolder).parent.path, )!; + expect(studio.version, equals(Version(2022, 3, 1))); expect(studio, isNotNull); expect(studio.pluginsPath, equals(fileSystem.path.join( homeMac, 'Library', 'Application Support', + 'Google', 'AndroidStudioPreview2022.3', ))); expect(studio.validationMessages, ['Java version 123']);