Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 57fd50f

Browse files
authored
Fix unable to find bundled Java version (#119244)
1 parent 909dc30 commit 57fd50f

File tree

3 files changed

+182
-6
lines changed

3 files changed

+182
-6
lines changed

AUTHORS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,5 @@ Jingyi Chen <[email protected]>
100100
Junhua Lin <[email protected]>
101101
Tomasz Gucio <[email protected]>
102102
Jason C.H <[email protected]>
103-
Hubert Jóźwiak <[email protected]>
103+
Hubert Jóźwiak <[email protected]>
104+
Eli Albert <[email protected]>

packages/flutter_tools/lib/src/android/android_studio.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,22 @@ class AndroidStudio implements Comparable<AndroidStudio> {
441441
return;
442442
}
443443

444-
final String javaPath = globals.platform.isMacOS ?
445-
version != null && version.major < 2020 ?
446-
globals.fs.path.join(directory, 'jre', 'jdk', 'Contents', 'Home') :
447-
globals.fs.path.join(directory, 'jre', 'Contents', 'Home') :
448-
globals.fs.path.join(directory, 'jre');
444+
final String javaPath;
445+
if (globals.platform.isMacOS) {
446+
if (version != null && version.major < 2020) {
447+
javaPath = globals.fs.path.join(directory, 'jre', 'jdk', 'Contents', 'Home');
448+
} else if (version != null && version.major == 2022) {
449+
javaPath = globals.fs.path.join(directory, 'jbr', 'Contents', 'Home');
450+
} else {
451+
javaPath = globals.fs.path.join(directory, 'jre', 'Contents', 'Home');
452+
}
453+
} else {
454+
if (version != null && version.major == 2022) {
455+
javaPath = globals.fs.path.join(directory, 'jbr');
456+
} else {
457+
javaPath = globals.fs.path.join(directory, 'jre');
458+
}
459+
}
449460
final String javaExecutable = globals.fs.path.join(javaPath, 'bin', 'java');
450461
if (!globals.processManager.canRun(javaExecutable)) {
451462
_validationMessages.add('Unable to find bundled Java version.');

packages/flutter_tools/test/general.shard/android/android_studio_test.dart

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ const Map<String, Object> macStudioInfoPlist2020_3 = <String, Object>{
5656
},
5757
};
5858

59+
const Map<String, Object> macStudioInfoPlist2022_1 = <String, Object>{
60+
'CFBundleGetInfoString': 'Android Studio 2022.1, build AI-221.6008.13.2211.9477386. Copyright JetBrains s.r.o., (c) 2000-2023',
61+
'CFBundleShortVersionString': '2022.1',
62+
'CFBundleVersion': 'AI-221.6008.13.2211.9477386',
63+
'JVMOptions': <String, Object>{
64+
'Properties': <String, Object>{
65+
'idea.vendor.name' : 'Google',
66+
'idea.paths.selector': 'AndroidStudio2022.1',
67+
'idea.platform.prefix': 'AndroidStudio',
68+
},
69+
},
70+
};
71+
5972
const Map<String, Object> macStudioInfoPlistEAP = <String, Object>{
6073
'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',
6174
'CFBundleShortVersionString': 'EAP AI-212.5712.43.2112.8233820',
@@ -486,6 +499,84 @@ void main() {
486499
Platform: () => platform,
487500
PlistParser: () => plistUtils,
488501
});
502+
503+
testUsingContext('Can find Android Studio 2020.3 bundled Java version on Mac', () {
504+
final String studioInApplicationPlistFolder = globals.fs.path.join(
505+
'/',
506+
'Application',
507+
'Android Studio.app',
508+
'Contents',
509+
);
510+
globals.fs.directory(studioInApplicationPlistFolder).createSync(recursive: true);
511+
512+
final String plistFilePath = globals.fs.path.join(studioInApplicationPlistFolder, 'Info.plist');
513+
plistUtils.fileContents[plistFilePath] = macStudioInfoPlist2020_3;
514+
processManager.addCommand(FakeCommand(
515+
command: <String>[
516+
globals.fs.path.join(studioInApplicationPlistFolder, 'jre', 'Contents', 'Home', 'bin', 'java'),
517+
'-version',
518+
],
519+
stderr: '123',
520+
)
521+
);
522+
final AndroidStudio studio = AndroidStudio.fromMacOSBundle(
523+
globals.fs.directory(studioInApplicationPlistFolder).parent.path,
524+
)!;
525+
526+
expect(studio.javaPath, equals(globals.fs.path.join(
527+
studioInApplicationPlistFolder,
528+
'jre',
529+
'Contents',
530+
'Home',
531+
)));
532+
}, overrides: <Type, Generator>{
533+
FileSystem: () => fileSystem,
534+
FileSystemUtils: () => fsUtils,
535+
ProcessManager: () => processManager,
536+
// Custom home paths are not supported on macOS nor Windows yet,
537+
// so we force the platform to fake Linux here.
538+
Platform: () => platform,
539+
PlistParser: () => plistUtils,
540+
});
541+
542+
testUsingContext('Can find Android Studio 2022.1 bundled Java version on Mac', () {
543+
final String studioInApplicationPlistFolder = globals.fs.path.join(
544+
'/',
545+
'Application',
546+
'Android Studio.app',
547+
'Contents',
548+
);
549+
globals.fs.directory(studioInApplicationPlistFolder).createSync(recursive: true);
550+
551+
final String plistFilePath = globals.fs.path.join(studioInApplicationPlistFolder, 'Info.plist');
552+
plistUtils.fileContents[plistFilePath] = macStudioInfoPlist2022_1;
553+
processManager.addCommand(FakeCommand(
554+
command: <String>[
555+
globals.fs.path.join(studioInApplicationPlistFolder, 'jbr', 'Contents', 'Home', 'bin', 'java'),
556+
'-version',
557+
],
558+
stderr: '123',
559+
)
560+
);
561+
final AndroidStudio studio = AndroidStudio.fromMacOSBundle(
562+
globals.fs.directory(studioInApplicationPlistFolder).parent.path,
563+
)!;
564+
565+
expect(studio.javaPath, equals(globals.fs.path.join(
566+
studioInApplicationPlistFolder,
567+
'jbr',
568+
'Contents',
569+
'Home',
570+
)));
571+
}, overrides: <Type, Generator>{
572+
FileSystem: () => fileSystem,
573+
FileSystemUtils: () => fsUtils,
574+
ProcessManager: () => processManager,
575+
// Custom home paths are not supported on macOS nor Windows yet,
576+
// so we force the platform to fake Linux here.
577+
Platform: () => platform,
578+
PlistParser: () => plistUtils,
579+
});
489580
});
490581

491582
late FileSystem windowsFileSystem;
@@ -596,6 +687,38 @@ void main() {
596687
ProcessManager: () => FakeProcessManager.any(),
597688
});
598689

690+
testUsingContext('Can find Android Studio 2020.3 bundled Java version on Windows', () {
691+
windowsFileSystem.file(r'C:\Users\Dash\AppData\Local\Google\AndroidStudio2020.3\.home')
692+
..createSync(recursive: true)
693+
..writeAsStringSync(r'C:\Program Files\AndroidStudio');
694+
windowsFileSystem.directory(r'C:\Program Files\AndroidStudio')
695+
.createSync(recursive: true);
696+
697+
final AndroidStudio studio = AndroidStudio.allInstalled().single;
698+
699+
expect(studio.javaPath, equals(r'C:\Program Files\AndroidStudio\jre'));
700+
}, overrides: <Type, Generator>{
701+
Platform: () => windowsPlatform,
702+
FileSystem: () => windowsFileSystem,
703+
ProcessManager: () => FakeProcessManager.any(),
704+
});
705+
706+
testUsingContext('Can find Android Studio 2022.1 bundled Java version on Windows', () {
707+
windowsFileSystem.file(r'C:\Users\Dash\AppData\Local\Google\AndroidStudio2022.1\.home')
708+
..createSync(recursive: true)
709+
..writeAsStringSync(r'C:\Program Files\AndroidStudio');
710+
windowsFileSystem.directory(r'C:\Program Files\AndroidStudio')
711+
.createSync(recursive: true);
712+
713+
final AndroidStudio studio = AndroidStudio.allInstalled().single;
714+
715+
expect(studio.javaPath, equals(r'C:\Program Files\AndroidStudio\jbr'));
716+
}, overrides: <Type, Generator>{
717+
Platform: () => windowsPlatform,
718+
FileSystem: () => windowsFileSystem,
719+
ProcessManager: () => FakeProcessManager.any(),
720+
});
721+
599722
group('Installation detection on Linux', () {
600723
late FileSystemUtils fsUtils;
601724

@@ -686,6 +809,47 @@ void main() {
686809
Platform: () => linuxPlatform,
687810
ProcessManager: () => FakeProcessManager.any(),
688811
});
812+
813+
testUsingContext('Can find Android Studio 2020.3 bundled Java version on Linux', () {
814+
const String studioHomeFilePath = '$homeLinux/.cache/Google/AndroidStudio2020.3/.home';
815+
const String studioInstallPath = '$homeLinux/AndroidStudio';
816+
817+
globals.fs.file(studioHomeFilePath)
818+
..createSync(recursive: true)
819+
..writeAsStringSync(studioInstallPath);
820+
821+
globals.fs.directory(studioInstallPath).createSync();
822+
823+
final AndroidStudio studio = AndroidStudio.allInstalled().single;
824+
825+
expect(studio.javaPath, equals('$studioInstallPath/jre'));
826+
}, overrides: <Type, Generator>{
827+
FileSystem: () => fileSystem,
828+
FileSystemUtils: () => fsUtils,
829+
Platform: () => linuxPlatform,
830+
ProcessManager: () => FakeProcessManager.any(),
831+
});
832+
833+
testUsingContext('Can find Android Studio 2022.1 bundled Java version on Linux', () {
834+
const String studioHomeFilePath =
835+
'$homeLinux/.cache/Google/AndroidStudio2022.1/.home';
836+
const String studioInstallPath = '$homeLinux/AndroidStudio';
837+
838+
globals.fs.file(studioHomeFilePath)
839+
..createSync(recursive: true)
840+
..writeAsStringSync(studioInstallPath);
841+
842+
globals.fs.directory(studioInstallPath).createSync();
843+
844+
final AndroidStudio studio = AndroidStudio.allInstalled().single;
845+
846+
expect(studio.javaPath, equals('$studioInstallPath/jbr'));
847+
}, overrides: <Type, Generator>{
848+
FileSystem: () => fileSystem,
849+
FileSystemUtils: () => fsUtils,
850+
Platform: () => linuxPlatform,
851+
ProcessManager: () => FakeProcessManager.any(),
852+
});
689853
});
690854
}
691855

0 commit comments

Comments
 (0)