Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ivaniskandar/e4a0f630437db7035ba731218daa8d4a to your computer and use it in GitHub Desktop.
Save ivaniskandar/e4a0f630437db7035ba731218daa8d4a to your computer and use it in GitHub Desktop.
get android app round icon id
val appContext = context.createPackageContext(packageName, 0)
val res = appContext.resources
// val cookie = res.assets.findCookieForPath(appContext.applicationInfo.sourceDir)
val apkCount = appContext.applicationInfo.splitSourceDirs.count() + 1
(0..apkCount).forEach { cookie ->
res.assets.openXmlResourceParser(cookie, "AndroidManifest.xml").use {
var eventType: Int
while (true) {
eventType = it.nextToken()
if (eventType == XmlPullParser.START_TAG && it.name == "application") {
for (i in 0 until it.attributeCount) {
if (it.getAttributeName(i) == "roundIcon") {
return ResourcesCompat.getDrawable(
res,
Integer.parseInt(it.getAttributeValue(i).substring(1)),
null
)
}
}
}
if (eventType == XmlPullParser.END_DOCUMENT) break
}
}
}
val ai = context.packageManager.getApplicationInfo(packageName, 0)
return ResourcesCompat.getDrawable(
context.packageManager.getResourcesForApplication(ai),
ai.roundIconRes,
null
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment