Development record of developer who study hard everyday.

레이블이 안드로이드proguard인 게시물을 표시합니다. 모든 게시물 표시
레이블이 안드로이드proguard인 게시물을 표시합니다. 모든 게시물 표시
, , , ,

안드로이드 난독화 적용하기

 


안드로이드 난독화 적용하기

이번에 회사에서 네이티브앱 수정하는 업무가 있었다.

작업내용 중에 난독화를 해달라는 요청사항이 있었다.

난독화가 뭔지는 알고있었지만 이렇게 까다로운건 줄 몰랐다;;

그냥 build.gradle에서 minifyEnabled를 true로 설정해주면 끝나는건 줄 알았다;;

삽질을 통해서 얻은 안드로이드 난독화 작업내용을 기록하고자한다.

1. build.gradle(앱수준) 파일 설정

buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
debuggable false
}
}

앱수준 그래들파일에서 위와같이 설정을 해준다.

minifyEnabled 값이 난독화 유무를 설정하는 값이다.

proguardFiles는 이름에서도 나타나듯이 난독화에서 예외처리를 해줄 코드를 정의한 파일을 적어주는 곳이다.

signingConfig는 앱서명정보를 나타내는 곳이다.

debuggalbe을 true로 설정해주면 release 버전으로 빌드했을 때도 로그가 찍히게된다.

android {
signingConfigs {
config {
keyAlias 'blahblah'
keyPassword 'blahblah'
storeFile file('keystore.jks')
storePassword 'blahblah'
}
}
}

그리고 마찬가지로 앱수준 gradle 파일에서 위와같이 앱서명정보를 적어준다.


2. 프로가드 설정하기

이제 그래들 설정은 끝났고 proguard-rules.pro 파일에서 난독화 예외를 적용할 코드를 표시해주어야한다.

# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
-keep class com.bumptech.glide.** {*;}
#-keep class com.github.windsekirun.** {*;}

-keep class com.kakao.sdk.**.model.* { <fields>; }
-keep class * extends com.google.gson.TypeAdapter

-keep interface com.bumptech.glide.** {*;}

-keepattributes *Annotation*
-keepclassmembers class * {
@org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

# If using AsyncExecutord, keep required constructor of default event used.
# Adjust the class name if a custom failure event type is used.
-keepclassmembers class org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}

-keep class org.greenrobot.eventbus.android.AndroidComponentsImpl

-keep class androidx.databinding.DataBindingComponent {*;}

#소스파일의 라인을 섞지 않는 옵션
#-keepattributes SourceFile,LineNumberTable

-keep class com.appg.hanwoori.data.** {*;}

위의 코드가 내가 이번에 작업한 proguard-rules.pro 파일의 내용이다.

프로가드파일을 어떻게 설정하는지 여기서 다 이야기할 수는 없고 간단한 사례들만 정리를 해보겠다.

우선, 가장 문제가 되는 것이 외부 라이브러리이다.

내가 작업한 프로젝트에서는 이벤트버스가 문제였다.

어떻게해야하나 삽질을 좀 했는데 이벤트버스 공식홈페이에 가보면 프로가드를 어떻게 설정해야하는지 친절히 알려준다.

이벤트버스 proguard 설정하는 법 자세히보기

나도 위 링크에 들어가서 복사 붙여넣기를 했을 뿐이다.

그리고 가장 삽질한 부분이 retrofit2와 관련된 내용이다.

retrofit2는 dependency를 추가하면 알아서 프로가드설정이 되어서 크게 문제는 없었다.

하지만 retrofit을 호출해서 그 데이터를 받는 부분이 문제가 되었다.

구글링을 좀 해보니까 data class를 만들어서 데이터를 받을 때, data class도 프로가드를 설정해줘야했다.

 -keep class com.appg.hanwoori.data.** {*;

위의 코드가 바로 그 내용이다.

이렇게해서 나의 첫번째 난독화 작업은 마무리가 되었다.....

프로가드를 설정하는 규칙에 대해서 궁금해할 것 같은데 솔직히 나도 아직 잘 모른다.

여러 블로그를 눈팅하면서 얻은 내용만 간단하게 정리해보겠다.

역시 자세한 내용은 공식문서를 참고해야한다.

proguard rule 공식문서 바로가기

대게

-keep class 패키지명.** : 해당 패키지 모든 클래스 난독화 제외

-keep interface 패키지명.** : 해당 패키지 모든 인터페이스 난독화 제외

이런식으로 진행이 된다.

간혹 블로그를 보면 *를 쓸 때도 있고 **를 쓸 때도 있는데 차이는 다음과 같다.

**를 쓰면 하위 폴더의 모든 패키지명까지 다 포함시킨다.

3. 난독화 에러로그 보기

난독화 에러

위의 캡처화면은 내가 난독화를 진행할 때 발생했던 로그다.

위 화면처럼 난독화를 적용하면 나의 코드가 암호화되어 나타난다.

그럼 위의 경우처럼 에러로그가 발생해서 앱이 종료되었을 때 어떻게 분석할 수 있을까?


안드로이드 mapping 파일

이럴 때 mapping.txt파일을 보면된다.

파일 탐색기를 "Project"로 설정해주고 app / build / outputs / mapping / release / mapping.txt 파일에 들어가면된다.

보안상 내가 맡은 프로젝트의 mapping.txt 파일을 일부만 공개하면 아래와 같다.

별거 아닐거라고 생각했던 난독화작업이 생각보다 쉽지않았다.

그래도 삽질을 통해서 난독화에 대해서 조금은 알 수가 있었다.

이 블로그를 보는 여러분들은 저와 같은 삽질을 조금이라도 줄일 수 있었으면 좋겠다.

Share:
Read More