Arthur Breton Arthur Breton - 1 year ago 170
Android Question

meteor cordova in-app apk update silently fails

I am trying to implement an autoupdate functionality to distribute an app outside of the regular markets.
I use a mix of cordova plugins in order to download and execute a newer version of my app.

I can see that I download the apk to external storage, launch it, allow the installation but then nothing happens (it goes back to my original app).
apk update steps

I have a feeling that this is a permission issue, but I cannot see anything in the logs.
If I open the downloaded apk from the filesystem, I can install it without any problems.
This is ONLY when I launch the apk from the cordova app, the installation doesn't work.

Here is the code I use to make the update:

/**
* Begin the download and install of the update for android.
*/
update() {
let _this = this;
let update = this._updateResult.get();

// Check permissions first
let permissions = cordova.plugins.permissions;
let list = [permissions.WRITE_EXTERNAL_STORAGE];

let errorPermissions = () => {
sAlert.error("Cannot update permissions");
};

let successPermissions = () => {
let fileTransfer = new FileTransfer();

// let targetLocation = cordova.file.externalDataDirectory + "app.apk"; // better use externalCacheDirectory
let targetLocation = "file:///storage/emulated/0/Download/"+"app-1.4.1.apk";
console.debug("Begin update in appManager ", update);
console.info("Download file", update.apk);
console.info("Download to ", targetLocation);

let onSuccess = (entry) => {
let fileURL = entry.toURL();
console.debug("download complete!", fileURL);

cordova.plugins.fileOpener2.open(
fileURL,
'application/vnd.android.package-archive',
{
error: function (e) {
console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
_this._updating.set(false);
},
success: function () {
console.log('file opened successfully');
_this._updating.set(false);
}
}
);
}
let onError = (error) => {
_this._updating.set(false);
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("download error code" + error.code);
}
let options = {
chunkedMode: true,
mimeType: "application/vnd.android.package-archive"
};

fileTransfer.download(
encodeURI(update.apk),
targetLocation,
onSuccess,
onError,
options,
true // trustAllHosts
);
fileTransfer.onprogress = (progressEvent) => {
if (progressEvent.lengthComputable) {
let percent = Math.floor((progressEvent.loaded / progressEvent.total) * 100);
_this._updating.set(percent);
} else {
_this._updating.set(true);
}
};
}

permissions.hasPermission(list,
function (status) {
console.debug("permissions status is", status);
if (status.hasPermission) {
successPermissions();
} else {
permissions.requestPermissions(
list,
function (status) {
if (!status.hasPermission) {
errorPermissions();
}
successPermissions();
},
errorPermissions
);
}
}, errorPermissions);
}


I also tried to add other permissions like INSTALL_PACKAGES, DELETE_PACKAGES, RESTART_PACKAGES...

I suppose the only important part of the code should be

cordova.plugins.fileOpener2.open(
fileURL,
'application/vnd.android.package-archive',
{
error: function (e) {
console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
_this._updating.set(false);
},
success: function () {
console.log('file opened successfully');
_this._updating.set(false);
}
}
);


My logs show 'file opened successfully', any help much appreciated :)

Answer Source

After checking the code of the fileopener2 plugins, I found that intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); was commented out, I tried adding the flag and now the update works.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download