0

Recently I complied the source code of adw-launcher, some of the class of this project use protected variable of android classes. Below is a code snippet:

 @Override
    protected boolean setFrame(int left, int top, int right, int bottom) {
        if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
            mBackgroundSizeChanged = true;
        }
        return super.setFrame(left, top, right, bottom);
    }

mLeft, mRight are all protected.

How to go about this ? I am totally noob in this, need your guidance.

I went through this link: How do I build the Android SDK with hidden and internal APIs available?

If I replace new jar file, hope it will access these new files, will I have to alter these files ? I just don't have any idea.

Thanks

Community
  • 1
  • 1
viv
  • 6,158
  • 6
  • 39
  • 54

3 Answers3

0

You may use powerful (and dangerous) Reflection API of Java at your own risk!

   try {
        Field field = TargetClass.class.getDeclaredField(fieldName);
        field.setAccessible(true);

        result = field.get(targetObject);

        field.setAccessible(false);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }

to get result as int :

int result = field.getInt(targetObject);
semsamot
  • 805
  • 9
  • 11
0

You can't access protected members outside their packages - this is by design of Java.

You might be able to access it using Java reflection, but this isn't advisable.

 Field[] f = ClassName.getClass().getDeclaredFields();

Then, simply f[i].setAccessible(true) to allow yourself to read from them. But this definitely isn't advisable unless truly needed.

  • But then how do they compile the source? For ex they have used mleft variable of view which is protected. I have updated quest. – viv Aug 30 '14 at 06:01
  • @viv I'm sorry, I'm just not sure what you're asking here. This is how you access protected variables of other classes with reflection (in, albeit, a rather dangerous way). What is it you need to know? –  Aug 30 '14 at 06:02
  • The above code snippet is from a class that extends TextView still eclipse, showing the error. – viv Aug 30 '14 at 09:54
0

Android SDK hidden classes are only available to framework level apps/code. You need to link framework.jar and framework2.jar with your code to access hidden APIs. But then also your app might raise a Security Exception at runtime.

As suggested in link you provided you will have to build source to obtain those jars. Those protected variables must be coming from base class. If i understand your question correctly that base class might be coming from framework jars.

Abhishek Bansal
  • 5,197
  • 4
  • 40
  • 69
  • You understood my question well, even if I have compiled those jars, the variables will still be protected, then ? – viv Aug 30 '14 at 07:00
  • Protected variables can only be accessed by subclasses. You need to inherit from that class which is having those variables. – Abhishek Bansal Aug 30 '14 at 08:53