我正在参加「启航方案」

前言

1、咱们都希望自己开发的 App 得到必定,一般的做法是在运用内预置一个弹窗,在适宜的机遇弹出,并引导用户去商铺点评。

2、App 开发中,运用内更新是必不可少的,国内常见的做法是在 App 中完结下载并装置,可是假如要上架 GooglePlay,这种做法会过不了审阅。

接下来咱们就运用 GooglePlay 运用内点评和运用内更新完结上面两个需求。

留意: GooglePlay 运用内点评和运用内更新只适用于 GooglePlay 上上架的 App,国内的运用商铺并不适用

一、GooglePlay 运用内点评

1.1、设备要求

1、需 5.0 或更高版别的 Android 设备

2、 Android 设备装置了 Google Play 商铺

1.2、Play Core 库要求

如需在你的运用中集成 GooglePlay 运用内点评,库版别有必要运用 1.8.0:

dependencies {
    //中心库
    implementation 'com.google.android.play:core:1.8.0'
    //项目运用了 Kotlin 能够集成
    implementation 'com.google.android.play:core-ktx:1.8.0'
}

或许更高版别的Play Core 库,现在最新版别如下:

dependencies {
    //中心库
    implementation 'com.google.android.play:review:2.0.1'
    //项目运用了 Kotlin 能够集成
    implementation 'com.google.android.play:review-ktx:2.0.1'
}

1.3、集成 GooglePlay 运用内点评

1.3.1、增加 Gradle 远程依靠

在 app 的 build.gradle 中增加如下依靠:

//中心库
implementation 'com.google.android.play:review:2.0.1'
//项目运用了 Kotlin 能够集成
implementation 'com.google.android.play:review-ktx:2.0.1'

1.3.2、初始化 GooglePlay 运用内点评

/**
  * 初始化 GooglePlay 运用内点评
  */
fun initInnerReview(){
   //1、经过 ReviewManagerFactory 创立 ReviewManager 目标,用于发动运用内点评的流程
   manager = ReviewManagerFactory.create(MyApplication.getInstance())
   //2、获取 ReviewInfo 目标。当咱们判别能够让用户进行点评时,运用 ReviewManager 创立一个请求,
   //用于真实发动运用内点评流程。这儿咱们能够先缓存好 ReviewInfo 目标。
   val request = manager?.requestReviewFlow()
   request?.addOnCompleteListener {
      if(it.isSuccessful){
          reviewInfo = it.result
          Log.d("erdai", "init: get reviewInfo sucess")
      }else{
          Log.d("erdai", "init: get reviewInfo failed")
      }
   }
}

1.3.3、发动 GooglePlay 运用内点评

/**
  * 发动运用内点评
  *
  * @param activity 其时 Activity
  */
fun innerReview(activity: Activity){
  //调用 launchReviewFlow 来发动点评流程,剩余的工作就交给 Google 了
  reviewInfo?.apply {
    val flow = manager?.launchReviewFlow(activity, this)
    flow?.addOnCompleteListener {
        if(it.isSuccessful){
            Log.d("erdai", "innerReview: launchReviewFlow success")
        }else{
            Log.d("erdai", "innerReview: launchReviewFlow failed")
        }
    }
  }
}

1.3.4、完整代码

咱们能够将 GooglePlay 运用内点评的代码进行封装,放到一个单例里边:

/**
 * function: GooglePlay 运用内 app 辅助类
 */
object GooglePlayInnerAppHelper {
    /**
     * ReviewInfo 目标
     */
    var reviewInfo: ReviewInfo? = null
    /**
     * ReviewManager 目标
     */
    var manager: ReviewManager? = null
    /**
     * 初始化 GooglePlay 运用内点评
     */
    fun initInnerReview(){
        //1、经过 ReviewManagerFactory 创立 ReviewManager 目标,用于发动运用内点评的流程
        manager = ReviewManagerFactory.create(MyApplication.getInstance())
        //2、获取ReviewInfo目标。当咱们判别能够让用户进行点评时,运用ReviewManager创立一个使命,
        //用于真实发动运用内点评流程。这儿谷歌文档中建议提前一点缓存好 ReviewInfo 目标。
        val request = manager?.requestReviewFlow()
        request?.addOnCompleteListener {
            if(it.isSuccessful){
                reviewInfo = it.result
                Log.d("erdai", "init: get reviewInfo sucess")
            }else{
                Log.d("erdai", "init: get reviewInfo failed")
            }
        }
    }
    /**
     * 运用内点评
     *
     * @param activity 其时 Activity
     */
    fun innerReview(activity: Activity){
        //调用 launchReviewFlow 来发动点评流程,剩余的工作就交给 Google 了
        reviewInfo?.apply {
            val flow = manager?.launchReviewFlow(activity, this)
            flow?.addOnCompleteListener {
                if(it.isSuccessful){
                    Log.d("erdai", "innerReview: launchReviewFlow success")
                }else{
                    Log.d("erdai", "innerReview: launchReviewFlow failed")
                }
            }
        }
    }
}

在适宜的机遇,以我的为比如:

在 Application 的 onCreate 里边初始化 GooglePlay 运用内点评,然后在项目的主页发动 GooglePlay 运用内点评。

进行调用即可。

1.4、测验

上述集成代码比较简单,主要仍是测验比较费事,测验的方法有多种,讲下我引荐的一种:

1、将你的运用打包成 .aab 格式的文件上传到 GooglePlay 的 Internal testing(内测)途径:

上传到 Internal testing 途径的好处便是只要你没有进行点评,那么每次都会进行点评弹窗提示。Production(出产)途径则不会。

2、将你的 GooglePlay 账号加入到内测途径中,然后从 GooglePlay 商铺中下载运用:

能够看到,GooglePlay 商铺会提示你是一个内测账号

留意: 这儿不能运用受保护的用户账号(如企业账号),我其时便是运用了企业分配的 GooglePlay 账号导致一向弹不出点评弹窗。需求改用 Gmail 账号,后边运用私家的 Gmail 账号,就成功的弹出了点评弹窗。

3、保证你运用的 GooglePlay 账号没有点评过该运用,假如点评了也弹不出来

1.5、作用展现

如下图:

二、GooglePlay 运用内更新

2.1、设备要求

1、需 5.0 或更高版别的 Android 设备

2、 Android 设备装置了 Google Play 商铺

2.2、Play Core 库要求

如需在你的运用中集成 GooglePlay 运用内点评,库版别有必要运用 1.8.0:

dependencies {
    //中心库
    implementation 'com.google.android.play:core:1.8.0'
    //项目运用了 Kotlin 能够集成
    implementation 'com.google.android.play:core-ktx:1.8.0'
}

或许更高版别的Play Core 库,现在最新版别如下:

dependencies {
    //中心库
    implementation 'com.google.android.play:app-update:2.0.1'
    //项目运用了 Kotlin 能够集成
    implementation 'com.google.android.play:app-update-ktx:2.0.1'
}

2.3、集成 GooglePlay 运用内更新

2.3.1、增加 Gradle 远程依靠

在 app 的 build.gradle 中增加如下依靠:

//中心库
implementation 'com.google.android.play:app-update:2.0.1'
//项目运用了 Kotlin 能够集成
implementation 'com.google.android.play:app-update-ktx:2.0.1'

2.3.2、发动运用内更新

GooglePlay 运用内更新方法有两种:

1、当即更新

2、灵敏更新

2.3.2.1、当即更新

当即更新你也能够理解为强制更新,它会打断用户与 app 的交互,弹出一个新的页面提示你晋级

代码完结:

/**
  * 运用内强制更新
  */
fun innerAppForceUpdate(activity: Activity) {
    val appUpdateManager = AppUpdateManagerFactory.create(MyApplication.getInstance())
    appUpdateManager.appUpdateInfo.addOnSuccessListener {
        if (it.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && 
           it.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
            //发动强制更新
            appUpdateManager.startUpdateFlowForResult(it,
                AppUpdateType.IMMEDIATE,
                activity,
                UPDATE_REQUEST_CODE)
         }
    }
}
2.3.2.2、灵敏更新

灵敏更新不会打断用户与 app 的交互,它是在后台静默下载新版别,等下在完了在提示更新

代码完结:

/**
 * 运用内灵敏更新
 */
fun innerAppSmartUpdate(activity: Activity) {
    val appUpdateManager = AppUpdateManagerFactory.create(MyApplication.getInstance())
    appUpdateManager.appUpdateInfo.addOnSuccessListener {
        if (it.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && 
            it.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
            //灵敏更新
            val installListener = object : InstallStateUpdatedListener {
                override fun onStateUpdate(installState: InstallState) {
                    if(installState.installStatus() == InstallStatus.DOWNLOADING){
                      	//下载中,咱们能够在此获取进展给用户进行提示
                      	//已下载文件字节巨细
                	val bytesDownloaded = installState.bytesDownloaded()
                	//文件总的字节巨细
                	val totalBytesToDownload = installState.totalBytesToDownload()
                      	//todo 关于户进行下载进展提示
                    } else if (installState.installStatus() == InstallStatus.DOWNLOADED) {
                        //告诉用户更新已下载完毕,重启以装置运用
                        appUpdateManager.unregisterListener(this)
                        appUpdateManager.completeUpdate()
                    }
                }
            }
            appUpdateManager.registerListener(installListener)
            appUpdateManager.startUpdateFlowForResult(it,
                AppUpdateType.FLEXIBLE,
                activity,
                UPDATE_REQUEST_CODE)
        }
    }
}

2.3.3、完整代码

同样的,咱们能够对其进行封装,放到一个单例里边:

/**
 * function: GooglePlay 运用内 app 辅助类
 */
object GooglePlayInnerAppHelper {
    const val UPDATE_REQUEST_CODE: Int = 0x001
    /**
     * 运用内强制更新
     */
    fun innerAppForceUpdate(activity: Activity) {
        val appUpdateManager = AppUpdateManagerFactory.create(MyApplication.getInstance())
        appUpdateManager.appUpdateInfo.addOnSuccessListener {
            if (it.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && it.isUpdateTypeAllowed(
                    AppUpdateType.IMMEDIATE)
            ) {
                //强制更新
                appUpdateManager.startUpdateFlowForResult(it,
                    AppUpdateType.IMMEDIATE,
                    activity,
                    UPDATE_REQUEST_CODE)
            }
        }
    }
    /**
     * 运用内灵敏更新
     */
    fun innerAppSmartUpdate(activity: Activity) {
        val appUpdateManager = AppUpdateManagerFactory.create(MyApplication.getInstance())
        appUpdateManager.appUpdateInfo.addOnSuccessListener {
            if (it.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&
                it.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
                //灵敏更新
                val installListener = object : InstallStateUpdatedListener {
                    override fun onStateUpdate(installState: InstallState) {
                        if(installState.installStatus() == InstallStatus.DOWNLOADING){
                            //下载中,咱们能够在此获取进展给用户进行提示
                            //已下载文件字节巨细
                            val bytesDownloaded = installState.bytesDownloaded()
                            //文件总的字节巨细
                            val totalBytesToDownload = installState.totalBytesToDownload()
                            //todo 关于户进行下载进展提示
                        } else if (installState.installStatus() == InstallStatus.DOWNLOADED) {
                            //告诉用户更新已下载完毕,重启以装置运用
                            appUpdateManager.unregisterListener(this)
                            appUpdateManager.completeUpdate()
                        }
                    }
                }
                appUpdateManager.registerListener(installListener)
                appUpdateManager.startUpdateFlowForResult(it,
                    AppUpdateType.FLEXIBLE,
                    activity,
                    UPDATE_REQUEST_CODE)
            }
        }
    }
}

在适宜的机遇,以我的为比如,当即更新和灵敏更新结合运用:

根据后台下发装备,判别是运用当即更新仍是灵敏更新

2.4、测验

咱们需求做以下三步:

1、打包高版别 .aab 格式的文件上传到 GooglePlay 的 Internal testing(内测)途径,将 GooglePlay 账号,增加到内测途径

留意: 此刻无受保护的用户账号要求,因而这儿你能够运用企业账号

2、保证你现在运用的账号至少从 GooglePlay 下载过一次运用,因为只有具有运用的用户帐号才能够运用运用内更新

3、复原到低版别,运用 AndroidStudio 编译打包即可

2.5、作用展现

2.5.1、后台下发装备当即更新

如下图:

当即更新给咱们新开了一个页面进行下载,等待下载完结会自动给你更新并重启运用。

2.5.2、后台下发装备灵敏更新

如下图:

灵敏更新会在后台静默下载,等下载完结会自动给你更新并重启运用。

三、总结

本篇文章咱们介绍了:

1、GooglePlay 运用内点评

2、GooglePlay 运用内更新

两者集成过程比较简单,主要便是测验作用比较费事,按照我文中的过程,问题不大。

好了,本篇文章到这儿就完毕了,希望能给你带来帮助

感谢你阅览这篇文章

参考和引荐

GooglePlay 运用内点评官方文档

GooglePlay 运用内更新官方文档

你的点赞,评论,是对我巨大的鼓舞!

欢迎重视我的大众号: sweetying ,文章更新可第一时间收到

假如有问题,大众号内有加我微信的进口,在技术学习、个人成长的道路上,咱们一起行进!