ProgramingTip

Android : Proguard에 권장되는 구성은 무엇입니까?

bestdevel 2020. 11. 21. 09:31
반응형

Android : Proguard에 권장되는 구성은 무엇입니까?


저는 Android 용 앱을 개발하고 Proguard를 사용하여 코드를 난독 화하고 있습니다.

현재 ProGuard 구성을 사용하고 있습니다.

-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 com.android.vending.licensing.ILicensingService

레이아웃 XML에 사용되는 사용자 구성 요소 이름을 유지 비용 :

-keep public class custom.components.**

디버그 로그를 제거 비용 :

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

레이아웃의 onClick에서 호출 된 메서드의 이름을 변경하지면 :

-keepclassmembers class * {
 public void onClickButton1(android.view.View);
 public void onClickButton2(android.view.View);
 public void onClickButton3(android.view.View);
}

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

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

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

질문은 다음과 가변합니다.

다른 태그가 권장 검증? 왜 그리고 무엇을 위해?

proguard.cfg 파일에 주석을 달 수 있습니까? 다른 개발자가 내가 추가 한 이유에 대해 의심하지 않도록 일부 라인이 수행하는 작업에 대한 주석을 달고 싶습니다.

또한 proguard에서 파일의 주석 헤더 (저작권 포함)를 어디에 있습니까? 어떤 경우에는 좋은 정책이 라이선스를 어디에 추가해야합니까?


Android SDK (r20 이상)

project.properties에 참조 된 사전 정의 된 proguard.config를 확인하십시오.

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt

추가 정보 : http://proguard.sourceforge.net/manual/examples.html#androidapplication

여기 에서 내가 계속 업데이트 하는 proguard "기본"파일을 확인할 수 있습니다 . https://medium.com/code-procedure-and-rants/android-my-standard-proguard-ffeceaf65521


Android SDK (r19 이하)

내 대답 에 따라 Android 용 Eclipse에서 ProGuard 활성화가

일반 파일로 끝났습니다. 각 줄의 용도를 기억하기 위해 주석을 추가했습니다. 사람들에게 도움이 될 수 있으므로 여기에 있습니다.

-optimizationpasses 5

#When not preverifing in a case-insensitive filing system, such as Windows. Because this tool unpacks your processed jars, you should then use:
-dontusemixedcaseclassnames

#Specifies not to ignore non-public library classes. As of version 4.5, this is the default setting
-dontskipnonpubliclibraryclasses

#Preverification is irrelevant for the dex compiler and the Dalvik VM, so we can switch it off with the -dontpreverify option.
-dontpreverify

#Specifies to write out some more information during processing. If the program terminates with an exception, this option will print out the entire stack trace, instead of just the exception message.
-verbose

#The -optimizations option disables some arithmetic simplifications that Dalvik 1.0 and 1.5 can't handle. Note that the Dalvik VM also can't handle aggressive overloading (of static fields).
#To understand or change this check http://proguard.sourceforge.net/index.html#/manual/optimizations.html
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

#To repackage classes on a single package
#-repackageclasses ''

#Uncomment if using annotations to keep them.
#-keepattributes *Annotation*

#Keep classes that are referenced on the AndroidManifest
-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 com.android.vending.licensing.ILicensingService


#To remove debug logs:
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}

#To avoid changing names of methods invoked on layout's onClick.
# Uncomment and add specific method names if using onClick on layouts
#-keepclassmembers class * {
# public void onClickButton(android.view.View);
#}

#Maintain java native methods 
-keepclasseswithmembernames class * {
    native <methods>;
}

#To maintain custom components names that are used on layouts XML.
#Uncomment if having any problem with the approach below
#-keep public class custom.components.package.and.name.**

#To maintain custom components names that are used on layouts XML:
-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);
}

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

#To keep parcelable classes (to serialize - deserialize objects to sent through Intents)
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

#Keep the R
-keepclassmembers class **.R$* {
    public static <fields>;
}

###### ADDITIONAL OPTIONS NOT USED NORMALLY

#To keep callback calls. Uncomment if using any
#http://proguard.sourceforge.net/index.html#/manual/examples.html#callback
#-keep class mypackage.MyCallbackClass {
#   void myCallbackMethod(java.lang.String);
#}

#Uncomment if using Serializable 
#-keepclassmembers class * implements java.io.Serializable {
#    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();
#}

Ant 또는 Eclipse 를 사용하는 표준 빌드의 경우 Android SDK (r20 이상)는 이미 project.properties 파일의 proguard.config 속성에서 참조하는 적절한 구성을 제공합니다 (Michal이 답변에서 지적한대로). 이전 릴리스에서는 항상 자신 만의 완전한 구성을 지정해야했기 때문에 이전 조언 (첫 번째 답변과 같은)은 약간의 혼란을 초래할 수 있습니다.

를 들어 사용자 정의 빌드 , 당신은 최신 권장 구성을 찾을 수 있습니다 ProGuard에서 설명서 > > 전체 안드로이드 응용 프로그램을 . Proguard와 분포는 또한 샘플 파일 예제가 포함되어 있습니다 / android.pro

특히 View 확장에서 주석 및 setter를 보존 할 수 있습니다.

샘플 파일에서 광범위하게 수행 한 것처럼 해시 문자 '#'뒤에 주석을 추가 할 수 있습니다.


project.properties 라는 프로젝트 파일에서 다음 값을 설정하십시오.

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt

Android SDK에서 최신 버전의 Proguard 설정을 가져옵니다.

참고 URL : https://stackoverflow.com/questions/5068251/android-what-are-the-recommended-configurations-for-proguard

반응형