Setting Up ProGuard Obfuscation in a React Native App
This guide explains how to configure code obfuscation for your React Native app using ProGuard. ProGuard shrinks, optimizes, and obfuscates your code, making it harder to reverse-engineer and significantly enhancing your app's security.
1. Configure the build.gradle File
First, add the following configuration to your android/app/build.gradle file:
release {
// For production, you must generate your own keystore file.
// For more details, see: https://reactnative.dev/docs/signed-apk-android
signingConfig signingConfigs.release
minifyEnabled true
proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
}
signingConfig: The configuration for app signing. For production releases, you must generate a separate, secure keystore file.minifyEnabled: Setting this option totrueenables code optimization and ProGuard obfuscation.proguardFiles: Specifies the path to the ProGuard configuration files. This includes the default Android optimization rules and your project-specific rules.
2. Configure the proguard-rules.pro File
To prevent essential classes from being broken by obfuscation, add the following exception rules to your android/app/proguard-rules.pro file:
# React Native Base Rules
-keep class com.facebook.react.** { *; }
-keep class com.facebook.hermes.** { *; }
-keep class com.facebook.proguard.** { *; }
# Keep Classes Implementing the Serializable Interface
-keep class * implements java.io.Serializable { *; }
# Keep Native Infrastructure Classes
-keep class * extends android.app.Application { *; }
-keep class com.facebook.react. { *; }: Excludes core React Native classes from obfuscation to ensure the framework runs properly.-keep class * implements java.io.Serializable { *; }: Prevents obfuscation errors on classes that implement serialization.-keep class * extends android.app.Application { *; }: Protects the custom Application class to ensure the app initializes correctly.
3. Conclusion
By applying these configurations, you can bolster your React Native app's security while ensuring that critical classes function as intended. After enabling ProGuard, make sure to thoroughly test your release build to verify that everything works seamlessly in a production environment.
React Native 앱에서 ProGuard 난독화 설정
이 글에서는 ProGuard를 사용해 React Native 앱의 코드 난독화 작업을 설정하는 방법에 대해 설명합니다. ProGuard는 코드를 압축하고 최적화하며, 읽기 어려운 형태로 난독화하여 앱의 보안을 강화할 수 있습니다.
1. build.gradle 파일 설정
먼저 android/app/build.gradle 파일에 다음 설정을 추가해야 합니다:
release {
// 실제 배포 환경에서는 자신만의 keystore 파일을 생성해야 합니다.
// 자세한 내용은 https://reactnative.dev/docs/signed-apk-android 참고
signingConfig signingConfigs.release
minifyEnabled true
proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
}
- signingConfig: 앱 서명을 위한 설정입니다. 실제 배포 시에는 별도의 keystore 파일을 생성해야 합니다.
- minifyEnabled: 이 옵션을
true로 설정하면 코드 최적화 및 난독화가 활성화됩니다. - proguardFiles: ProGuard 설정 파일의 경로를 지정합니다. 기본 설정 파일과 프로젝트 전용 규칙 파일이 포함됩니다.
2. proguard-rules.pro 파일 설정
난독화 예외 처리를 위해 proguard-rules.pro 파일에 다음 규칙을 추가합니다:
# React Native 기본 설정
-keep class com.facebook.react.** { *; }
-keep class com.facebook.hermes.** { *; }
-keep class com.facebook.proguard.** { *; }
# Serializable 인터페이스 구현 클래스 유지
-keep class * implements java.io.Serializable { *; }
# 네이티브 라이브러리 예외 처리
-keep class * extends android.app.Application { *; }
- -keep class com.facebook.react.** { *; }: React Native의 핵심 클래스들을 난독화에서 제외합니다.
- -keep class * implements java.io.Serializable { *; }: 직렬화를 사용하는 클래스들은 난독화 시 오류가 발생할 수 있어 예외 처리합니다.
- -keep class * extends android.app.Application { *; }: 커스텀 Application 클래스를 보호해 앱이 정상적으로 동작하도록 합니다.
3. 마무리
이 설정을 통해 React Native 앱의 보안을 강화하면서도 주요 클래스들이 올바르게 동작하도록 관리할 수 있습니다. 난독화 설정을 적용한 후에는 반드시 충분히 테스트를 진행해 앱이 정상적으로 작동하는지 확인해야 합니다.
댓글
댓글 쓰기