Search code examples
androidupgradeparse-error

Android Parse Error during application upgrade


I have been trying to implement an upgrade plan for my Android app which will not be released on the Android market.

The designed application work flow is as follows:

  1. User is validated at login and the main menu activity is launched.
  2. A request is sent from the main menu activity to my website for latest version code value (Stor in an SQL database on the server).
  3. The returned version code value is compared against the version code value of the apk installed on the device.
  4. If the returned version code value is different, then the new apk file which is located on the server is downloaded and installed.

It is also important to make sure that the following tasks have been completed before the upgrade work flow will work.

  1. The new version of the apk must be compiled singed for release with an updated versionCode value
  2. The value stored in the website database for the versionCode value must be updated
  3. The newly compiled apk file must be uploaded to the correct location on the web server

I have implemented the first 3 stages of my work flow but when I try to implement the 4th part I end up getting the "Parse Error: There is an issue parsing the package" error.

Below is the offending code:

protected void upgradeApplication() {
    StringBuilder urlBuilder = new StringBuilder(getString(R.string.website_ip) + "/" + getString(R.string.application_middleware) + "/" + getString(R.string.apk_folder) + "/"); 

    File file = new File(urlBuilder.toString(), getString(R.string.apk_file));

    Intent intent = new Intent(Intent.ACTION_VIEW);

    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");

    startActivity(intent);
}

I have tested that the apk will install correctly by downloading the apk directly though the devices web browser and installing it from the downloads folder. But when I try to install from within the application I get the posted error.

"Parse Error: There is aproblem parsing the package"

Any help would be appreciated


Solution

  • If with this line

    File file = new File(urlBuilder.toString(), getString(R.string.apk_file));
    

    you're trying to download the file by passing the url as an argument, then it won't work. The File constructor cannot download a file.

    You should use an AsyncTask to download the apk, wait before it's done then only you can install it.