【gradle】编译完成直接安装和打开的指定页面的task

【gradle】编译完成直接安装和打开的指定页面的task


督促自己学习总结,特用文章的形式记录下来,共同进步

安装和打开方法

在Gradle方法中调用就可以

/**
 * 安装和打开apk的方法
 */
void installAndOpen() {
    println "InstallAndOpen"
    def isWindows = System.properties['os.name'].contains('windows') || System.properties['os.name'].contains('Windows')
    println("System is " + System.properties['os.name'] + "  is Windows =" + isWindows)
    def defaultApkPath = ""
    if (isWindows) {//Windows系统
        defaultApkPath = rootDir.getAbsolutePath() + "\xx\xx\xx\xx\windows下apk的路径"
    } else {//Linux系统
        defaultApkPath = rootDir.getAbsolutePath() + "/xxp/xx/xx/xx/Linux下apk的路径"
    }
    println defaultApkPath
    def apkFiles = new File(defaultApkPath)
    println apkFiles.getClass()
    //记录最新的文件修改时间
    Long lastModifiedTime = Long.MIN_VALUE
    def apkPath = ""
    if (apkFiles.isDirectory()) {
        apkFiles.listFiles().toList().forEach { file ->
            if (file.isFile() && file.name.endsWith("apk")) {
                if (file.lastModified() > lastModifiedTime) {
                    lastModifiedTime = file.lastModified()
                    apkPath = file.path
                }
            }
        }
    }
    if (apkPath.length() == 0) {
        return
    }
    //安装apk adb install 命令
    def command = "adb install -r -d ${apkPath}"
    println command
    def result = command.execute()
    def resultText = result.text
    println resultText
    if (resultText.contains("Success")) {
        //启动apk 指定要启动的Activity啦
        def startCommand = "adb shell am start 包名/类名"
        println startCommand
        def startResult = startCommand.execute()
        println startResult.text
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值