mercredi 15 avril 2020

Access fields of android api classes

Is there a way to access the fields of android api classes like GnssMeasurements to set the values. In this class already exist public setter-methods, but they have a @TestApi annotation. I don't know how to access these methods or how to set the private fields

I tried to access the private fields with:

    Class<?> c = GnssMeasurement.class;
    Object o = null;
    try {
        o = c.newInstance();

    } catch (IllegalAccessException | InstantiationException e) {
        e.printStackTrace();
    }

    assert cc != null;
    try {
        Field f1  = o.getClass().getDeclaredField("mPseudorangeRateMetersPerSecond");
        f1.setAccessible(true);
        f1.set(o, 123456.789);
        Double d = (Double) f1.get(o);
        System.out.println("field: " + d);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }

Here i got the following stacktrace:

W/ocation_logger: Accessing hidden field Landroid/location/GnssMeasurement;->mPseudorangeRateMetersPerSecond:D (dark greylist, reflection)
W/System.err: java.lang.NoSuchFieldException: No field mPseudorangeRateMetersPerSecond in class Landroid/location/GnssMeasurement; (declaration of 'android.location.GnssMeasurement' appears in /system/framework/framework.jar)
W/System.err:     at java.lang.Class.getDeclaredField(Native Method)
        at com.example.simplelocation_logger2.MainActivity.initApp(MainActivity.java:287)
        at com.example.simplelocation_logger2.MainActivity.onCreate(MainActivity.java:152)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

And i tried to access the methods as well:

    Class c;
    try {
        c = Class.forName("android.location.GnssMeasurement");
        Method m = c.getMethod("setPseudorangeRateMetersPerSecond", Double.class);
        Object o = m.invoke(null, 123456.789);
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
    }

Which gave me this error:

W/System.err: java.lang.NoSuchMethodException: setPseudorangeRateMetersPerSecond [class java.lang.Double]
W/System.err:     at java.lang.Class.getMethod(Class.java:2068)
        at java.lang.Class.getMethod(Class.java:1690)
        at com.example.simplelocation_logger2.MainActivity.initApp(MainActivity.java:299)
W/System.err:     at com.example.simplelocation_logger2.MainActivity.onCreate(MainActivity.java:152)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
W/System.err:     at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Is there no way to access these fields?





Aucun commentaire:

Enregistrer un commentaire