I am updating the dojo libraries in a Cordova mobile app. Everything works correctly when running the updated application in the browser (using Cordova-simulate). When running my application in an emulator or on the device, I get an error like:
Unable to open asset URL: file:///android_asset/www/scripts/lib/3.32/dojo/_base/Url.js```
When I examine my .apk file, the error makes sense, because the _base and _firebug folders have not been copied into the apk.
The _base and_firebug folders are properly copied to the platforms folder during the application build process, but are not being included into the final APK. As near as I can tell, the problem is these folder names begin with an underscore character.
I renamed _base and _firebug, to base and firebug. AFter building again, I was able to confirm that they were included in the APK. This seems to confirm that the underscore is what is preventing the directories from being included.
I found this 2014 post dojo/_base/*js missing from worklight apk in wl 6.1 which is the same basic issue. And I tried the suggested fix, but Android rejected my apk at install time with the error below. The post is 6 years old, so obviously something has changed, or I'm doing something completely wrong. Probably both.
adb: failed to install app-debug.apk: Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES: Failed to collect certificates from /data/app/vmdl633061898.tmp/base.apk: META-INF/CERT.SF indicates /data/app/vmdl633061898.tmp/base.apk is signed using APK Signature Scheme v2, but no such signature was found. Signature stripped?]
Ideally, I would like to specify some command line option (or build-extras.gradle option, or whatever) to force the build process to include the _base and _firebug folders.
Alternately, if someone has a link to clear instructions on how to unzip, modify, rezip, sign and align my APK, that would be super helpful also.
Any suggestions will be greatly appreciated.
Thanks in advance.
This isn't the perfect (easiest) solution I would have liked, but it worked for me. Maybe it will help others who hit this same problem with the dojo/_base library.
TL;DR I added a before_compile hook into the build process that manually copied the _base folder into place.
To accomplish this, I first used npm to install fs-extra into my project.
npm i fs-extra
Next, I added a ./build_modifications/before_compile/ folder into my project root folder, and created the file beforeCordovaCompile.js.
#!/usr/bin/env node
// required node modules
var fs = require('fs-extra');
module.exports = function(context) {
var androidFrom = 'C:\\Users\\username\\git_repos\\projectfolder\\platforms\\android\\app\\src\\main\\assets\\www\\scripts\\lib\\3.32\\dojo\\_base';
var androidTo = 'C:\\Users\\username\\git_repos\\projectfolder\\platforms\\android\\app\\build\\intermediates\\merged_assets\\debug\\out\\www\\scripts\\lib\\3.32\\dojo\\_base';
// function that copies resource files to chosen platform
function filesToCopy(fromFolder, toFolder) {
console.log('copying ' + fromFolder + ' to ' + toFolder);
try {
fs.copySync(fromFolder, toFolder);
console.log('success!');
} catch (err) {
console.error(err);
}
}
filesToCopy(androidFrom, androidTo);
};
Finally, I added the hook element to my config.xml. Where "." equals my project folder root.
<hook type="before_compile" src="./build_modifications/before_compile/beforeCordovaCompile.js" />
I tested by performing the build and it included the _base folder into the merged_assets folder and into my apk.
cordova build android --debug --buildConfig=C:/Users/username/git_repos/projectfolderDevSupport/signing/build.json
Note 1: When I first checked my apk I was super disappointed because I didn't see the _base folder. Eventually I realized my zip utility was sorting differently than windows and the _base folder was listed after all the other library folders instead of listed first.
Note 2: After a platform remove/add, I was frustrated to have this problem re-occur. I don't know why, but for some reason cordova run is not triggering the hook in the same way that cordova build does. My solution for this, was to do a build first, and then do the run. It appears that, once the _base folder is included into the apk, the folder doesn't get deleted unless you delete the apk.