27

I'm new to Android dev and I'm almost ready to release a first version of my app :)

While testing the signed release apk on my phone, it refuse to install because the debug version is installed with the debug signature.

So I have to uninstall the debug version but it delete all my database (and it will do it to my friends who are testing it).

Is there a way to manage a debug and a release version of the same app without losing data?

Geob-o-matic
  • 5,203
  • 4
  • 29
  • 38
  • I'd be very interested in a solution for this too. However, I'm fairly certain that short of changing the package names, this won't be possible. :/ – Victor Feb 10 '11 at 17:05

5 Answers5

37

Many Android projects are starting to use the gradle build system (we transitioned to it when we started using Android Studio). Fortunately, gradle makes it really simple to install both a dev and release version simultaneously, each with their own independent data. The Android docs cover this, just add a applicationIdSuffix to your debug build type like so:

android {
    buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }
    }
}
Evan Grim
  • 4,354
  • 3
  • 21
  • 19
  • I'm not that familiar (yet) with gradle, but at least according to what I've read so far, I think this should be the accepted answer, since it's the best solution to the OP's question. – Andre Feb 04 '15 at 17:05
4

I'm not aware of any easy way to do get around the uninstall/reinstall process, so your options include...

  • Buy a second device for testing (some Android devices are very cheap now, especially on eBay)
  • Use the emulator for testing

I see the same issue, but it's to be expected, so I use the phone for debug dev, and the tablet for production testing. When I'm close to a release, I test the production version on both devices and the emulator.

With your testers, I'd advise that you always give them release versions, but you could include extensive logging to help with problems. Debug versions are then only used by you, and release versions by them. If you provide testers with a release version, they use, and accumulate data, when they come to upgrade to the next version, the data can be retained (or updated, if you change the schema) to migrate their data.

I don't see a need for your testers to be using debug & release versions.

Ollie C
  • 27,184
  • 33
  • 125
  • 211
  • it's because I provide a debug menu with some advanced actions only available when the app is signed wish debug signature – Geob-o-matic Feb 10 '11 at 17:44
  • In that case I would suggest you add a boolean flag in your build for whether to include that menu or not, and put it in your production build. Another option is to "hide" it in the UI, perhaps as a long-press on the app logo, something most people won't do, but if you want specific people to have access to it, they can. Then when testing is finished, you do a proper production release and remove it. – Ollie C Feb 10 '11 at 17:55
  • 3
    @Geobert I think it's a bad idea to be sending debug builds to testers, much better to send them production releases. It gives them an experience closer to the end result, e.g. makes sure they're testing a version that's using the production MapView key, for example. Debug builds are for you, production for your testers and end-users – Ollie C Feb 10 '11 at 18:04
3

Thanks @Evan your solution works perfect:

android {
    buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }
    }
}

To append " (DEBUG)" to your app title when running in debug mode, place this code in your Activity's onCreate:

PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
    //The .debug specified in gradle
    if (pInfo.packageName.equals("YOUR_PACKAGE_NAME_HERE.debug")) {
        setTitle(getTitle() + " (DEBUG)");
}
Francois
  • 1,521
  • 2
  • 13
  • 8
2

Why uninstall the app? Normally, installing the new version of the same app (identified by the package ID) retains all the app data.

EDIT: to retain app data by hand, copy it from /data/data/my.package.name/... to a safe place, then restore when necessary.

Seva Alekseyev
  • 55,897
  • 22
  • 151
  • 252
  • 1
    The same app will be overwriting the old one while keeping the data, yes, but you can not install a second app with the same package, but a different signing, as @Geobert has. So that's what happens. – Nanne Feb 10 '11 at 17:10
  • I did not change the package id but it does not install. I've uninstall the debug version then install the release one. Then when I tried to launch in debug mode from eclipse, it says: – Geob-o-matic Feb 10 '11 at 17:11
  • [2011-02-10 18:11:24 - radis] Uploading radis.apk onto device '10006609c6cb' [2011-02-10 18:11:24 - radis] Installing radis.apk... [2011-02-10 18:11:25 - radis] Re-installation failed due to different application signatures. [2011-02-10 18:11:25 - radis] You must perform a full uninstall of the application. WARNING: This will remove the application data! [2011-02-10 18:11:25 - radis] Please execute 'adb uninstall fr.geobert.Radis' in a shell. [2011-02-10 18:11:25 - radis] Launch canceled! – Geob-o-matic Feb 10 '11 at 17:12
  • Oh, debug and release at the same time on the same device? Possible, but only if they introduce different package IDs. Android identifies apps by their package ID. – Seva Alekseyev Feb 10 '11 at 17:12
  • Is "package ID" the java style package name? Can we change it according to debug/release mode? – Geob-o-matic Feb 10 '11 at 17:17
  • The one that's listed in the manifest file as . Normally matches the Java package name, but does not have to. – Seva Alekseyev Feb 10 '11 at 17:18
  • Isn't the `data` folder protected? Don't you need root to access it? – John Feb 10 '11 at 17:21
  • Not sure... My Android users are giving me conflicting reports. Probably varies by device (carrier?) and OS version. – Seva Alekseyev Feb 10 '11 at 17:25
  • 1
    I think this is a bad solution - you should send production APKs to your testers and customers, otherwise they could give feedback, everything is ok, then you release the production build and you see problems. Your testers should be testing a version as close as possible to that you will finally release. – Ollie C Feb 10 '11 at 18:06
  • 1
    I kinda agree; in my case, the beta was signed by prod key, and the debugging features were well-hidden (activated by a cheat code in one of the input fields). Just answering the question. – Seva Alekseyev Feb 10 '11 at 18:07
1

For me, I also needed to add:

<permission                                                                               
      android:name="${applicationId}.permission.C2D_MESSAGE"                                
      android:protectionLevel="signature" />                                                

<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" /> 

Otherwise, both would receive the same C2D_MESSAGE permission which resulted in:

Failure [INSTALL_FAILED_DUPLICATE_PERMISSION perm=<your applicationId>.permission.C2D_MESSAGE pkg=<your applicationId>] 
peuhse
  • 1,430
  • 2
  • 18
  • 29