플레이 스토어에서 apk 혹은 aab 를 업로드한 뒤, 사용자에게 앱의 버전이 업데이트 되었음을 알리고, 업데이트하도록 독려하는 UX 를 구현하는 것은 그리 어려운 일은 아닙니다. 하지만, 이를 구현하는 것은 약간 귀찮은 일임에는 틀림없습니다. 앱이 플레이스토어에 업로드 된 앱 버전 이름 혹은 코드을 체크한 뒤, 현재의 앱 버전 이름 혹은 코드와 비교하여 일정한 차이를 보일 때,(혹은 항상) 업데이트를 유도하는 UX 를 추가하여야 합니다. 예전에는 별도의 서버 API 를 사용하는 경우도 있었고, 그 때 그 때 플레이스토어를 크롤링하는 방법을 사용하는 경우도 있었습니다.

 

그런데, 이제 플레이 스토어 라이브러리를 이용하면 되기 때문에, 그런 번거로움은 생각하지 않아도 됩니다. 개발자 사이트의 참조 주소는 developer.android.com/guide/playcore/in-app-updates 입니다.

 

1. gradle dependency 를 설정합니다.

dependencies {
...
	implementation 'com.google.android.play:core:1.8.0'
	implementation 'com.google.android.play:core-ktx:1.8.1'
...
}

 

주의해야할 점은 VERSION.SDK_INT >= 21 을 체크하셔야 합니다. 체크하지 않아도 앱이 죽지는 않지만 UX 를 해칠 수 있습니다.

그리고, 플레이 스토어가 설치되어 있지 않은 에뮬레이터에서는 당연히 동작하지 않습니다. 또한, 이런 속성이 있기 때문에, 플레이 스토어에 게재(Publish)된 apk 버전보다 versionCode 를 낮게 설정해놓아야 동작을 확인할 수 있습니다.

 

2. 추가할 코드

 

// Creates instance of the manager.
val appUpdateManager = AppUpdateManagerFactory.create(context)

// Returns an intent object that you use to check for an update.
val appUpdateInfoTask = appUpdateManager.appUpdateInfo

// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
    if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
        // For a flexible update, use AppUpdateType.FLEXIBLE
        && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
    ) {
        // Request the update.
        appUpdateManager.startUpdateFlowForResult(
        // Pass the intent that is returned by 'getAppUpdateInfo()'.
        appUpdateInfo,
        // Or 'AppUpdateType.FLEXIBLE' for flexible updates.
        AppUpdateType.IMMEDIATE,
        // The current activity making the update request.
        this,
        // Include a request code to later monitor this update request.
        1)        
    }
}

 

appUpdateInfo 로 부터 최신 VersionCode 를 가져올 수 있습니다. 이 값과 현재 앱 버전을 비교하여, update 버튼을 활성화시키고, 버튼 클릭에 따라, startUpdateFlowForResult 를 호출해주시면 적절한 update UX 를 구현할 수 있습니다.


appUpdateManager.appUpdateInfo.addOnSuccessListener { appUpdateInfo ->
	val versionCode = appUpdateInfo.availableVersionCode()
}

 

-------

(아직 작성 중인 글입니다.)

 

 

'Lonely Developer' 카테고리의 다른 글

Github Actions 에서 cron 설정  (0) 2023.08.15
Google Firebase : TestLab  (0) 2021.06.02
Android Studio - Image Asset Tool  (0) 2020.03.13
나 홀로 안드로이드 앱 개발 - Color Tool  (0) 2020.03.11