【问题标题】:Android Proguard - Reflection java.lang.reflect call not workingAndroid Proguard - 反射 java.lang.reflect 调用不起作用
【发布时间】:2014-05-19 18:10:22
【问题描述】:

我正在使用以下方法在我的 Android ActionBar 上强制堆叠标签。

当我开始使用 proguard 时,类操作不再起作用。没有收到错误消息,我只是没有得到堆叠的选项卡 ActionBar。

这就是我的 proguard 规则中的内容。有什么明确的我遗漏的吗?

-keepattributes Signature
-keepattributes *Annotation*
-keep class java.lang.reflect.** { *; }
-keep class com.company.myapplication1.SetStackedTabs.** { *; }

_

public static void SetStackedTabs(Object inActionBar, final boolean inHasEmbeddedTabs){
    // get the ActionBar class
    Class<?> actionBarClass = inActionBar.getClass();

    // if it is a Jelly Bean implementation (ActionBarImplJB), get the super class (ActionBarImplICS)
    if ("android.support.v7.app.ActionBarImplJB".equals(actionBarClass.getName()))
    {
        actionBarClass = actionBarClass.getSuperclass();
    }

    // if Android 4.3 >
    if ("android.support.v7.app.ActionBarImplJBMR2".equals(actionBarClass.getName())){
        actionBarClass = actionBarClass.getSuperclass().getSuperclass();
    }

    try
    {
        // try to get the mActionBar field, because the current ActionBar is probably just a wrapper Class
        // if this fails, no worries, this will be an instance of the native ActionBar class or from the ActionBarImplBase class
        final Field actionBarField = actionBarClass.getDeclaredField("mActionBar");
        actionBarField.setAccessible(true);
        inActionBar = actionBarField.get(inActionBar);
        actionBarClass = inActionBar.getClass();
    }
    catch (IllegalAccessException e) {}
    catch (IllegalArgumentException e) {}
    catch (NoSuchFieldException e) {}

    try
    {
        // now call the method setHasEmbeddedTabs, this will put the tabs inside the ActionBar
        // if this fails, you're on you own <img class="wp-smiley" alt=";-)" src="http://www.blogc.at/wp-includes/images/smilies/icon_wink.gif">
        final Method method = actionBarClass.getDeclaredMethod("setHasEmbeddedTabs", new Class[] { Boolean.TYPE });
        method.setAccessible(true);
        method.invoke(inActionBar, new Object[]{ inHasEmbeddedTabs });
    }
    catch (NoSuchMethodException e)        {}
    catch (InvocationTargetException e) {}
    catch (IllegalAccessException e) {}
    catch (IllegalArgumentException e) {}
}

【问题讨论】:

    标签: java android reflection proguard


    【解决方案1】:

    解决了。

    我必须将此行输入到 ProGuard 配置中。

    -keep class android.support.v7.** { *; }
    

    【讨论】:

    • Proguard 混淆你的代码。根据 proguard 配置类,方法,字段,局部变量被重命名。所以在这里你检查一个类名是否等于另一个类名,但是 proguard 将它更改为 A.class 之类的东西。使用 -keep class proguard 不会更改匹配给定模式的类。
    • 你在哪个位置添加了 -keep class android.support.v7.** { *; } ?我也面临同样的问题。
    猜你喜欢
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    相关资源
    最近更新 更多