How can you get the Manifest Version number from the App's (Layout) XML variables?

android get app version from google play
android check app version programmatically
get version code from google play
android app version check and update
how to get version code from android manifest
get app version from context
how to get app current version in android
get version android java

I would like to have a way to reference the project's manifest version number in the main part of the code. What I have been doing up until now is to link the version number in a String XML file to the manifest (@string/Version). What I would like to do is to do it the other way around, link a string XML variable to the version in the manifest. The reason? I'd like to only have to change the version number in one location, the manifest file. Is there any way to do this? Thanks!

I believe that was already answered here.

String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;

OR

int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;

Version your app, These values are then merged into your app's manifest file during the This is not the version number shown to users; that number is set by the  For example if you use an android submodule in your project and you call it from the code of the submodule it will reference to build config of the submodule which may have different version. Or if you call it from the code of your project you may reference by mistake to build config of your submodule and get the same result.

There is not a way to directly get the version out, but there are two work-arounds that could be done.

  1. The version could be stored in a resource string, and placed into the manifest by:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="com.somepackage"
         android:versionName="@string/version" android:versionCode="20">
    
  2. One could create a custom view, and place it into the XML. The view would use this to assign the name:

    context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    

Either of these solutions would allow for placing the version name in XML. Unfortunately there isn't a nice simple solution, like android.R.string.version or something like that.

<manifest>, The versionCode attribute holds the significant version number used internally. android:installLocation: The default install location for the app. The version name can be any string value you want. The android:versionCode , on the other hand, must be an integer, and newer versions must have higher version codes than do older versions. Android and the Play Store will compare the version code of a new .apk file to the version code of an installed .apk file to determine if the new APK is

You can use the versionName in XML resources, such as activity layouts. First create a string resource in the app/build.gradle with the following snippet in the android node:

applicationVariants.all { variant ->
    variant.resValue "string", "versionName", variant.versionName
}

So the whole build.gradle file contents may look like this:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '24.0.0 rc3'
    defaultConfig {
        applicationId 'com.example.myapplication'
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 17
        versionName '0.2.3'
        jackOptions {
            enabled true
        }
    }
    applicationVariants.all { variant ->
        variant.resValue "string", "versionName", variant.versionName
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
} 

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:design:23.3.0'
    compile 'com.android.support:support-v4:23.3.0'
}

Then you can use @string/versionName in the XML. Android Studio will mark it red, but the app will compile without issues. For example, this may be used like this in app/src/main/res/xml/preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="About"
        android:key="pref_key_about">

        <Preference
            android:key="pref_about_build"
            android:title="Build version"
            android:summary="@string/versionName" />

    </PreferenceCategory>


</PreferenceScreen>

How to Read Version Number and Other Details From Java Manifest , Read this post on reading version numbers and other details from the app. A brief breakdown on running a Kotlin Spring Boot application  Manifest Version Applications are simply bundles of resources, wrapped up with a manifest.json file that describes the package's contents. The format of this file is generally stable, but occasionally breaking changes must be made to address important issues.

I solved this issue by extending the Preference class.

package com.example.android;

import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;

public class VersionPreference extends Preference {
    public VersionPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        String versionName;
        final PackageManager packageManager = context.getPackageManager();
        if (packageManager != null) {
            try {
                PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
                versionName = packageInfo.versionName;
            } catch (PackageManager.NameNotFoundException e) {
                versionName = null;
            }
            setSummary(versionName);
        }
    }
}

Then in my preferences XML:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <com.example.android.VersionPreference android:title="Version" />
</PreferenceScreen>

Manifest - Version, Contents#. Version Name. Important: Chrome will be removing support for Chrome Apps on all platforms. Chrome browser and the Chrome Web Store will  The following illustration shows the different ways the manifest can be stored. For an assembly with one associated file, the manifest is incorporated into the PE file to form a single-file assembly. You can create a multifile assembly with a standalone manifest file or with the manifest incorporated into one of the PE files in the assembly.

I use BuildConfig.VERSION_NAME.toString();. What's the difference between that and getting it from the packageManager?

No XML based solutions have worked for me, sorry.

Build numbering and app versioning, gradle file or the AndroidManifest.xml file for Android apps). Bitrise has two steps to do this for you. These two Steps can insert the Bitrise build number or some  Additionally, the version number of Windows 8.1 packages must always be greater than the version numbers of any of your Windows 8 packages for the same app. In other words, the version number of any Windows 8 package that you submit must be lower than the version number of any Windows 8.1 package that you've submitted for the same app.

Manage your Android app's versionCode & versionName with Gradle, be doing manually. As you know, every Android app must declare… Define individual components of the version number. Start off with build/intermediates/​full/debug/AndroidManifest.xml:<manifest xmlns:android=”…" The name of an application manifest file is the name of the application's executable followed by .manifest. For example, an application manifest that refers to example.exe or example.dll would use the following file name syntax. You can omit the < resource ID > field if resource ID is 1. example.exe.<resource ID>.manifest.

Beginning Mobile Application Development in the Cloud, For the vast majority of apps, obtaining a list of files is all you'll ever need to do. Using a comment line in the manifest file containing a version number that you  There are two parts you need: android:versionCode android:versionName. versionCode is a number, and every version of the app you submit to the Market needs to have a higher number then the last. VersionName is a string, and can be anything you want it to be.

Package version numbering, The Microsoft Store enforces certain rules related to version numbers, Version attribute of the Package/Identity element in the app manifest). While I can get the assembly version using the following code. var assembly = typeof(App).GetTypeInfo().Assembly; var assemblyVersion = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version; I would like to retrieve the Version from Package.appxmanifest in this case 1.0.0.4

Comments
  • This is NOT a duplicate. The question in question asks how to do the same thing in CODE, I'm asking in XML. Two very different constructs in Android programming...
  • Almost, but not quite... What I'd like to do is to reference it to an XML code, but it looks like that might not be possible, so...
  • Thank you so much!!! I have been looking everywhere for this: String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
  • @Shubh I don't think so.
  • Add BuildConfig.VERSION_NAME to your answer for gradle fokes.
  • a little note to 1.: it's working but I think it's discouraged since I get a warning telling me The android:versionName cannot be a resource url, it must be a literal string
  • What I did is leave the manifest version name untouched (but update it whenever a new release) and use the string.xml to store the value that I'll be using across the app.
  • For AndroidStudio, the manifest versionName is override by the one set in build.gradle file
  • @DenisGL michiganlabs.com/…
  • This is better, than programmatic style.
  • Wonderful solution. Thanks a ton.
  • I am using the latest and still works for me. I use this and several other variables like this. @k2col what error do u get while compiling? Pls post ur gradle code
  • This should be considered the best answer.