From c2dc92ca881d3f71e8db872798362dcaa4eecf4b Mon Sep 17 00:00:00 2001 From: Lau Ching Jun Date: Wed, 13 Apr 2022 22:49:06 -0700 Subject: [PATCH] Default to the newer version path when checking Android Studio Java path (#101862) --- .../lib/src/android/android_studio.dart | 4 +- .../android/android_studio_test.dart | 87 ++++++++++++++++++- 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/packages/flutter_tools/lib/src/android/android_studio.dart b/packages/flutter_tools/lib/src/android/android_studio.dart index 3a1b0629129..0049f781ff9 100644 --- a/packages/flutter_tools/lib/src/android/android_studio.dart +++ b/packages/flutter_tools/lib/src/android/android_studio.dart @@ -445,9 +445,9 @@ class AndroidStudio implements Comparable { } final String javaPath = globals.platform.isMacOS ? - version != null && version.major >= 2020 ? - globals.fs.path.join(directory, 'jre', 'Contents', 'Home') : + version != null && version.major < 2020 ? globals.fs.path.join(directory, 'jre', 'jdk', 'Contents', 'Home') : + globals.fs.path.join(directory, 'jre', 'Contents', 'Home') : globals.fs.path.join(directory, 'jre'); final String javaExecutable = globals.fs.path.join(javaPath, 'bin', 'java'); if (!globals.processManager.canRun(javaExecutable)) { 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 c81f0c8069d..9673129d2f2 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 @@ -58,6 +58,19 @@ const Map macStudioInfoPlist2020_3 = { }, }; +const Map macStudioInfoPlistEAP = { + 'CFBundleGetInfoString': 'Android Studio EAP AI-212.5712.43.2112.8233820, build AI-212.5712.43.2112.8233820. Copyright JetBrains s.r.o., (c) 2000-2022', + 'CFBundleShortVersionString': 'EAP AI-212.5712.43.2112.8233820', + 'CFBundleVersion': 'AI-212.5712.43.2112.8233820', + 'JVMOptions': { + 'Properties': { + 'idea.vendor.name' : 'Google', + 'idea.paths.selector': 'AndroidStudio2021.2', + 'idea.platform.prefix': 'AndroidStudio', + }, + }, +}; + final Platform linuxPlatform = FakePlatform( environment: {'HOME': homeLinux}, ); @@ -135,6 +148,14 @@ void main() { final String plistFilePath = globals.fs.path.join(studioInApplicationPlistFolder, 'Info.plist'); plistUtils.fileContents[plistFilePath] = macStudioInfoPlist4_1; + processManager.addCommand(FakeCommand( + command: [ + globals.fs.path.join(studioInApplicationPlistFolder, 'jre', 'jdk', 'Contents', 'Home', 'bin', 'java'), + '-version', + ], + stderr: '123', + ) + ); final AndroidStudio studio = AndroidStudio.fromMacOSBundle( globals.fs.directory(studioInApplicationPlistFolder)?.parent?.path, ); @@ -147,10 +168,11 @@ void main() { 'Google', 'AndroidStudio4.1', ))); + expect(studio.validationMessages, ['Java version 123']); }, overrides: { FileSystem: () => fileSystem, FileSystemUtils: () => fsUtils, - ProcessManager: () => FakeProcessManager.any(), + ProcessManager: () => processManager, // Custom home paths are not supported on macOS nor Windows yet, // so we force the platform to fake Linux here. Platform: () => platform, @@ -168,6 +190,14 @@ void main() { final String plistFilePath = globals.fs.path.join(studioInApplicationPlistFolder, 'Info.plist'); plistUtils.fileContents[plistFilePath] = macStudioInfoPlist2020_3; + processManager.addCommand(FakeCommand( + command: [ + globals.fs.path.join(studioInApplicationPlistFolder, 'jre', 'Contents', 'Home', 'bin', 'java'), + '-version', + ], + stderr: '123', + ) + ); final AndroidStudio studio = AndroidStudio.fromMacOSBundle( globals.fs.directory(studioInApplicationPlistFolder)?.parent?.path, ); @@ -180,10 +210,11 @@ void main() { 'Google', 'AndroidStudio2020.3', ))); + expect(studio.validationMessages, ['Java version 123']); }, overrides: { FileSystem: () => fileSystem, FileSystemUtils: () => fsUtils, - ProcessManager: () => FakeProcessManager.any(), + ProcessManager: () => processManager, // Custom home paths are not supported on macOS nor Windows yet, // so we force the platform to fake Linux here. Platform: () => platform, @@ -201,6 +232,14 @@ void main() { final String plistFilePath = globals.fs.path.join(studioInApplicationPlistFolder, 'Info.plist'); plistUtils.fileContents[plistFilePath] = macStudioInfoPlist; + processManager.addCommand(FakeCommand( + command: [ + globals.fs.path.join(studioInApplicationPlistFolder, 'jre', 'jdk', 'Contents', 'Home', 'bin', 'java'), + '-version', + ], + stderr: '123', + ) + ); final AndroidStudio studio = AndroidStudio.fromMacOSBundle( globals.fs.directory(studioInApplicationPlistFolder)?.parent?.path, ); @@ -212,10 +251,52 @@ void main() { 'Application Support', 'AndroidStudio3.3', ))); + expect(studio.validationMessages, ['Java version 123']); }, overrides: { FileSystem: () => fileSystem, FileSystemUtils: () => fsUtils, - ProcessManager: () => FakeProcessManager.any(), + ProcessManager: () => processManager, + // Custom home paths are not supported on macOS nor Windows yet, + // so we force the platform to fake Linux here. + Platform: () => platform, + PlistParser: () => plistUtils, + }); + + testUsingContext('Can discover Android Studio EAP location on Mac', () { + final String studioInApplicationPlistFolder = globals.fs.path.join( + '/', + 'Application', + 'Android Studio with suffix.app', + 'Contents', + ); + globals.fs.directory(studioInApplicationPlistFolder).createSync(recursive: true); + + final String plistFilePath = globals.fs.path.join(studioInApplicationPlistFolder, 'Info.plist'); + plistUtils.fileContents[plistFilePath] = macStudioInfoPlistEAP; + processManager.addCommand(FakeCommand( + command: [ + globals.fs.path.join(studioInApplicationPlistFolder, 'jre', 'Contents', 'Home', 'bin', 'java'), + '-version', + ], + stderr: '123', + ) + ); + final AndroidStudio studio = AndroidStudio.fromMacOSBundle( + globals.fs.directory(studioInApplicationPlistFolder)?.parent?.path, + ); + + expect(studio, isNotNull); + expect(studio.pluginsPath, equals(globals.fs.path.join( + homeMac, + 'Library', + 'Application Support', + 'AndroidStudio2021.2', + ))); + expect(studio.validationMessages, ['Java version 123']); + }, overrides: { + FileSystem: () => fileSystem, + FileSystemUtils: () => fsUtils, + ProcessManager: () => processManager, // Custom home paths are not supported on macOS nor Windows yet, // so we force the platform to fake Linux here. Platform: () => platform,