4

My appllication implements an auto upgrade capability as the app will not be release to market. The apk file is hosted on a webserver. I have implemented part one of the following link

Use AsyncTask and show the download progress in a dialog

Now when I run the application the following things occur.

  1. If a new version is available then the application prompts the user if they want to upgrade. Upgrade prompt

  2. If the user selects yes then the application downloads the file from the server. Download

  3. Once completed the app then prompts the user if they want to replace the app Replace prompt
  4. If the user hits ok then the application starts to install but fails with the following screen enter image description here

There is no log file information provided as to why this occurs. Please could someone help me understand what could be causing this issue. This is default android code to produce the fianl 2 screens not my code.

This occurs after the following code has been run on the asyncronous task:

    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        // dismiss the progress dialog
        mProgressDialog.dismiss();
        // get the file
        String path = getFilesDir().getAbsolutePath()+ File.separator + getString(R.string.apk_file);
        File file = new File(path);
        // if it exists
        if(file.exists()) {
            // create and start the intent with the new file
            Intent intent = new Intent(Intent.ACTION_VIEW); 
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); 
            startActivity(intent);
        } else {
            // display error message 
            Toast.makeText(context, "File was not downloaded", Toast.LENGTH_LONG).show();
        }           
    }

If i uninstall the app and use the browser to go the file location the app is downloaded automatically and then I can install it succesfully from the downloads folder.

Could this be a problem that the app is trying to Install its self while the app is currently running? The file is downloaded to the internal memory during the upgrade process and not the SD card location as I cannot guarentee that the users device will have an SD card present.

Community
  • 1
  • 1
Comic Coder
  • 519
  • 1
  • 9
  • 32
  • 1
    It's possible that the problem is the app is running when you're trying to upgrade it although part of the upgrade process involves the package manager stopping an app that it's upgrading. You could try calling `finish()` immediately after you call `startActivity(...)`. Another possible reason is the current version on the device is `release` code and you're upgrading with `debug` code (or vice versa) - that will cause an upgrade failure. – Squonk Jun 18 '12 at 16:42
  • This looks like a similar issue, and I suspect that there is an issue when the system uninstalls your apk then the package manager tries to access that area http://stackoverflow.com/questions/5443074/android-internal-data-save-getfilesdir – Idistic Jun 18 '12 at 16:46
  • Hi Squonk. I have tried it both on my emulator and on my android phone. Both had the same issue. The android phone is using a signed apk file. – Comic Coder Jun 18 '12 at 16:49
  • Hi Idistic. I am already using `openFileOutput(getString(R.string.apk_file), Context.MODE_WORLD_READABLE);` so this is not the problem. – Comic Coder Jun 18 '12 at 16:54
  • 2
    Ok. Have you tried (just as an experiment) storing your apk on external storage? I know you don't want to do this but it would add another data point since there are no logs. PS use @ in front of a users name or they won't get your response for instance @ idistic (no space between @ and username) – Idistic Jun 18 '12 at 17:11
  • Also have you added the INSTALL_PACKAGES permission to your manifest? – Idistic Jun 18 '12 at 17:12
  • 1
    Do not add the install packages permission, it only works for system apps and is not needed to begin user-confirmed installation via an intent. – Chris Stratton Jun 18 '12 at 21:41
  • @Idistic Thanks for the tip in messaging in the comments section +1 to you :D, Yes I had put the INSTALL_PACKAGES permission already. I finaly got it working last night. I went back and recompiled version 1 and installed it on the device and then imediately installed version 2 and it worked correctly so perhaps the signing process had become corrupted on one of the previous versions i was trying to install. I have removed the INSTALL_PACKAGES permission now following Chris Stratton's comment – Comic Coder Jun 19 '12 at 08:23

1 Answers1

5
  1. Be sure that old and new apks signed with the same key and have the same package name. Otherwise you will see the same error.

  2. Try to call finish to your activity(or just use System.exit) just after startActivity.

Jin35
  • 8,602
  • 3
  • 32
  • 52