Skip to content

Commit

Permalink
fix(android): compatibility with cordova kotlin plugins (#5438)
Browse files Browse the repository at this point in the history
  • Loading branch information
IT-MikeS committed Feb 21, 2022
1 parent bc7b49f commit 55bf004
Showing 1 changed file with 63 additions and 5 deletions.
68 changes: 63 additions & 5 deletions cli/src/android/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,13 @@ export async function handleCordovaPluginsGradle(
config.android.cordovaPluginsDirAbs,
'build.gradle',
);
const kotlinNeeded = await kotlinNeededCheck(config, cordovaPlugins);
const isKotlinVersionInVariablesGradle = (
await getVariablesGradleFile(config)
).includes('kotlin_version');
const kotlinVersionString =
config.app.extConfig.cordova?.preferences?.GradlePluginKotlinVersion ??
'1.4.32';
const frameworksArray: any[] = [];
let prefsArray: any[] = [];
const applyArray: any[] = [];
Expand Down Expand Up @@ -328,6 +335,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 +349,28 @@ 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:` +
(isKotlinVersionInVariablesGradle
? '$kotlin_version'
: kotlinVersionString) +
`"\n$8`,
);
buildGradle = buildGradle.replace(
/(ext\s{)/,
`$1\n kotlin_version = project.hasProperty('kotlin_version') ? rootProject.ext.kotlin_version : '${kotlinVersionString}'\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 +385,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 Expand Up @@ -421,11 +474,7 @@ async function getPluginsTask(config: Config) {
});
}

async function replaceFrameworkVariables(
config: Config,
prefsArray: any[],
frameworkString: string,
) {
async function getVariablesGradleFile(config: Config) {
const variablesFile = resolve(
config.android.platformDirAbs,
'variables.gradle',
Expand All @@ -434,6 +483,15 @@ async function replaceFrameworkVariables(
if (await pathExists(variablesFile)) {
variablesGradle = await readFile(variablesFile, { encoding: 'utf-8' });
}
return variablesGradle;
}

async function replaceFrameworkVariables(
config: Config,
prefsArray: any[],
frameworkString: string,
) {
const variablesGradle = await getVariablesGradleFile(config);
prefsArray.map((preference: any) => {
if (!variablesGradle.includes(preference.$.name)) {
frameworkString = frameworkString.replace(
Expand Down

0 comments on commit 55bf004

Please sign in to comment.