Android - dexguard删除菜单图标

时间:2015-12-08 07:37:40

标签: android android-menu android-actionbaractivity dexguard

嘿,我使用dexguard对我的应用进行了混淆,到目前为止一切正常。 现在我尝试在操作栏菜单中添加图标。如果没有dexguard混淆,则会显示图标,但随之显示它们会消失(文本仍然存在)。我觉得这与xml <menu>标签有关,因为混淆不会影响其他衬垫/相对布局。

这是我的actions.xml(在res.layout下,图标在res.drawble-xxx下):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent">

    <item android:id="@+id/action_1"
          android:title="action_1"
          android:icon="@drawable/action_1_icon"
    />

    <item android:id="@+id/action_2"
          android:title="action_2"
          android:icon="@drawable/action_2_icon"
    />

    <item android:id="@+id/action_3"
          android:title="action_3"
          android:icon="@drawable/action_3_icon"
    />

</menu>

我还试图以编程方式添加它们

 @Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem action_1 = menu.findItem(R.id.action_1);
    action_1.setIcon(getResources().getDrawable(R.drawable.action_1_icon));

    MenuItem action_2 = menu.findItem(R.id.action_2);
    action_2.setIcon(getResources().getDrawable(R.drawable.action_2_icon));

    MenuItem action_3 = menu.findItem(R.id.action_3);
    action_3.setIcon(getResources().getDrawable(R.drawable.action_3_icon));

    return super.onPrepareOptionsMenu(menu);
}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

但仍然混淆会删除菜单图标。

编辑:dexguard config

    -verbose
-dalvik

-dontshrink
-dontoptimize
-dontpreverify

-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*
-keepresourcexmlattributenames
    manifest/installLocation,
    manifest/versionCode,
    manifest/application/*/intent-filter/*/name

-dontwarn javax.management.**
-dontwarn java.lang.management.**
-dontwarn org.apache.log4j.**
-dontwarn org.apache.commons.logging.**
-dontwarn org.slf4j.**
-dontwarn org.json.**

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-keep class javax.** { *; }
-keep class org.** { *; }
-keep class twitter4j.** { *; }


-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}


-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider

-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}


-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.content.Context {
   public void *(android.view.View);
   public void *(android.view.MenuItem);
}

-keepclassmembers class * implements android.os.Parcelable {
    static ** CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

-keep public interface com.android.vending.licensing.ILicensingService

-dontwarn android.support.**

1 个答案:

答案 0 :(得分:0)

此问题的解决方案: Menu类(在android.view下)使用MenuBuilder实现(在android.support.v7.internal.view.menu下)。 所以我添加了规则

-keep class android.support.v7.internal.view.menu.** { *; }

现在可行。