Skip to content

Instantly share code, notes, and snippets.

@seventhmoon
Last active December 18, 2024 15:56
Show Gist options
  • Save seventhmoon/da05dd22820ca74f5d79fbc9cb9bc927 to your computer and use it in GitHub Desktop.
Save seventhmoon/da05dd22820ca74f5d79fbc9cb9bc927 to your computer and use it in GitHub Desktop.
Getting Mainline version and ART version
package com.example.myapplication
import android.content.Context
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.ui.Modifier
import com.example.myapplication.ui.theme.MyApplicationTheme
@OptIn(ExperimentalMaterial3Api::class)
class MainActivity : ComponentActivity() {
private val ART_PACKAGE = "com.google.android.art"
private val MAINLINE_PACKAGE = "com.google.android.modulemetadata"
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
Scaffold(modifier = Modifier.fillMaxSize(), topBar = {
TopAppBar(
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.primaryContainer,
titleContentColor = MaterialTheme.colorScheme.primary,
),
title = {
Text("Mainline Info")
}
)
}) { innerPadding ->
Column(modifier = Modifier.padding(innerPadding)) {
Text("mainlineVersion: " + mainlineVersion(this@MainActivity).toString())
Text("artVersion: " + artVersion(this@MainActivity).toString())
}
}
}
}
Log.d(this.javaClass.name, "mainlineVersion: " + mainlineVersion(this).toString())
Log.d(this.javaClass.name, "artVersion: " + artVersion(this).toString())
}
private fun artVersion(context: Context): Long? {
val moduleProvider = ART_PACKAGE
return try {
val pm = context.packageManager
val packageInfo = pm.getPackageInfo(moduleProvider, PackageManager.MATCH_APEX)
packageInfo.longVersionCode
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
null
}
}
private fun mainlineVersion(context: Context): String? {
val moduleProvider = MAINLINE_PACKAGE
return try {
val pm = context.packageManager
val packageInfo = pm.getPackageInfo(moduleProvider, 0)
packageInfo.versionName
} catch (e: PackageManager.NameNotFoundException) {
null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment