如何proguard android jar

时间:2012-03-23 06:52:49

标签: android proguard

我知道如何预测android项目,但目前我使用android做一个jar,以便人们可以在Android应用程序中使用我的jar,但我想我的jar proguard .i使用proguard工具,我的.pro文件是:

-libraryjars 'D:\Android\android-sdk\platforms\android-7\android.jar'

-optimizationpasses 5
 -dontusemixedcaseclassnames 
 -dontskipnonpubliclibraryclasses 
  -dontpreverify 
 -verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* 

-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 public abstract interface com.asqw.android.Listener{
 public protected <methods>; 
}

-keepclasseswithmembernames class * { 
 native <methods>;
}
-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.app.Activity {
 public void *(android.view.View);
}

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

}     -keep class *实现android.os.Parcelable {      public static final android.os.Parcelable $ Creator *;    } 它给了我成功,但当我在我的应用程序中调用proguarded jar时,找不到某个类。

在我的项目中:它给我:import com.ant.liao.GifView.GifImageType;找到了canot。我的gifView文件是:

 public class GifView extends ImageView implements GifAction{


private GifDecoder gifDecoder = null;

private Bitmap currentImage = null;

private boolean isRun = true;

private boolean pause = false;

private DrawThread drawThread = null;

private Context context = null;

private boolean cacheImage = false;

private View backView = null;

private GifImageType animationType = GifImageType.SYNC_DECODER;

public enum GifImageType{

    WAIT_FINISH (0),

    SYNC_DECODER (1),

    COVER (2);

    GifImageType(int i){
        nativeInt = i;
    }
    final int nativeInt;
}


public GifView(Context context) {
    super(context);
    this.context = context;
    //gifDecoder = new GifDecoder(this);
    setScaleType(ImageView.ScaleType.FIT_XY);
}

public GifView(Context context, AttributeSet attrs) {
    this(context, attrs,0);

}  

public GifView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context = context;
   // TypedArray a = context.obtainStyledAttributes(attrs,R.array.);
    //gifDecoder = new GifDecoder(this);
    setScaleType(ImageView.ScaleType.FIT_XY);
}


 private void setGifDecoderImage(byte[] gif){

    if(gifDecoder == null){
        gifDecoder = new GifDecoder(this);
    }
    gifDecoder.setGifImage(gif);
    gifDecoder.start();
}


private void setGifDecoderImage(InputStream is){

    if(gifDecoder == null){
        gifDecoder = new GifDecoder(this);
    }
    gifDecoder.setGifImage(is);
    gifDecoder.start();


}


public void setAsBackground(View v){
    backView = v;
}

protected Parcelable onSaveInstanceState() {
    super.onSaveInstanceState();
    if(gifDecoder != null)
        gifDecoder.free();

    return null;
}

// public void setCahceImage(){ // if(gifDecoder == null){ // gifDecoder = new GifDecoder(this); //} // cacheImage = true; // gifDecoder.setCacheImage(true,context); //}

public void setGifImage(byte[] gif){
    setGifDecoderImage(gif);
}


public void setGifImage(InputStream is){
    setGifDecoderImage(is);
}


public void setGifImage(int resId){
    Resources r = getResources();
    InputStream is = r.openRawResource(resId);
    setGifDecoderImage(is);
}


public void destroy(){
    if(gifDecoder != null)
        gifDecoder.free();
}


public void showCover(){
    if(gifDecoder == null)
        return;
    pause = true;
    currentImage = gifDecoder.getImage();
    invalidate();
}


public void showAnimation(){
    if(pause){
        pause = false;
    }
}


public void setGifImageType(GifImageType type){
    if(gifDecoder == null)
        animationType = type;
}


 public void parseOk(boolean parseStatus,int frameIndex){
    if(parseStatus){
        if(gifDecoder != null){
            switch(animationType){
            case WAIT_FINISH:
                if(frameIndex == -1){
                    if(gifDecoder.getFrameCount() > 1){    
                        DrawThread dt = new DrawThread();
                        dt.start();
                    }else{
                        reDraw();
                    }
                }
                break;
            case COVER:
                if(frameIndex == 1){
                    currentImage = gifDecoder.getImage();
                    reDraw();
                }else if(frameIndex == -1){
                    if(gifDecoder.getFrameCount() > 1){
                        if(drawThread == null){
                            drawThread = new DrawThread();
                            drawThread.start();
                        }
                    }else{
                        reDraw();
                    }
                }
                break;
            case SYNC_DECODER:
                if(frameIndex == 1){
                    currentImage = gifDecoder.getImage();
                    reDraw();
                }else if(frameIndex == -1){
                    reDraw();
                }else{
                    if(drawThread == null){
                        drawThread = new DrawThread();
                        drawThread.start();
                    }
                }
                break;
            }

        }else{
            Log.e("gif","parse error");
        }

    }
}

private void reDraw(){
    if(redrawHandler != null){
        Message msg = redrawHandler.obtainMessage();
        redrawHandler.sendMessage(msg);
    }

}

private void drawImage(){
    setImageBitmap(currentImage);
    invalidate();
}

private Handler redrawHandler = new Handler(){
    public void handleMessage(Message msg) {
        try{
            if(backView != null){
                backView.setBackgroundDrawable(new BitmapDrawable(currentImage));
            }else{
                drawImage();
            }
        }catch(Exception ex){
            Log.e("GifView", ex.toString());
        }
    }
};

  private class DrawThread extends Thread{  
    public void run(){
        if(gifDecoder == null){
            return;
        }
        while(isRun){
            if(gifDecoder.getFrameCount() == 1){

                GifFrame f = gifDecoder.next();
                currentImage = f.image;
                gifDecoder.free();
                reDraw();

                break;
            }
            if (pause == false) {
                GifFrame frame = gifDecoder.next();

                if (frame == null) {
                    SystemClock.sleep(50);
                    continue;
                }
                if (frame.image != null)
                    currentImage = frame.image;
                else if (frame.imageName != null) {
                    currentImage = BitmapFactory.decodeFile(frame.imageName);
                }
                long sp = frame.delay;
                if (redrawHandler != null) {
                    reDraw();
                    SystemClock.sleep(sp);
                } else {
                    break;
                }
            } else {
                SystemClock.sleep(50);
            }
        }
    }
}

} 当我用proguard工具编写文件时,gf2.setGifImage(R.drawable.a2); gf2.setGifImageType(GifImageType.COVER); gf1.showCover()等等,所有未从jar调用的公共方法

2 个答案:

答案 0 :(得分:2)

将以下内容添加到proguard.config。我认为这将有助于保留枚举:

-keep public enum com.ant.liao.GifView.GifImageType$** {
    **[] $VALUES;
    public *;
}

或绕过定义枚举的类:

-keep class com.ant.liao.GifView { *; }

答案 1 :(得分:1)

-injars GifView.jar -outjars G.jar

-libraryjars'D:\ Android \ android-sdk \ platforms \ android-7 \ android.jar'

-optimizations!code / simplifiedification / arithmetic,!field / ,! class / merge / - 优化通过5 -dontusemixedcaseclassnames -dontpreverify -verbose

-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 public interface com.ant.liao.GifAction -keepclassmembers类com.ant.liao.GifView {      public void );      public void set *( * );      public void show *();

} -keep class com.ant.liao.GifImageType {*; }

-keep public interface com.asqw.android.Listener {     公共保护; }

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

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

-keepclassmembers class * extends android.app.Activity {     public void *(android.view.View); }

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

还保留 - 枚举。保留

中所需的特殊静态方法

枚举类。

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

保留名称 - 本机方法名称。保留所有本机类/方法名称。

-keepclasseswithmembers,allowshrinking class * {     原生的 } 我从gifView java文件中获取eunm,并将eunm GifImageType作为一个单独的java文件放置

相关问题