configureEnvironment static method
Configures environment variables to include native library directories.
Sets up the appropriate environment variables based on the operating system to ensure native libraries can be found at runtime.
nativesPath
Path to the directory containing native libraries.
environment
Optional existing environment variables map to extend. If not provided,
the current platform environment will be used.
Returns a map of environment variables with the natives path properly configured.
Implementation
static Map<String, String> configureEnvironment({
required String nativesPath,
Map<String, String>? environment,
}) {
final envVars = Map<String, String>.from(
environment ?? Platform.environment,
);
if (Platform.isWindows) {
final String currentPath = envVars['PATH'] ?? '';
envVars['PATH'] = '$nativesPath;$currentPath';
debugPrint('Added natives directory to PATH: $nativesPath');
debugPrint('Java library path: -Djava.library.path=$nativesPath');
} else {
final String currentLdLibraryPath = envVars['LD_LIBRARY_PATH'] ?? '';
envVars['LD_LIBRARY_PATH'] = '$nativesPath:$currentLdLibraryPath';
if (Platform.isMacOS) {
final String currentDyldLibraryPath =
envVars['DYLD_LIBRARY_PATH'] ?? '';
envVars['DYLD_LIBRARY_PATH'] = '$nativesPath:$currentDyldLibraryPath';
}
debugPrint('Added natives directory to LD_LIBRARY_PATH: $nativesPath');
}
return envVars;
}