본문 바로가기
📱 Android

[Android] 프로가드(Proguard)에 대해

by DEV_DAON 2023. 11. 29.

오늘은 난독화 도구인 프로가드에 대해 알아보도록 하겠다!

 

Proguard란?

자바 기반 코드를 난독화해 주는 도구로 난독화, 용량 축소, 코드 축소, 최적화 등의 기능을 제공하여 안드로이드 애플리케이션 기본 보안 수단으로 구축할 필요성이 있다.

 

사용 이유

  • 코드 난독화
    - 코드를 읽기 어렵게 만드는 작업
    - 디컴파일시 소스 코드를 보호할 수 있다.
    - 클래스와 멤버 이름을 줄여 DEX 파일 크기를 줄입니다.
  • 앱 용량 줄이기
    - 불필요한 메서드 제거
    - 소스를 컴파일하게 되면 Dex파일이 생성되고 하나의 Dex파일은 65536개의 메서드를 참고할 수 있다.이를 초과하게 되면 여러 개의 Dex파일(=MultiDex)이 생성되고 이는 빌드 속도를 느리게 하고 용량이 커지게 된다.
  • 리소스 축소
    - 앱 라이브러리 종속 항목의 미사용 리소스를 포함하여 패키징된 앱에서 사용하지 않는 리소스를 삭제합니다.
  • 최적화
    - 코드를 검사하고 다시 작성하여 앱 DEX 파일의 크기를 더 줄입니다.

설정 방법

  • 프로젝트 수준의 build.gradle
android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}

 

  • 추가 설정(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

// 이곳에 원하는 난독화 방식을 작성해주면 된다.

 

난독화 옵션

- keepattributes SourceFile,LineNumberTable
: 소스 파일의 라인을 섞지 않는 옵션 (stacktrace를 통해 어느 line에서 오류가 난 것인지 확인)

- renamesourcefileattribute SourceFile
: 소스 파일 변수 명 바꾸는 옵션 (보통 라이브러리는 딱히 난독화할 필요 없을 때 이렇게 적어준다.)

- keep class 라이브러리패키지명.* { ; }
: 난독화 필요 없는 파일

- ignorewarnings
: 경고 무시

- dontwarn 패키지명.**
: 지정한 패키지의 경고 무시

dontskipnonpubliclibraryclasses, dontskipnonpubliclibraryclassmembers, dontoptimize...

 

단점

  • 잘못된 구성의 경우, 애플리케이션 충돌 가능성이 있다.
  • 완벽한 보안은 아니다.
  • 안드로이드 전용으로 설계된 것이 아닌, 자바 바이트코드를 최적화해주는 도구.
  • 산술 및 논리 식을 난독화하지는 않는다.