예제코드는 https://github.com/susemi99/APKGenerateScriptSample 에 있다.
apk 서명에 관한 정보는 https://developer.android.com/studio/publish/app-signing?hl=ko 에 있다.
.keystore 넣기
.keystore 파일을 앱의 root 경로에 넣는다.
keystore.properties
파일에 비번 같은 정보를 넣어준다.
build.gradle 설정
app/build.gradle
에 keystore 정보를 사용해서 apk를 만들게 설정한다.
def keystorePropertiesFile = rootProject.file("keystore.properties") def keystoreProperties = new Properties() keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) android { signingConfigs { TestAppSigning { keyAlias keystoreProperties['keyAlias'] keyPassword keystoreProperties['keyPassword'] storeFile file(keystoreProperties['storeFile']) storePassword keystoreProperties['storePassword'] } } . . . buildTypes { debug{ signingConfig signingConfigs.TestAppSigning . . . } release { signingConfig signingConfigs.TestAppSigning . . . } } }
쉘 스크립트
script/make_apk.sh
파일을 실행하면 바탕화면에 `TestApp_v1.0.apk` 파일을 만들어준다.
#! /bin/bash RED=`tput setaf 1` # 빨간색 시작 RESET=`tput sgr0` # 원래 색으로 되돌리기 echo -e "${RED}@@@@@@@@@@@@@@@ 바탕화면에 apk 만들기 @@@@@@@@@@@@@@@${RESET}\n" cd "$(dirname "$0")" # 바로가기 아이콘으로 실행했을 때를 위해 make_apk.sh 파일이 있는 곳으로 이동. cd ../TestApp # 앱 경로로 이동 rm -rf app/build/outputs/apk/* # 기존의 apk 파일 모드 제거 # echo -e "${RED}############ Debug ############${RESET}\n" # bash ./gradlew assembleDebug # APK_FOLDER="app/build/outputs/apk/debug" echo -e "${RED}############ Release ############${RESET}\n" bash ./gradlew assembleRelease APK_FOLDER="app/build/outputs/apk/release" APP_VERSION=`sed -e 's/^.*"versionName":"\([^"]*\)".*$/\1/' ${APK_FOLDER}/output.json` # output.json 에서 versionName 가져오기 mv ${APK_FOLDER}/*.apk ~/Desktop/TestApp_v"${APP_VERSION}".apk # 바탕화면으로 파일 이동 read -rep "${RED}아무 키나 누르세요. 5초 뒤에 자동으로 닫힙니다.${RESET}"$'\n' -n1 -s -t 5 # 5초 후 터미널 닫기