Get App Version | Android Studio | Compose
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 Jetpack Compose
📌 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
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.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.TextStyle import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.sp import com.technifysoft.myapplication.ui.theme.MyApplicationTheme class MainActivityCompose : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { MyApplicationTheme { Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> MainUI( modifier = Modifier.padding(innerPadding) ) } } } } } @Composable fun MainUI(modifier: Modifier = Modifier) { val context = LocalContext.current var appVersionName = "" var appVersionCode = "" try { // Get the PackageInfo object for the current application package val packageInfo = context.packageManager.getPackageInfo(context.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() // Set the text of the appVersionTextView to display the app's version name appVersionName = "Version Name: $appVersion" // Set the text of the appCodeTextView to display the app's version code appVersionCode = "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) } Row(modifier = modifier.fillMaxSize(), verticalAlignment = Alignment.CenterVertically) { // Set the app's version name to Text Text(text = appVersionName, style = TextStyle(fontSize = 25.sp)) // Set the app's version code to Text Text(text = appVersionCode, style = TextStyle(fontSize = 25.sp)) } } @Preview(showBackground = true) @Composable fun GreetingPreview() { MyApplicationTheme { MainUI() } }
Comments
Post a Comment