安装APK 免输入vivo、oppo密码

2020-06-03补充 最优化版本

GitHub demo地址

apk下载地址

  1. 新建一个APP,
    • 使用Accessibility功能 监听vivo的 安装引用 界面
    • 用户输入 vivo密码,后续 自动填写用
  2. 安装应用弹出时
    1. 用Accessiblity功能 主动输入密码
    2. 模拟点击 安装、打开 按钮

----------- -----------下面的老方法可以不看了----------- -----------

2018/08/20 优化

不足

之前写的要复制到model的build.gradle内,如果你有4、5个项目,每次都得复制,很糟糕的解决方案

解决
  1. vivo安装单独写一个脚本vivo.gradle
  2. 放到工作空间下(我的是C://WorkSpace)
  3. 然后每个项目 只要在model的build.gradle中导入(apply from: ‘C:\WorkSpace\vivo.gradle’)即可

额外改动
给task加一个group:‘custom’ 就不需要去 gradle视图的 other分组找了

代码
File getNewestAPK1(File it, File newestCreateFile) {
    println "is called ${it.path} exists: ${it.exists()}"

    if (it.isDirectory()) {
        it.listFiles().each {
            newestCreateFile = getNewestAPK1(it, newestCreateFile)
        }
    } else {
        newestCreateFile = doGetNewestAPK(it, newestCreateFile)
    }

    newestCreateFile
}

File doGetNewestAPK(File it, File newestCreateFile) {

    if (it.name.endsWith(".apk")) {
        if (newestCreateFile == null || !newestCreateFile.exists()) {
            newestCreateFile = it
            println "assign value"
        } else if (it.lastModified() > newestCreateFile.lastModified()) {
            newestCreateFile = it
            println "newestCreateFile is ${it.name}"
        }
    }
    newestCreateFile
}


task installSdcard(group:'custom', dependsOn: ':app:assembleDebug') {
    File apkDir = file("build/outputs/apk")
    if (apkDir != null && apkDir.exists()) {
        apkDir.deleteDir()
    }

    doLast {
        File newestCreateFile = getNewestAPK1(apkDir, null)
        if (newestCreateFile == null) {
            println "Apk file not exist"
            return
        }

        println "APK file exit, path:${newestCreateFile.path}"

        "adb shell mkdir /sdcard/apk"
        //"adb shell rm /sdcard/apk/app-debug.apk"
        "adb push ${newestCreateFile.path} /sdcard/apk/app-debug.apk".execute().waitForProcessOutput()
        'adb shell am start -a "android.intent.action.VIEW" -d "file:///mnt/sdcard/apk/app-debug.apk" -t "application/vnd.android.package-archive"'.execute().waitForProcessOutput()
        println "install task finish"
    }
}

打包路径不是固定的,使用了productFlavors的会导致多一个目录,所以我才用的是model相对目录 build/outputs/apk 下的最新改动的APK。就解决了 APK名称、路径不一致的问题


Context

vivo、oppo,固然是出于安全考虑,安装应用要输密码。但实现安全的途径有很多,这种方式 对开发者非常不友好

方法:

  1. gradle中编写一个task
  2. 用adb命令将apk push到sdcard上
  3. 然后调用adb shell调起安装程序

在sdcard上安装apk不用输入密码

前期准备

  1. adb命令已经加入到path里

示例中的目录结构

Project/app(运行的model)
Project/app/build.gradle (在这增加task)

注意
需换成你自己的变量有:

  • apk名称

运行

AndroidStudio右侧—>Gradle projects栏---->项目model名称----》Tasks-----》other分组—》installSdcard这个task 双击运行

代码:

task installSdcard (dependsOn: ':app:assembleDebug') {
    def apkName = "app-debug.apk" //记得修改成你的APK名称,这个是默认的debug名称

    def apkoutput = file("build/outputs/apk/debug/${apkName}")
    def isApkExist = apkoutput.exists()
    if (isApkExist) {
        apkoutput.delete()
    }

    doLast{
        if (!apkoutput.exists()) {
            println "Apk file not exist, PATH:${apkoutput.path}"
            return
        }

        "adb shell mkdir /sdcard/apk"
        "adb shell rm /sdcard/apk/app-debug.apk "
        "adb push ${apkoutput.path} /sdcard/apk/app-debug.apk".execute().waitForProcessOutput()
        'adb shell am start -a "android.intent.action.VIEW" -d "file:///mnt/sdcard/apk/app-debug.apk" -t "application/vnd.android.package-archive"'.execute().waitForProcessOutput()
        println "install task finish"
    }
}

你可能感兴趣的:(Android)