1

我已经使用 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代码完成了添加,删除操作栏中的菜单项。

4

2 回答 2

3

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

在我的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解决问题。IE:

-keep class com.example.widget.ActionSeparatorView { *; }
于 2015-04-01T07:08:40.027 回答
0

Android 最近改变了他们推荐集成 proguard 的方式。尝试这个:

项目属性

# 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

于 2013-05-26T18:14:28.217 回答