java – Installing applications without the participation of the android user

Question:

How to programmatically install/update an application?

Android 4.4.2

The program that will install applications will be system (system / app) and will be signed with the system key.

Tried this option https://github.com/paulononaka/Android-InstallInBackgroundSample doesn't work. Gives an error message

RuntimeException("Stub!")

when initializing packageManager

Answer:

As a result, I wrote the following code:

fun install(filePath: String) {
        var inputStream: InputStream? = null
        try { 
            val sb = StringBuilder()
            sb.append("pm install -r ")
            sb.append(filePath)
            val proc = Runtime.getRuntime().exec(sb.toString())
            inputStream = proc.inputStream
            val bufferedReader = BufferedReader(InputStreamReader(inputStream))
            var line: String
            val log = StringBuilder()
            while (bufferedReader.readLine().also { line = it } != null) {
                log.append(line + "\n")
            }
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
    }
Scroll to Top