web-dev-qa-db-ja.com

列挙型の定数とフィールドを保持するようにproguardに指示する方法

Spring、jaxbを使用し、注釈とリフレクションに大きく依存するWebアプリケーションを難読化してみました。インターネットにある多くのレシピを適用して、いくつかのクラス、属性、注釈、列挙を保持します。しかし、列挙にはまだ問題があります。私は http://proguard.sourceforge.net/manual/examples.html#enumerations から列挙定数適用設定を保持することができました:

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

一見、動作するソリューションと定数が保存されているように見えるため、( Class.getEnumConstants() )は正しい値のリストを返します。しかし、名前のいずれかでフィールドを取得しようとすると、NoSuchFieldExceptionが発生しました。

問題は jaxb reflection navigator から来ています。コードを見てください:

public Field[] getEnumConstants(Class clazz) {
    try {
        Object[] values = clazz.getEnumConstants();
        Field[] fields = new Field[values.length];
        for (int i = 0; i < values.length; i++) {
            fields[i] = clazz.getField(((Enum) values[i]).name());
        }
        return fields;
    } catch (NoSuchFieldException e) {
        // impossible
        throw new NoSuchFieldError(e.getMessage());
    }
}

私はまさに「不可能」ブランチに分類されます。デバッグセッションのスクリーンショットを見るのは簡単に理解できると思います( constants もリストされています): Screenshot of debug session1

そして fields を取得しようとすると、a、b、c、d、e、fとして難読化されてリストされます:- Screenshot of debug session2

私のproguardの構成は次のようになります(いくつかのライブラリリストを削除し、proguardについての特定のクラス、フィールド、およびメソッドをクレームします):

-injars  core-3.15.rc5.6.jar
-outjars core-3.15.rc5.6.proguard.jar

-libraryjars <Java.home>/lib/rt.jar

-libraryjars ... # Other libs listed, strip out for shortness

-printmapping core-3.15.rc5.6.proguard.map

-keep public class ru.rlh.egais.portal.backend.controller.rest.**
-keep public class ru.rlh.egais.portal.backend.integration.soap.service.**

# http://midgetontoes.com/blog/2015/06/26/tips-for-using-proguard-with-spring-framework
-optimizations !class/marking/final

-adaptresourcefilecontents **.properties,META-INF/MANIFEST.MF,META-INF/spring.*,spring/*

-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
# Also tried:
# -keepattributes **

-allowaccessmodification
-dontshrink
-dontoptimize
-dontusemixedcaseclassnames
-keepdirectories
-keep @org.springframework.transaction.annotation.Transactional class *
-keep @org.springframework.stereotype.Service class *
-keep @org.springframework.stereotype.Repository class *
-keep @org.springframework.stereotype.Controller class *
-keep @org.springframework.stereotype.Component class *
-keep @org.springframework.beans.factory.annotation.Autowired class *
-keep @org.springframework.web.bind.annotation.ResponseBody class *
-keep @org.springframework.web.bind.annotation.RequestMapping class *
-keep @org.springframework.stereotype.Repository class *
-keep @javax.annotation.Resource class *
-keep @org.springframework.cache.annotation.EnableCaching class *
-keep @org.springframework.context.annotation.Configuration class *

-keepclassmembers class * {
    @org.springframework.beans.factory.annotation.* *;
    @org.springframework.beans.factory.annotation.Qualifier *;
    @org.springframework.beans.factory.annotation.Value *;
    @org.springframework.beans.factory.annotation.Required *;
    @org.springframework.context.annotation.Bean *;
    @javax.annotation.PostConstruct *;
    @javax.annotation.PreDestroy *;
    @org.aspectj.lang.annotation.AfterReturning *;
    @org.aspectj.lang.annotation.Pointcut *;
    @org.aspectj.lang.annotation.AfterThrowing *;
    @org.aspectj.lang.annotation.Around *;
}

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

それで、パブリック列挙型を難読化から完全に防ぐ方法はありますか?両方の場合-定数( class.getEnumConstants() )とフィールド(- class.getFields() )を使用します。

20
Hubbitus

http://sourceforge.net/p/proguard/discussion/182455/thread/1c28f199/ のおかげで、質問に対する解決策を見つけました( <⁠fields> を追加する必要があります):

-keepclassmembers class * extends Java.lang.Enum {
    <fields>;
    public static **[] values();
    public static ** valueOf(Java.lang.String);
}
45
Hubbitus

これはうまくいきました。

-keep public enum com.company.appname.**{
    *;
}

**はパッケージとサブパッケージです。

10
Joma