Get App Version | Android Studio | Java
How to get the app's current version name and version Code?
When building an Android app, you often need to display or use the version name and version code of your application. For example, you might want to show the version number inside an About Page, or compare versions when checking for app updates.
In this article, I’ll explain:
-
What is version name and version code
-
How to set them in
build.gradle
-
How to retrieve them in Java code
📌 Difference Between Version Code and Version Name
Before jumping into the code, let’s understand what these two terms mean:
-
Version Code (int):
-
An integer value used internally by Android.
-
It must be increased with every release.
-
Users don’t see this number.
-
Example:
1
,2
,3
…
-
-
Version Name (String):
-
A user-friendly string value shown to users.
-
Can be in any format, like
"1.0"
,"1.2.5-beta"
. -
Example:
"1.0"
,"2.5.1"
📌 Getting Version Name and Version Code in Java
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <!--TextView: Show App version e.g. 1.0.3--> <TextView android:id="@+id/appVersion" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" /> <!--TextView: Show App version Code e.g. 4--> <TextView android:id="@+id/appCode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" /> </LinearLayout>
MainActivity.java
package com.technifysoft.myapplication; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { // Get the PackageInfo object for the current application package PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0); // Get the version name of the application (e.g., "1.0.3") String appVersion = packageInfo.versionName; // Get the version code of the application (e.g., 4) and convert it to a String String appCode = String.valueOf(packageInfo.versionCode); // Find the TextView in the layout with the ID appVersion TextView appVersionTextView = findViewById(R.id.appVersion); // Find the TextView in the layout with the ID appCode TextView appCodeTextView = findViewById(R.id.appCode); // Set the text of the appVersionTextView to display the app's version name appVersionTextView.setText("Version Name: " + appVersion); // Set the text of the appCodeTextView to display the app's version code appCodeTextView.setText("Version Code: " + appCode); } catch (PackageManager.NameNotFoundException e) { // Handle the case where the package name is not found. // This should ideally not happen if getPackageName() is used correctly. Log.e("MAIN_TAG", "onCreate: ", e); } } }
Comments
Post a Comment