【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
}
}