mardi 18 juin 2019

Why can't I access private field via reflection?

I'm trying to get "Animator" instances that are stored in the private class "AnimatedVectorDrawableState" that is in "AnimatedVectorDrawable" class via reflection.

If all properties were public, the "Animators" would be able to get by such line:

AnimatedVectorDrawable().mAnimatedVectorState.mAnimators

To get Animators I use following method:

private fun AnimatedVectorDrawable.getAnimators() {
    val field = javaClass.getDeclaredField("mAnimatedVectorState")
    field.isAccessible = true

    val animatedVectorDrawableState = field.get(this)

    val animatorsField = animatedVectorDrawableState.javaClass.getDeclaredField("mAnimators")
    animatorsField.isAccessible = true

    Log.d("custom", animatorsField.toString())
}

But the program crashed when I try get "mAnimators" with message: "No field mAnimators in class Landroid/graphics/drawable/AnimatedVectorDrawable$AnimatedVectorDrawableState;"

Despite the fact that I'm able to see "mAnimators" property via Android Debugger.

How can I obtain "mAnimators" data from "AnimatedVectorDrawable"?

Here is the screenshot of the debug data: debug_data





Aucun commentaire:

Enregistrer un commentaire