混淆ActionBar代码不显示菜单项

时间:2013-05-26 17:13:40

标签: android android-actionbar proguard menubar

我使用ActionBar在Android 4.1版本中开发了应用程序。如果我没有混淆ActionBar代码意味着菜单项显示在ActionBar中,而在混淆ActionBar代码后,ActionBar中的菜单项根本不显示。混淆代码如下所示。

-injars       input.jar
-outjars      output.obfuscated.jar
-libraryjars  android.jar
-libraryjars  android-support-v4.jar
-libraryjars  annotations.jar 


-dontpreverify
-dontskipnonpubliclibraryclassmembers
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*

-keepparameternames
-renamesourcefileattribute SourceFile
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
            SourceFile,LineNumberTable,*Annotation*,EnclosingMethod

-keep public class * {
 public protected *;
}

-keepclassmembernames class * {
java.lang.Class class$(java.lang.String);
java.lang.Class class$(java.lang.String, boolean);
}

-keepclasseswithmembernames class * {
native <methods>;
 }

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

-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}

-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 android.os.Parcelable$Creator CREATOR;
}

-keepclassmembers class **.R$* {
 public static <fields>;
 }
-keep class android.support.v4.app.** { *; } 
-keep interface android.support.v4.app.** { *; } 
-keep class com.actionbarsherlock.** { *; } 
-keep interface com.actionbarsherlock.** { *; } 

是否有任何过程来混淆ActionBar代码?

编辑1:

public static void AddMenuItem(String path, int id, String itemLabel)
{ 
    try
    {           
        if(menu!=null)
        {               
        menu.add(0, id, 0, itemLabel).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        MenuItem item=menu.findItem(id);
        InputStream ims = sActiveContext.getAssets().open(path);
        Drawable d = Drawable.createFromStream(ims, null);         
        item.setIcon(d); 
        }

    }
    catch(Exception error)
    { 

    }
}

我没有使用菜单xml资源。我已经通过java代码添加,删除操作栏中的菜单项。

2 个答案:

答案 0 :(得分:1)

我有一个类似的问题,有点不同。看看它是否也适用于你的情况。

在我menu_custom.xml我有一个有actionViewClass的项目,即:

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_share"
        android:icon="@drawable/ic_menu_share"
        android:showAsAction="always"
        android:title="@string/menu_ad_detailed_share"/>

    <item
        android:actionViewClass="com.example.widget.ActionSeparatorView"
        android:showAsAction="always"
        android:title="@null"/>

    <item
        android:id="@+id/action_favorite"
        android:icon="@drawable/ic_menu_favorite_deselected"
        android:showAsAction="always"
        android:title="@string/menu_ad_detailed_favorite"/>
</menu>

根据http://developer.android.com/guide/topics/resources/menu-resource.html,如果您有以下任何属性:

android:actionViewClass android:actionProviderClass android:onClick

您必须将类排除在混淆之外:

  

警告:如果您使用ProGuard(或类似工具)对代码进行模糊处理,请务必通过重命名排除您在此属性中指定的类,因为它可能会破坏功能。

这意味着我必须将我的小部件添加到proguard-rules.txt以解决问题。即:

-keep class com.example.widget.ActionSeparatorView { *; }

答案 1 :(得分:0)

Android最近改变了他们推荐整合proguard的方式。试试这个:

project.properties

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-17

proguard的-project.txt

# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

-keep public class <MyClass>

您可以找到有关如何记录您需要保留的文档:
http://proguard.sourceforge.net/index.html#manual/usage.html