11

I want to set "NO_CACHE" to be newValue. Here is my code:

static void setFinalStatic(Object newValue) throws Exception
{
    Field field = CacheManager.class.getDeclaredField("NO_CACHE");
    field.setAccessible(true);
    Field modifiersField=Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field,field.getModifiers() & ~Modifier.FINAL);
    field.set(null, newValue);
}

But running this code on Android produces the following error:

java.lang.NoSuchFieldException: modifiers

I find modifiers is a field in the Field class in Java, but it is not a field in the Field class on Android.

This is Java Field class code:

http://www.docjar.com/html/api/java/lang/reflect/Field.java.html

This is Android Field class code:

http://www.oschina.net/code/explore/android-2.2-froyo/java/lang/reflect/Field.java

The above code in Java can work, but it does not work on the Android platform.

How do I fix this?

5
  • android.webkit.CacheManager Commented Dec 7, 2012 at 11:50
  • How would this work outside of Android, if it is an Android-only class? Commented Dec 7, 2012 at 12:03
  • The CacheManager class code: oschina.net/code/explore/android-2.2-froyo/android/webkit/… Commented Dec 7, 2012 at 12:21
  • I mean, how/why would you use it outside Android? You say this works in Java, but, this class has no field modifiers to begin with. I don't see how it would ever work. Commented Dec 7, 2012 at 13:08
  • Maybe my thinking is wrong. Commented Dec 7, 2012 at 13:44

2 Answers 2

7

I have tried the solutions above and get the correct way to modify static and final field in Android,here is the code works in Android 7.0

Field booleanField = Boolean.class.getField("FALSE");
booleanField.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("accessFlags");
modifiersField.setAccessible(true);
modifiersField.setInt(field,field.getModifiers() & ~Modifier.FINAL);
booleanField.set(null, true);
Sign up to request clarification or add additional context in comments.

1 Comment

when android 30, it not work
3

Android actually doesn't use JDK source code. It's something very alike but not the same.

In Java, modifiers are of type java.lang.reflection.Field, but in Android there is no such type. Android implemented a system which uses an integer and the name of the Field to validate it's modifiers, by calling a native method.

I think a private static final field will have slot-1 and a private field will have the value 3. If you wish to change a private static final field, just change the slot to 3 before setting the value.

Field booleanField = Boolean.class.getField("FALSE");
booleanField.setAccessible(true);

Field slotField = Field.class.getDeclaredField("slot");
slotField.setAccessible(true);
slotField.set(booleanField, 3);

booleanField.set(Boolean.FALSE, true);

1 Comment

The field in Android is called accessFlags not slot and not modifiers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.