Saturday, March 5, 2016

android - How to get application version name and code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#fdedfd"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="50sp"
        android:textStyle="bold"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#f219b5"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private TextView mTextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();

        // Get the activity
        mActivity = MainActivity.this;

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mTextView = (TextView) findViewById(R.id.tv);

        /*
            PackageInfo
                Overall information about the contents of a package. This corresponds to all of the
                information collected from AndroidManifest.xml.
        */
        try{
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(),0);

            /*
                versionName
                    The version name of this package, as specified by the <manifest> tag's
                    versionName attribute.
            */
            String versionName = packageInfo.versionName;

            /*
                versionCode
                    The version number of this package, as specified by the <manifest> tag's
                    versionCode attribute.
            */
            int versionCode = packageInfo.versionCode;

            // Display the application version name and code to TextView
            mTextView.setText("Version Name : " + versionName);
            mTextView.setText(mTextView.getText() + "\nVersion Code : " + versionCode);
        }catch (PackageManager.NameNotFoundException e){
            e.printStackTrace();
        }
   }
}
build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.cfsuman.me.androidcodesnippets"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 2
        versionName "1.1"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'

    compile 'com.android.support:appcompat-v7:23.1.1'
}