woong's

Android Proguard 사용하기 본문

Develop/Android

Android Proguard 사용하기

dlsdnd345 2017. 4. 4. 14:57
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

 Proguard 사용하지 않았을시 문제점

 - 프로젝트 코드가 난독화가 되지 않아 디버그 시 코드 노출이 우려

 - 불필요한 코드가 앱 컴파일시 포함 되어 불필요하게 앱의 용량(비용) 이 증가

 - 불필요한 코드가 컴파일 포함 되기 때문에 개발시 메서드 65536 개 초과로 멀티덱스 사용을 피할수 없음 


장점

 - 프로젝트 코드 난독화 (디컴파일시 난독화가 되어 코드를 읽기가 어려움)

 - 프로젝트에서 사용하지 않는 메서드 제거

 - 불필요한 메서드 제거로 인해 멀티덱스 사용하지 않을수 있음


proguard를 사용하면서 불편점

- 난독화가 진행 되어 프로젝트 클래스 이름 , 라인 넘버가 제거 되어 디버그가 어려움

 - 다른 라이브러리가 추가시 난독화시 warning 이 발생


proguard 불편점 해결방법

- 난독화가 진행 되어 프로젝트 클래스 이름 , 라인 넘버가 제거 되어 디버그가 어려움


proguard - debug.pro 라는 이름으로 파일을 추가 하고 하단의 코드를 작성합니다.

# Begin: Debug ProGuard rules

-dontobfuscate
-keepattributes SoureFile,LineNumberTable

# End: Debug ProGuard rules

명령어

-dontobfuscate : 난독화를 수행 하지 않도록 함

- keepattributes : 소스파일 , 라인 정보 유지

buildTypes {

debug {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt')
//프로가드 설정
proguardFile 'proguard-rules.pro'
//디버그에 필요한 프로가드 설정
proguardFile 'proguard-debug.pro'
}

release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt')
//프로가드 설정
proguardFile 'proguard-rules.pro'
}
}

build.gradle 파일에 생성한 proguard-debug 파일을 추가합니다. 위와같이 작성후 debug 시에만 proguard-debug 룰을 추가함으로써

난독화가 되고 라인이 지워지는 것을 방지 할수 있습니다. 릴리즈 되는 apk에 대해서는 proguard-debug 제거함으로써 난독화를 통해 앱 디버그시

코드 유출을 피할수 있습니다.


Tip


shrinkResources 명령어를 통해서 난독화 과정에서 사용하지 않는 리소스를 제거 할수 있습니다.

리소스 축소는 코드 축소와 함께만 동작 합니다. 모든 미사용 코드를 제거후 앱에서 사용되지 않는 리소스를 식별 할수 있습니다.


Proguard Rule 사용방법


Proguard 룰은 필요한것과 불필요한것을 분리 할수 있습니다. 다른 라이브러리 추가시 난독화가 불필요한경우 제거를 해주어야 합니다.

여러 주요 옵션이 많이 있지만 자주 쓰이는 옵션만 정리 하겠습니다.


난독화 옵션


-dontwarn 패키지명.** : 지정해서 경고 무시

-keep class 패키지명.** : 난독화가 필요하지 않은 경우

-ignorewarnings : 경고 무시

-dontoptimize : 최적화 하지 않기

-dontshrink : 사용하지 않는 메소드 유지

-keepclassmembers : 특정 클래스 멤버 원상태 유지

-keepattributes : 내부 클래스 원상태 유지 적용

-keep class org.xmlpull.v1.** { *; }

-dontwarn org.ejml.**
-dontwarn org.xmlpull.**
-dontwarn io.reactivex.**
-dontwarn com.squareup.okhttp.**
-dontwarn com.thoughtworks.xstream.**
-dontwarn boofcv.**
-dontwarn java.awt.**

자주 사용하는 라이브러리 Proguard Rlue


1. Retrofit2

# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions

2. Glide

-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}

# for DexGuard only
-keepresourcexmlelements manifest/application/meta-data@value=GlideModule


3.Picasso

-dontwarn com.squareup.okhttp.**


적용사례


실제 개발 중인 어플을 적용해 보았습니다. 사용하지 않은 경우는 54006 사용하고 있고 패키를 봐도 불필요한 메서드들이 다 포함되어있습니다.

하지만 아래 적용된 경우를 보면 16432 를 사용하고 있고 불필요한 패키지및 메서드가 제거된것을 볼수 있습니다.

앱의 용량도 적용하지 않은 경우와 적용한경우 비교시 1/4 만큼 줄어든것을 보았습니다.

shrinkResources 명령어를 통해서 리소스 용량 32MB - > 26MB 로 줄어든것을 볼수 있습니다.


Proguard 를 사용하지 않은 경우


Proguard 를 사용한 경우


shrinkResources 사용하지 않은경우


shrinkResources 사용한 경우



커니님의 멀티덱스 포스팅 참고 http://kunny.github.io/lecture/proguard/2017/02/16/reduce-method-count-with-proguard/

'Develop > Android' 카테고리의 다른 글

Kotlin 기본 문법 공부하기  (0) 2017.05.19
Kotlin 사용하기 위한 준비 작업 하기  (0) 2017.05.19
Android ButterKnife 사용하기  (0) 2017.02.10
Android Trasitions API 사용하기  (0) 2016.10.12
Android 국가별 폴더 분기  (0) 2016.10.10
Comments