5

When i run my app every time, versionName is incremented in Manifest file. 20389 means that is old version name, 20390 is incremented number.

enter image description here

Project is built successfully like BUILD SUCCESSFUL. But the question is that why Android studio caches previous apk version. Here is what is error:

Target device: lge-nexus_5-061642fd00511249 Uploading file local path: H:\customFolder\app\build\outputs\apk\MyAppName-0.6.200_20383-debug.apk remote path: /data/local/tmp/com.example.app Local path doesn't exist.

As you pay attention when app runs on a device, android studio try to instal 20383 version of apk. It is wrong. Could anyone help me? I've googeled and saw this link.

Here is how i can change manifest file with gradle script:

def updateRevisionNumber () {//autoIncrement Version Name
  def currentVersion = getVersionName();
  def currentRevisionInManifest =           currentVersion.substring(currentVersion.indexOf("_")+1);
  def lastRevision = Integer.parseInt(currentRevisionInManifest) + 1;
  println("currentRevisionInManifest: " + currentRevisionInManifest);
  println("lastRevision: " + lastRevision);

  def oldText = manifestFile.getText();
  def changedText =      oldText.replace(currentRevisionInManifest,lastRevision+"");
  manifestFile.setText(changedText);}
Community
  • 1
  • 1
nAkhmedov
  • 3,522
  • 4
  • 37
  • 72

1 Answers1

3

Instead of editing your manifest file during the build, you can override the version name in the android section of your manifest :

android {
    ...
    defaultConfig {
        versionName someScriptToComputeVersionName()
        ...

And there is at least 2 good reasons to do it that way :

  • the build process won't change your source code (i.e. AndroidManifest.xml will not be modified - at least the AndroidManifest.xml in your editable sources won't be modified)
  • there are good chances that Android Studio will be happy.

Now your problem is to define correctly the function someScriptToComputeVersionName() , that's up to you.

IMO, simply incrementing a number is probably not the best choice and you should probably go for a solution were you build the version name based on VCS revision number.

ben75
  • 29,217
  • 10
  • 88
  • 134
  • AndroidManifest.xml is changing without doubt. Here is _android:versionName="0.6.200_20390"_ – nAkhmedov May 05 '15 at 13:10
  • I didn't say that your AndroidManifest.xml didn't change... but look into \build\intermediates\manifests\... you will see that Android Studio is using another AndroidManifest.xml to build the apk (the result of a merge in a multi-module project, or the result of injection of some configuration from your build.gradle). I don't know when `updateRevisionNumber()` is executed, but I guess that it is too late (i.e. AndroidStudio don't see the change) – ben75 May 05 '15 at 13:25
  • All of them is changed, but a bit strange matter to me is that apk is being installed when i run _installDebug_ via Gradle Tasks. – nAkhmedov May 05 '15 at 13:38