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
45 changes: 45 additions & 0 deletions cli/src/android/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ export async function handleCordovaPluginsGradle(
config.android.cordovaPluginsDirAbs,
'build.gradle',
);
const kotlinNeeded = await kotlinNeededCheck(config, cordovaPlugins);
const frameworksArray: any[] = [];
let prefsArray: any[] = [];
const applyArray: any[] = [];
Expand Down Expand Up @@ -328,6 +329,10 @@ export async function handleCordovaPluginsGradle(
prefsArray,
frameworkString,
);
if (kotlinNeeded) {
frameworkString += `\n implementation "androidx.core:core-ktx:$androidxCoreKTXVersion"`;
frameworkString += `\n implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"`;
}
const applyString = applyArray.join('\n');
let buildGradle = await readFile(pluginsGradlePath, { encoding: 'utf-8' });
buildGradle = buildGradle.replace(
Expand All @@ -338,6 +343,26 @@ export async function handleCordovaPluginsGradle(
/(PLUGIN GRADLE EXTENSIONS START)[\s\S]*(\/\/ PLUGIN GRADLE EXTENSIONS END)/,
'$1\n' + applyString.concat('\n') + '$2',
);
if (kotlinNeeded) {
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'\n androidxCoreKTXVersion = project.hasProperty('androidxCoreKTXVersion') ? rootProject.ext.androidxCoreKTXVersion : '1.6.0'`,
);
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',
);
}
await writeFile(pluginsGradlePath, buildGradle);
const cordovaVariables = `// DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN
ext {
Expand All @@ -352,6 +377,26 @@ ext {
);
}

async function kotlinNeededCheck(config: Config, cordovaPlugins: Plugin[]) {
if (
config.app.extConfig.cordova?.preferences?.GradlePluginKotlinEnabled !==
'true'
) {
for (const plugin of cordovaPlugins) {
const androidPlatform = getPluginPlatform(plugin, platform);
const srcFiles = androidPlatform['source-file'];
for (const srcFile of srcFiles) {
if (/^.*\.kt$/.test(srcFile['$'].src)) {
return true;
}
}
}
return false;
} else {
return true;
}
}

async function copyPluginsNativeFiles(
config: Config,
cordovaPlugins: Plugin[],
Expand Down