[Setting | macOS] React Native Android Release 배포

mac에서 하이브리드앱을 빌드하고 배포하는 과정을 정리한다.


작성일 : 2021-08-22
OS : macOS Big Sur 버전 11.5
CPU : Intel

1> KeyStore 생성

cd {UserHome}/{AppPath}/android/app
keytool -genkey -v -keystore opendocs_music-release-key.keystore -alias opendocs_music_key -keyalg RSA -keysize 2048 -validity 10000

2> 빌드설정

android/gradle.properties 파일에 환경변수 설정

OPENDOCS_MUSIC_RELEASE_STORE_FILE=opendocs_music-release-key.keystore
OPENDOCS_MUSIC_RELEASE_KEY_ALIAS=opendocs_music_key
OPENDOCS_MUSIC_RELEASE_STORE_PASSWORD=*****
OPENDOCS_MUSIC_RELEASE_KEY_PASSWORD=*****

android/app/build.gradle 파일에 release 환경 설정

android {
...
   signingConfigs {
      debug {...}
      release {
            if (project.hasProperty('OPENDOCS_MUSIC_RELEASE_STORE_FILE')) {
                storeFile file(OPENDOCS_MUSIC_RELEASE_STORE_FILE)
                storePassword OPENDOCS_MUSIC_RELEASE_STORE_PASSWORD
                keyAlias OPENDOCS_MUSIC_RELEASE_KEY_ALIAS
                keyPassword OPENDOCS_MUSIC_RELEASE_KEY_PASSWORD
            }
      }
   }
   buildTypes {
      debug {...}
      release {
            ...
            signingConfig signingConfigs.release
      }
   }

3> Android 빌드

cd {UserHome}/{AppPath}/android
./gradlew clean
./gradlew cleanBuildCache
./gradlew assembleRelease
# permission denied 에러가 발생할 경우
chmod +x gradlew

4> APK 확인

아래폴더에 apk 파일을 핸드폰에 옮겨 실행한다.

{UserHome}/{AppPath}/android/app/build/outputs/apk/release

5> Fail Case

다음 유형으로 배포실패시 해결 방안을 정리한다.

5-1> > Task :app:mergeReleaseResource FAILED / Error: Duplicate resource

> Task :app:mergeReleaseResources FAILED
[drawable-xhdpi-v4/node_modules_reactnavigation_stack_src_views_assets_backicon] /Users/opendocs/_Work/_Development/source/opendocs/mobile/android/app/src/main/res/drawable-xhdpi/node_modules_reactnavigation_stack_src_views_assets_backicon.png [drawable-xhdpi-v4/node_modules_reactnavigation_stack_src_views_assets_backicon] /Users/opendocs/_Work/_Development/source/opendocs/mobile/android/app/build/generated/res/react/release/drawable-xhdpi/node_modules_reactnavigation_stack_src_views_assets_backicon.png: Resource and asset merger: Duplicate resources

아래 두 폴더에 리소스가 중복되어 나타나는 현상이다.

① /android/app/build/generated/res/react/release/

② /android/app/src/main/res/

① 폴더에 있는 파일들을 ② 폴더에서 삭제하도록 한다. 검색해보면 ② 폴더에 drawable* 를 삭제하라고 하지만 이와같이 할경우 생성된 Splash & Icon 이미지 등이 같이 삭제되어 버리니 불편하더라도 확인하여 삭제하도록 한다.

5-2> Task :app:mergeReleaseResource FAILED / Error: Found item Style/SplashScreenTheme more than one time

> Task :app:mergeReleaseResources FAILED
/Users/opendocs/_Work/_Development/source/opendocs/mobile/android/app/src/main/res/values/styles.xml: Resource and asset merger: Found item Style/SplashScreenTheme more than one time

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeReleaseResources'.
> /Users/opendocs/_Work/_Development/source/opendocs/mobile/android/app/src/main/res/values/styles.xml: Error: Found item Style/SplashScreenTheme more than one time

react-native-splash-screen을 사용해 Splash Screen 자동 생성 하였을 경우 발생한 에러이다.
최초 생성시에는 발생하지 않으나 수정을 위해 자동 생성 명령을 재시도 하였다면 styles.xml 파일에 아래 와 같이 중복된 내용이 추가 된다. 삭제하고 다시 시도하자.