Read versionCode from the Manifest, since BuildConfig.VERSION_CODE does not contain the correct last digit.

This commit is contained in:
Benoit Marty
2024-05-24 15:06:47 +02:00
parent c9d5a1027e
commit 7d3cef33a9
4 changed files with 18 additions and 3 deletions

View File

@@ -28,6 +28,7 @@ import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.annotation.ChecksSdkIntAtLeast
import androidx.annotation.RequiresApi
import androidx.core.content.pm.PackageInfoCompat
import io.element.android.libraries.androidutils.R
import io.element.android.libraries.androidutils.compat.getApplicationInfoCompat
import io.element.android.libraries.core.mimetype.MimeTypes
@@ -47,6 +48,19 @@ fun Context.getApplicationLabel(packageName: String): String {
}
}
/**
* Retrieve the versionCode from the Manifest.
* The value is more accurate than BuildConfig.VERSION_CODE, as it is correct according to the
* computation in the `androidComponents` block of the app build.gradle.kts file.
* In other words, the last digit (for the architecture) will be set, whereas BuildConfig.VERSION_CODE
* last digit will always be 0.
*/
fun Context.getVersionCodeFromManifest(): Long {
return PackageInfoCompat.getLongVersionCode(
packageManager.getPackageInfo(packageName, 0)
)
}
// ==============================================================================================================
// Clipboard helper
// ==============================================================================================================