Last active
April 19, 2022 00:53
-
-
Save vrdriver/efac466e64dfd23deb30e69cd5ffe072 to your computer and use it in GitHub Desktop.
An after sync script for 'npx cap sync' to fix up android:label="@string/activity_name" missing for AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Modifed by S. Monro 20220418 | |
configCordovaPlugin.js | |
This gist was published after I wrote this post: | |
https://forum.ionicframework.com/t/android-build-fails-string-activity-name-not-found/200866/7?u=stephenmonro | |
In short, you can find problems that after using Ionic with Cordova and adding Capacitor as well, there's probably | |
going to be problems with compilation. | |
For me, the first problem was an error complaining about a string missing in AndroidManifest.xml | |
Specifically this: android:label="@string/activity_name" | |
While I could have just opened that file manually every time and replaced @string/activity_name with my App name, | |
this was going to be time consuming and painful. Fortunately, the Ionic team addressed this: | |
Original script found here: https://ionic.zendesk.com/hc/en-us/articles/4435520090519-Configuring-Cordova-Plugins-for-Capacitor-Apps-Built-with-Appflow | |
While that was a great starting point, I needed it to be a little different, because it was set up to just replace | |
an AndroidManifest.xml file with another one that was pre-compiled already. The trouble is, what happens if the 'system' | |
adds extra features that you don't know about, or need? | |
So, I made this script, which replaces strings. | |
There's scope for other things in the future, but this is a start. | |
How to use: | |
Save this file to where you want. I've used the filename "configCordovaPlugin.js" and stored it in the root path of | |
my Ionic project. | |
Next, Modify your package.json to include the last line in the scripts. | |
With that additional line, it now means that when you run 'npx cap sync', that after that, it will execute this script. | |
... | |
"scripts": { | |
"ng": "ng", | |
"start": "ng serve", | |
"build": "ng build", | |
"test": "ng test", | |
"lint": "ng lint", | |
"e2e": "ng e2e", | |
"capacitor:sync:after": "node ./configCordovaPlugin.js" // Add this line. | |
}, | |
... | |
Once you've done that, go to the bottom of this file to adjust what needs to be changed. | |
You can either replace strings dynamically from this script, or just copy over pre-built settings files. | |
This is designed to be used with with an 'npx cap sync' | |
You will notice there are extra and unused config lines of code have been left in this file deliberately. | |
*/ | |
// | |
// NO NEED TO CHANGE THE FUNCTIONS. CONTINUE ON... | |
// | |
fs = require('fs'); | |
function overwriteFile(src, dest, fileName, platform) { | |
try { | |
var data = fs.readFileSync(src, 'utf8'); | |
} catch (err) { | |
console.error(err); | |
} | |
try { | |
fs.writeFileSync(dest, data); | |
console.log(`${fileName} file written successfully to ${platform} project`); | |
} catch (err) { | |
console.error(err); | |
} | |
} | |
function replaceStringInFile(src, whatstring, correctstring) | |
{ | |
try | |
{ | |
var data = fs.readFileSync(src, 'utf8'); | |
} | |
catch (err) { | |
console.error(err); | |
} | |
try | |
{ | |
var result = data.replace(whatstring, correctstring); | |
fs.writeFile(AndriodmanifestFileDest, result, 'utf8', function (err) { | |
if (err) return console.log(err); | |
}); | |
console.log(`${src} file string updateded correctly for string replacement.`); | |
} catch (err) | |
{ | |
console.error(err); | |
} | |
} | |
// END FUNCTIONS | |
/* ************************* SETTINGS AND CONFIGS ****************** */ | |
let appName = 'Your App Name'; | |
/* Other known filename settings if required */ | |
//let stringsFileSrc = "./cordovaConfig/strings.xml"; | |
//let stringsFileDest = "./android/app/src/main/res/values/strings.xml"; | |
/* Required if you are copying settings. */ | |
let AndriodmanifestFileSrc = "./cordovaConfig/AndroidManifest.xml"; | |
/* The end file that needs replacing. */ | |
let AndriodmanifestFileDest = "./android/capacitor-cordova-android-plugins/src/main/AndroidManifest.xml"; | |
console.log("*** Android build:"); | |
/* Fuction: Replace a string: | |
@string/activity_name | |
In this example, we are just replacing a string, listed for the original problem - as mentioned at | |
the top of this page. */ | |
//replaceStringInFile(AndriodmanifestFileDest, '@string/activity_name', appName); | |
// Or, for less configuration, which is what the original problem was about. | |
replaceStringInFile(AndriodmanifestFileDest, '@string/activity_name', '@string/app_name'); | |
/* Fuction: Replace the entire file. | |
Replace AndroidManifest.xml */ | |
//overwriteFile(AndriodmanifestFileSrc, AndriodmanifestFileDest, "AndroidManifest.xml", "android"); | |
/* Fuction: Replace the entire file. | |
Replace strings.xml */ | |
//overwriteFile(stringsFileSrc, stringsFileDest, "strings.xml", "android"); | |
// Apple stuff if required. | |
// console.log("*** iOS build:"); | |
// Filename definitions: | |
//let plistFileSrc = "./cordovaConfig/info.plist"; | |
//let plistFileDest = "./ios/App/App/info.plist"; | |
/* Fuction: Replace the entire file. | |
Replace info.plist */ | |
//overwriteFile(stringsFileSrc, plistFileDest, "info.plist", "iOS"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment