2

The Story Now I am working on a project where an Android application is running on a custom device and this app is not installed through the Play Store, but the APK file is manually uploaded, installed and launched on the device.

The problem here is when new version is released we have to update the application manually on all devices.

We want to automate this process and build process like this:

  • Install the app
  • Launch the app
  • Start a background process which to check for a new version in out Server
  • Download the new APK file
  • Uninstall the current app / Update the app
  • Install the new app / Update the app

The Questions:

Can we uninstall and install APK file programmatically?

Shell we use the existing app for this job or create a new android app which to handle the new version checking and update the app in the background?

4

3 回答 3

0

一个自动化的 ADB 脚本可以工作,也许与 Python 脚本配对?您需要触发 APK 的侧载,然后在完成后卸载

于 2019-08-29T11:26:28.033 回答
0

无需卸载 APK,而是将更新的 APK 文件放在您的服务器上,然后将其下载到您的应用程序中。

使用以下功能安装应用程序

fun install(context: Context, packageName: String, apkPath: String) {

// PackageManager provides an instance of PackageInstaller
val packageInstaller = context.packageManager.packageInstaller

// Prepare params for installing one APK file with MODE_FULL_INSTALL
// We could use MODE_INHERIT_EXISTING to install multiple split APKs
val params = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
params.setAppPackageName(packageName)

// Get a PackageInstaller.Session for performing the actual update
val sessionId = packageInstaller.createSession(params)
val session = packageInstaller.openSession(sessionId)

// Copy APK file bytes into OutputStream provided by install Session
val out = session.openWrite(packageName, 0, -1)
val fis = File(apkPath).inputStream()
fis.copyTo(out)
session.fsync(out)
out.close()

// The app gets killed after installation session commit
session.commit(PendingIntent.getBroadcast(context, sessionId,
        Intent("android.intent.action.MAIN"), 0).intentSender)
}

更新后重启应用

class UpdateReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {

    // Restart your app here
    val i = Intent(context, MainActivity::class.java)
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    context.startActivity(i)        
}
}

下面的链接描述了后台静默更新应用程序的完整过程

https://www.sisik.eu/blog/android/dev-admin/update-app

于 2019-08-29T11:38:13.583 回答
-2

您不能以编程方式从设备本身自动安装 APK。

  • pm如果设备已使用 shell 脚本和命令进行 root,您可以自动执行该过程
  • 您可以USB Debugging在所有设备上启用并编写 adb 脚本以使用计算机安装 apk
  • 您可以拥有另一个应用程序,该应用程序仅检查新 APK 并下载并启动安装 APK 的意图(奥利奥及以上需要明确许可),然后让用户点击Install按钮

但是,您走错了路,因为上面提到的所有方法都非常繁琐。因为 PlayStore 完全符合您的指定要求。我强烈建议您在 Playstore 上发布。

于 2019-08-29T11:39:19.060 回答