수색…


널리 사용되는 일부 라이브러리 규칙

현재 다음 라이브러리에 대한 규칙을 포함합니다.

  1. 버터 칼
  2. RxJava
  3. Android 지원 라이브러리
  4. Android 디자인 지원 라이브러리
  5. 개조
  6. Gson과 Jackson
  7. 장미유
  8. Crashlitycs
  9. 피카소
  10. 발리
  11. OkHttp3
  12. 소독 할 수있는
#Butterknife
-keep class butterknife.** { *; }
-keepnames class * { @butterknife.Bind *;}

-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }

-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}

# rxjava
-keep class rx.schedulers.Schedulers {
    public static <methods>;
}
-keep class rx.schedulers.ImmediateScheduler {
    public <methods>;
}
-keep class rx.schedulers.TestScheduler {
    public <methods>;
}
-keep class rx.schedulers.Schedulers {
    public static ** test();
}
-keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
    long producerIndex;
    long consumerIndex;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
    long producerNode;
    long consumerNode;
}

# Support library
-dontwarn android.support.**
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-keep interface android.support.v4.** { *; }
-dontwarn android.support.v7.**
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }

# support design
-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; }

# retrofit
-dontwarn okio.**
-keepattributes Signature
-keepattributes *Annotation*
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**

-dontwarn rx.**
-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {
    @retrofit.http.* <methods>;
}

-keep class sun.misc.Unsafe { *; }
#your package path where your gson models are stored
-keep class com.abc.model.** { *; }

# Keep these for GSON and Jackson
-keepattributes Signature
-keepattributes *Annotation*
-keepattributes EnclosingMethod
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.** { *; }
  
#keep otto
-keepattributes *Annotation*
-keepclassmembers class ** {
    @com.squareup.otto.Subscribe public *;
    @com.squareup.otto.Produce public *;
}

# Crashlitycs 2.+
-keep class com.crashlytics.** { *; }
-keep class com.crashlytics.android.**
-keepattributes SourceFile, LineNumberTable, *Annotation*
# If you are using custom exceptions, add this line so that custom exception types are skipped during obfuscation:
-keep public class * extends java.lang.Exception
# For Fabric to properly de-obfuscate your crash reports, you need to remove this line from your ProGuard config:
# -printmapping mapping.txt

# Picasso
-dontwarn com.squareup.okhttp.** 

# Volley
-keep class com.android.volley.toolbox.ImageLoader { *; }

# OkHttp3
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**

# Needed for Parcelable/SafeParcelable Creators to not get stripped
-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

빌드에 ProGuard 사용 설정

응용 프로그램에서 ProGuard 구성을 활성화하려면 모듈 레벨 gradle 파일에서 활성화해야합니다. minifyEnabled true 값을 minifyEnabled true 로 설정해야 minifyEnabled true .

당신은 또한 사용할 수 있습니다 shrinkResources true 자원 제거 할 ProGuard 미사용 flaggs을.

buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

위의 코드는 proguard-rules.pro 포함 된 ProGuard 구성 (Eclipse의 "proguard-project.txt")을 출시 된 apk에 적용합니다.

나중에 스택 추적에서 예외가 발생한 행을 결정할 수있게하려면 "proguard-rules.pro"에 다음 행이 있어야합니다.

-renamesourcefileattribute SourceFile    
-keepattributes SourceFile,LineNumberTable

Eclipse에서 Proguard를 활성화하려면 proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 를 "project.properties"에 추가하십시오.

빌드시 추적 로깅 (및 기타) 문 제거

특정 메소드에 대한 호출을 제거하려는 경우, void를 리턴하고 부작용이 없다고 가정하면 (예를 들어 호출 할 때 시스템 값, 참조 인수, 통계 등을 변경하지 않는 경우) ProGuard에서 호출을 제거 할 수 있습니다. 빌드가 완료된 후 출력됩니다.

예를 들어, 디버깅에 유용한 디버그 / verbose 로깅 구문을 제거하는 데 유용하지만, 프로덕션 환경에서는 이들을위한 문자열을 생성 할 필요가 없습니다.

# Remove the debug and verbose level Logging statements.
# That means the code to generate the arguments to these methods will also not be called.
# ONLY WORKS IF -dontoptimize IS _NOT_ USED in any ProGuard configs
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}

참고 : 사용하지 않는 코드를 축소 / 제거하지 않도록 ProGuard 구성에서 -dontoptimize 를 사용하면 -dontoptimize 이 제거되지 않습니다. (하지만 사용하지 않는 코드를 제거하지 않으려는 사람은 누구겠습니까?)

참고 2 :이 호출은 로그 호출을 제거하지만 코드를 보호하지는 않습니다. 문자열은 생성 된 apk에 실제로 남아 있습니다. 이 게시물 에서 더 많은 것을 읽으십시오.

해커로부터 코드 보호

난독 화는 코드 보호를위한 마술 솔루션으로 간주되는 경우가 많습니다. 해커가 코드를 해독하면 코드를 이해하기 어렵게 만듭니다.

그러나 Log.x(..) 제거하면 실제로 해커가 필요로하는 정보가 제거 Log.x(..) 생각하는 사람은 놀랄 것입니다.

다음을 사용하여 모든 로그 호출 제거 ​​:

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    ...etc
}

실제로 로그 호출 자체를 제거하지만 일반적으로 사용자가 입력 한 String은 제거 하지 않습니다 .

예를 들어 로그 호출 내에서 다음과 같은 공통 로그 메시지를 입력하면 Log.d(MyTag,"Score="+score); , 컴파일러는 Log 호출 외부에서 + 를 '새 StringBuilder ()'로 변환합니다. ProGuard는이 새 개체를 변경하지 않습니다.

컴파일 된 코드에는 "Score=" 매달린 StringBuilder 가 있으며 score 변수에 대해 난독 화 된 버전이 추가됩니다 (이 코드가 b 로 변환되었다고 가정 해 봅시다).
이제 해커는 b 가 무엇인지 파악하고 코드를 이해합니다.

코드에서 이러한 잔차를 실제로 제거하는 좋은 방법은 먼저 StringFormatter를 사용하지 않고 Proguard 규칙을 사용하여 String Formatter를 제거하거나 Log 호출을 래핑하는 것입니다.

    if (BuildConfig.DEBUG) {
        Log.d(TAG,".."+var);
    }

팁:

난독 화 된 코드를 직접 컴파일 해제하여 테스트하십시오.

  1. dex2jar - apk를 jar 파일로 변환합니다.
  2. jd - jar 파일을 디 컴파일하고 gui 편집기에서 엽니 다.

사용자 정의 난독 화 구성 파일로 ProGuard 활성화

ProGuard는 개발자가 코드를 난독 화하고 축소하고 최적화 할 수있게 해줍니다.

# 1 프로 시저의 첫 번째 단계는 빌드에서 프로 가드를 활성화하는 것 입니다.

원하는 빌드 에서 'minifyEnabled'명령을 true설정하면 됩니다

# 2 두 번째 단계는 주어진 빌드에 어떤 proguard 파일을 사용할지 지정하는 것입니다.

적절한 파일 이름으로 'proguardFiles'라인을 설정하면 됩니다

buildTypes {
    debug {
        minifyEnabled false
    }
    testRelease {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules-tests.pro'
    }
    productionRelease {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules-tests.pro', 'proguard-rules-release.pro'
    }
}

# 3 개발자는 자신이 원하는 규칙으로 프로 가드 파일을 편집 할 수 있습니다.

파일 편집 (예 : 'proguard-rules-tests.pro') 및 원하는 제약 조건 추가로 수행 할 수 있습니다. 다음 파일은 proguard 파일의 예입니다.

// default & basic optimization configurations
-optimizationpasses 5
-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*

-verbose

-dump obfuscation/class_files.txt
-printseeds obfuscation/seeds.txt
-printusage obfuscation/unused.txt // unused classes that are stripped out in the process
-printmapping obfuscation/mapping.txt // mapping file that shows the obfuscated names of the classes after proguad is applied

// the developer can specify keywords for the obfuscation (I myself use fruits for obfuscation names once in a while :-) )
-obfuscationdictionary obfuscation/keywords.txt
-classobfuscationdictionary obfuscation/keywords.txt
-packageobfuscationdictionary obfuscation/keywords.txt

마지막으로, 개발자가 새로운 .APK 파일을 실행하거나 생성 할 때마다 사용자 정의 된 프로 가드 구성이 적용되어 요구 사항을 충족시킵니다.



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow