android UiAutomator如何利用adb install和adb uninstall实现安装和卸载

这篇博客介绍了如何利用Java编程结合adb命令,通过UiAutomator实现Android应用的自动安装和卸载。作者创建了一个UpdateApp类,包含install()和uninstall()方法,分别用于执行adb install和adb uninstall命令。此外,还提供了一个execCmd()方法来执行命令并捕获输出,同时将输出保存到log文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本人在看android UiAutomator快速调试的代码时,突然想到一个问题,既然能实现运行cmd命令,那写一个能实现快速调试安装和卸载的方法就挺好的,方便快捷,一键实现自己想要的功能。经过不断尝试,基本需求实现了。分享出来,如果不对的地方,还请指正。

在包下新建一个UpdateApp的类,里面的代码如下:

package student;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


import android.widget.VideoView;


public class UpdateApp {
public static void main(String[] args) {
String packagename="", filename="";
new UpdateApp(packagename, filename, false);


}
//通过key控制卸载安装还是直接卸载
public UpdateApp(String classname, String filename, boolean key) {
if (key) {
uninstall(classname);
install(filename);
}else{
uninstall(classname);
}

}
//安装
public void install(String filename) {
execCmd("cmd /c adb install c:\\users\\user\\Desktop\\"+filename+".apk");
System.out.println("安装成功!");
}
//卸载
public void uninstall(String classname) {
execCmd("cmd /c adb uninstall "+classname);
System.out.println("卸载成功!");
}
public void execCmd(String cmd) {
System.out.println("----execCmd:  " + cmd);
try {
Process p = Runtime.getRuntime().exec(cmd);
// 正确输出流
InputStream input = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = "";
while ((line = reader.readLine()) != null) {
//因为安装过程中,会不断输出发送百分比,这里就只输出“Success”
if(line.equalsIgnoreCase("Success")){
System.out.println(line);
}
//虽然上一部屏蔽了一些信息,但还是会写入log文件中
saveToFile(line, "runlog.log", false);
}
// 错误输出流,这里就不屏蔽任何错误信息了
InputStream errorInput = p.getErrorStream();
BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorInput));
String eline = "";
while ((eline = errorReader.readLine()) != null) {
System.out.println(eline);
saveToFile(eline, "runlog.log", false);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void saveToFile(String text, String path, boolean isClose) {
File file = new File("runlog.log");
BufferedWriter bf = null;
try {
FileOutputStream outputStream = new FileOutputStream(file, true);
OutputStreamWriter outWriter = new OutputStreamWriter(outputStream);
bf = new BufferedWriter(outWriter);
bf.append(text);
bf.newLine();
bf.flush();


if (isClose) {
bf.close();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}
}


在我的测试类中添加一个main方法即可:

public static void main(String[] args){
//最后一个参数,true表示先卸载再安装,false表示只卸载
new UpdateApp("com.dianzhi.student", "1", true);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值