Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): compatibility with cordova kotlin plugins #5438

Merged
merged 12 commits into from
Feb 21, 2022
59 changes: 59 additions & 0 deletions cli/src/android/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,65 @@ ext {
join(config.android.cordovaPluginsDirAbs, 'cordova.variables.gradle'),
cordovaVariables,
);
await kotlinNeededCheck(config, cordovaPlugins, pluginsGradlePath);
}

async function kotlinNeededCheck(
IT-MikeS marked this conversation as resolved.
Show resolved Hide resolved
config: Config,
cordovaPlugins: Plugin[],
pluginsGradlePath: string,
) {
let kotlinNeeded =
config.app.extConfig.cordova?.preferences?.GradlePluginKotlinEnabled ===
'true';
if (!kotlinNeeded) {
for (const plugin of cordovaPlugins) {
let platforms: any[] = plugin.xml.platform;
IT-MikeS marked this conversation as resolved.
Show resolved Hide resolved
platforms = platforms.filter(p => p['$'].name === 'android');
for (const platform of platforms) {
const srcFiles = platform['source-file'];
for (const srcFile of srcFiles) {
if (/^.*\.kt$/.test(srcFile['$'].src)) {
kotlinNeeded = true;
break;
}
}
if (kotlinNeeded) break;
}
if (kotlinNeeded) break;
}
}

if (kotlinNeeded) {
let buildGradle: string = await readFile(pluginsGradlePath, {
encoding: 'utf-8',
});
buildGradle = buildGradle.replace(
/(buildscript\s{\n(\t|\s{4})repositories\s{\n((\t{2}|\s{8}).+\n)+(\t|\s{4})}\n(\t|\s{4})dependencies\s{\n(\t{2}|\s{8}).+)\n((\t|\s{4})}\n}\n)/,
`$1\n classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32'\n$8`,
);
buildGradle = buildGradle.replace(
/(ext\s{)/,
`$1\n kotlin_version = project.hasProperty('kotlin_version') ? rootProject.ext.kotlin_version : '1.4.32'`,
);
buildGradle = buildGradle.replace(
/(apply\splugin:\s'com\.android\.library')/,
`$1\napply plugin: 'kotlin-android'`,
);
buildGradle = buildGradle.replace(
/(compileOptions\s{\n((\t{2}|\s{8}).+\n)+(\t|\s{4})})\n(})/,
'$1\n' +
` sourceSets {\n main.java.srcDirs += 'src/main/kotlin'\n }\n` +
'$5',
);
buildGradle = buildGradle.replace(
/(dependencies\s{\n((\t|\s{4})implementation.+\n)+)((\t|\s{4})\/\/\sSUB-PROJECT\sDEPENDENCIES\sSTART\n)/,
'$1' +
` implementation 'androidx.core:core-ktx:1.6.0'\n implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"\n` +
'$4',
);
await writeFile(pluginsGradlePath, buildGradle);
}
}

async function copyPluginsNativeFiles(
Expand Down
8 changes: 4 additions & 4 deletions ios/Capacitor/Capacitor/WebViewDelegationHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal class WebViewDelegationHandler: NSObject, WKNavigationDelegate, WKUIDel
// Reset the bridge on each navigation
bridge?.reset()
}

// TODO: remove once Xcode 12 support is dropped
#if compiler(>=5.5)
@available(iOS 15, *)
Expand All @@ -62,9 +62,9 @@ internal class WebViewDelegationHandler: NSObject, WKNavigationDelegate, WKUIDel

@available(iOS 15, *)
func webView(_ webView: WKWebView,
requestDeviceOrientationAndMotionPermissionFor origin: WKSecurityOrigin,
initiatedByFrame frame: WKFrameInfo,
decisionHandler: @escaping (WKPermissionDecision) -> Void) {
requestDeviceOrientationAndMotionPermissionFor origin: WKSecurityOrigin,
IT-MikeS marked this conversation as resolved.
Show resolved Hide resolved
initiatedByFrame frame: WKFrameInfo,
decisionHandler: @escaping (WKPermissionDecision) -> Void) {
decisionHandler(.grant)
}
#endif
Expand Down