[tools] allow explicitly specifying the JDK to use via a new config setting (#128264)
Closes https://github.com/flutter/flutter/issues/106416.
This PR adds a new `flutter config` setting named `jdk-dir`. When set, the tool will use the JDK found at this location for all Java-dependent tool operations such as building Android apps via gradle and running Android SDK tools.
diff --git a/packages/flutter_tools/lib/src/android/java.dart b/packages/flutter_tools/lib/src/android/java.dart
index 5304dd2..699fcfd 100644
--- a/packages/flutter_tools/lib/src/android/java.dart
+++ b/packages/flutter_tools/lib/src/android/java.dart
@@ -4,6 +4,7 @@
import 'package:process/process.dart';
+import '../base/config.dart';
import '../base/file_system.dart';
import '../base/logger.dart';
import '../base/os.dart';
@@ -52,6 +53,7 @@
///
/// Returns null if no java binary could be found.
static Java? find({
+ required Config config,
required AndroidStudio? androidStudio,
required Logger logger,
required FileSystem fileSystem,
@@ -65,6 +67,7 @@
processManager: processManager
);
final String? home = _findJavaHome(
+ config: config,
logger: logger,
androidStudio: androidStudio,
platform: platform
@@ -181,10 +184,16 @@
}
String? _findJavaHome({
+ required Config config,
required Logger logger,
required AndroidStudio? androidStudio,
required Platform platform,
}) {
+ final Object? configured = config.getValue('jdk-dir');
+ if (configured != null) {
+ return configured as String;
+ }
+
final String? androidStudioJavaPath = androidStudio?.javaPath;
if (androidStudioJavaPath != null) {
return androidStudioJavaPath;