Get App Version | Android Studio | Kotlin
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 the version name and version code
How to set them in
build.gradle
How to retrieve them in Kotlin 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 Kotlin
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.kt
package com.technifysoft.myapplication import android.content.pm.PackageManager import android.os.Bundle import android.util.Log import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivityy : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) try { // Get the PackageInfo object for the current application package val packageInfo = packageManager.getPackageInfo(packageName, 0) // Get the version name of the application (e.g., "1.0.3") val appVersion = packageInfo.versionName // Get the version code of the application (e.g., 4) and convert it to a String val appCode = packageInfo.versionCode.toString() // Find the TextView in the layout with the ID appVersion val appVersionTextView = findViewById<TextView>(R.id.appVersion) // Find the TextView in the layout with the ID appCode val appCodeTextView = findViewById<TextView>(R.id.appCode) // Set the text of the appVersionTextView to display the app's version name appVersionTextView.text = "Version Name: $appVersion" // Set the text of the appCodeTextView to display the app's version code appCodeTextView.text = "Version Code: $appCode" } catch (e: PackageManager.NameNotFoundException) { // Handle the case where the package name is not found. // This should ideally not happen if packageName is used correctly. Log.e("MAIN_TAG", "onCreate: ", e) } } }
Comments
Post a Comment