Skip to content

Commit

Permalink
A silly code to workaround issue apache/cordova-android#1178
Browse files Browse the repository at this point in the history
  • Loading branch information
almothafar committed Apr 7, 2021
1 parent ddb8012 commit 626ff04
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions example-app/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<preference name="WKSuspendInBackground" value="false" />
<preference name="WKWebViewOnly" value="true" />
<platform name="android">
<hook type="before_build" src="scripts/appAndroidBeforeBuild.js" />
<preference name="AndroidXEnabled" value="true" />
<preference name="android-minSdkVersion" value="22" />
<preference name="android-targetSdkVersion" value="30" />
Expand Down
60 changes: 60 additions & 0 deletions example-app/scripts/appAndroidBeforeBuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const fs = require('fs');
const path = require('path');

module.exports = (context) => {
// Make sure android platform is part of build
if (!context.opts.platforms.includes('android')) {
return;
}

const platformRoot = path.join(context.opts.projectRoot, 'platforms/android');
const buildGradleFile = path.join(platformRoot, 'build.gradle');
const projectPropertiesFile = path.join(platformRoot, 'project.properties');
const cordovaProjectPropertiesFile = path.join(platformRoot, 'CordovaLib/project.properties');

fs.readFile(buildGradleFile, 'utf8', function (error, data) {
if (error) {
return console.error(error);
}
const result = data
.replace(/defaultBuildToolsVersion="29.0.2"/g, 'defaultBuildToolsVersion="30.0.3"')
.replace(/defaultTargetSdkVersion=29/g, 'defaultTargetSdkVersion=30')
.replace(/defaultCompileSdkVersion=29/g, 'defaultCompileSdkVersion=30');

fs.writeFile(buildGradleFile, result, 'utf8', (error) => {
if (error) {
return console.error(error);
}
});
});

fs.readFile(projectPropertiesFile, 'utf8', (error, data) => {
if (error) {
return console.error(error);
}
const result = data
.replace(/target=android-29/g, 'target=android-30');

fs.writeFile(projectPropertiesFile, result, 'utf8', (error) => {
if (error) {
return console.error(error);
}
});
});

fs.readFile(cordovaProjectPropertiesFile, 'utf8', (error, data) => {
if (error) {
return console.error(error);
}
const result = data
.replace(/target=android-29/g, 'target=android-30');

fs.writeFile(cordovaProjectPropertiesFile, result, 'utf8', (error) => {
if (error) {
return console.error(error);
}
});
});

return Promise.resolve().then(() => console.log('Modifying build.gradle and project.properties is done to target SDK 30'))
};

0 comments on commit 626ff04

Please sign in to comment.