Skip to content

Instantly share code, notes, and snippets.

@jaredrummler
Last active June 25, 2019 20:49
Show Gist options
  • Save jaredrummler/0f4e5c3bad5a71a859022cb6c33d452c to your computer and use it in GitHub Desktop.
Save jaredrummler/0f4e5c3bad5a71a859022cb6c33d452c to your computer and use it in GitHub Desktop.
Hackity hack hack hack; Android resource attack
fun Resources.getValue(id: Int, resolveRefs: Boolean = true) = TypedValue().also {
  try {
    (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
      Resources::class.java.getDeclaredField("mResourcesImpl").apply {
        isAccessible = true
      }.get(this)
    } else this)?.run obj@{
      this::class.java.getDeclaredMethod("getValue",
          Int::class.java, TypedValue::class.java, Boolean::class.java).apply {
        isAccessible = true
        invoke(this@obj, id, it, resolveRefs)
      }
    }
  } catch (ignored: Exception) {
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment