Created
April 14, 2022 17:47
-
-
Save ivaniskandar/e4a0f630437db7035ba731218daa8d4a to your computer and use it in GitHub Desktop.
get android app round icon id
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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