unlinkKotlinLoadLibraries function
Removes System.loadLibrary("lib") for libs from the Kotlin plugin: an
earlier link loaded every module eagerly (see androidEagerLibs).
Implementation
void unlinkKotlinLoadLibraries(List<String> libs, {String baseDir = '.'}) {
if (libs.isEmpty) return;
final pluginFile = _kotlinPluginFile(baseDir);
if (pluginFile == null) return;
final content = pluginFile.readAsStringSync();
var next = content;
for (final lib in libs) {
final call = r'System\.loadLibrary\("' + RegExp.escape(lib) + r'"\)';
final name = RegExp.escape(lib);
// A call on its own line goes with its line and a comment right above it
// that names the library; one sharing a line (`init { … }`) goes alone.
final others = [
for (final m in RegExp(r'System\.loadLibrary\("([^"]+)"\)').allMatches(next))
if (m[1] != lib) m[1]!,
];
next = next
.replaceAllMapped(RegExp(r'^[ \t]*//[^\n]*\b' + name + r'\b[^\n]*\r?\n(?=[ \t]*' + call + r'[ \t]*\r?\n)', multiLine: true),
// A comment that also describes another library stays.
(c) => others.any((o) => RegExp(r'\b' + RegExp.escape(o) + r'\b').hasMatch(c[0]!)) ? c[0]! : '')
.replaceAll(RegExp(r'^[ \t]*' + call + r'[ \t]*\r?\n', multiLine: true), '')
.replaceAll(RegExp(call + r'[ \t]*(\r?\n[ \t]*)?'), '');
}
if (next != content) pluginFile.writeAsStringSync(next);
}