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

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;

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

  • 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">
    
  • 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.


    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>
    
    链接地址: http://www.djcxy.com/p/16120.html

    上一篇: 正在放弃申请吗?

    下一篇: 如何从应用程序的(布局)XML变量中获取Manifest版本号?