본문 바로가기

나의 플랫폼/안드로이드

[Android] Project에 Proguard 적용 하기

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

ProGuard를 적용하는 방법은 쉽습니다.


build.gradle 파일 에 아래 소스만 넣어주면 땡입니다.


buildTypes {

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


minifyEnabled true 를 해주면 클래스나 변수 명이 a,b,c등으로 구분 됩니다.

(runproguard true 는 에러(Could not find method runProguard() for arguments)가 발생 하므로 위 내용으로 사용 합니다.)


하!지!만! 역시 쉬운일은 없나 보더라구요.

위와 같이 설정을 하면 빌드 시, waring이 좌르를 뜨거나 요상한 에러가 떠서 원하는대로 되지가 않습니다.

이럴 경우 포기하지말고 아래를 참고해서 도전해보세요.


## WARNING: Dependency org.json:json:20090211 is ignored for debug

위와 같은 warning이 뜰 경우

compile ('특정 library'){
    exclude group: 'org.json', module: 'json'

}

https://github.com/socketio/engine.io-client-java/issues/13


대부분 compile '특정 library' 라고 사용을 하셨을 겁니다.

그 부분을 양쪽 끝에 '()' 로 묶은 후 '{}' 안에 exclude group: 'org.json', module: 'json' 라고 넣어 주세요.

어떤 library가 문제 인지 알수 없기 때문에 우선 전부다 넣어서 빌드해보신 후, 차근차근 제거하는 것도 방법일듯 합니다.


## Warning:Dependency org.apache.httpcomponents:httpclient:4.3.5 is ignored for debug as it may be conflicting with the internal version provided by Android.

이 부분은 httpclient 4.3.5 를 사용하는데 내부 요소와 겹쳐서 문제가 발생한듯 한데요.

열심히 검색 해보니 compile 'org.apache.httpcomponents:httpmime:4.3.5' 대신 httpmim-4.3.5.jar를 받아 추가 하면 된다고 합니다.

http://stackoverflow.com/questions/25483410/duplicate-files-during-packaging-of-apk-app-debug-unaligned-apk


compile 'org.apache.httpcomponents:httpmime:4.3.5' 부분을 삭제하고  libs 폴더에 다운 받은 httpmime-4.3.5.jar 파일을 넣습니다.

compile fileTree(dir: 'libs', include: ['*.jar'])


위 소스를 대신 넣어주세요. libs 폴더에 있는 jar파일을 모두 추가하는 소스 입니다.


[참고 사항] 

Dependency Tree

  • org.apache.httpcomponents:httpmime:jar:4.5.1 Information
    • org.apache.httpcomponents:httpclient:jar:4.5.1 (compile) Information
      • org.apache.httpcomponents:httpcore:jar:4.4.3 (compile) Information
      • commons-logging:commons-logging:jar:1.2 (compile) Information
      • commons-codec:commons-codec:jar:1.9 (compile) Information
    • junit:junit:jar:4.11 (test) Information
      • org.hamcrest:hamcrest-core:jar:1.3 (test) Information


https://hc.apache.org/httpcomponents-client-ga/httpmime/dependencies.html


참고 : Http 라이브러리 사용시 주의사항



## Warning:library class ...


Warning:library class android.net.http.AndroidHttpClient depends on program class org.apache.http.HttpRequest

Warning:library class android.net.http.AndroidHttpClient depends on program class org.apache.http.HttpEntity

Warning:library class android.net.http.AndroidHttpClient depends on program class org.apache.http.params.HttpParams

...


위와 같이 Warning이 뜨기도 한다. 

이부부은 waring을 뜨지 않도록 proguard-rules.pro 파일에서 정의해 줄수 있다.

-dontwarn org.apache.http.**


위 명령어는 org.apache.http. 로 시작하는 패키지 아래에 관련되어 있는 waring을 띄우게 하지 말라는 의미이다.


## App 실행 시, NullpointerException 이나 ClassCast 가 안된다는 Exception일 발생


이것은 암호화 할때, Class가 꼬여버리는 현상이다.

예를 들어 구글 라이브러리를 우리가 ProGuard 할 필요가 있을까??

따라서 특정 라이브러리를 ProGuard에서 제외 시키는 소스 이다.


-keep class android.support.v4.app.** { *; }

-keep interface android.support.v4.app.** { *; }

-keep class android.support.v7.app.** { *; }

-keep interface android.support.v7.app.** { *; }

...


progiard-rules.pro 파일에 위와 같은 소스를 우선 기본으로 넣어주는 것이 좋다
그럼 위와 관련된  클래스는 ProGuard가 적용되지 않아 문제가 발생하지 않습니다.

따라서, 만약 해당 클래스가 Exception이 발생 하면 그 Class를 ProGuard에서 제외 시키면 됩니다.

##   at com.google.gson.reflect.TypeToken.getSuperclassTypeParameter(Unknown Source)

혹시 에러 중에 위와 같은 경우가 있을 경우 아래 소스를 참조해 보세요.

Guess this is a gson "problem", here's the solution:

-keepattributes Signature
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.examples.android.model.** { *; }

Thanks to https://groups.google.com/forum/#!topic/google-gson/6XuHfOoZIKo

http://stackoverflow.com/questions/17429070/android-proguard-exceptionininitializererror-and-runtimeexception

## 그래도 안된다!!!!

그럼 아래 소스를 넣어보세요.

-renamesourcefileattribute SourceFile
-keepattributes Signature,SourceFile,LineNumberTable
-keep public class * extends android.app.Application

아마 왠만하면 될듯 합니다.


## 이블로그는 어디까지는 찾았던 부분을 잊지 않기 위해 올려놓은 것 입니다.

    내용이 부실해도 이해해 주시길 바랍니다.