adb 自动检测无线安装apk

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

import javax.swing.plaf.TextUI;

public class InstallApk {

    static String fileName;

    static String mainIntent;
    static List<String>ips;
    static List<String>macs;
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

//      if (args == null || args.length < 1) {
//          System.out.println("please input file name");
//          return;
//      }

//      fileName = args[0];
        if (args.length == 2) {
            mainIntent = args[1];
        }
        fileName="xxxxxxx.apk";
        mainIntent="xxx/.xxx.MainActivity";
        System.out.println("file dir"+System.getProperty("user.dir"));
        try {
            //-----no need----
//          InetAddress addr = InetAddress.getLocalHost();
//          String ip = addr.getHostAddress();
//          ip = ip.substring(0, ip.lastIndexOf("."));
            //------
            getIpsAndMacs();
            ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(5);
            if(ips!=null&&ips.size()>0){
                for(int i=0;i<ips.size();i++){
                    final String ip=ips.get(i);
                    final String mac=macs.get(i);
                    newFixedThreadPool.execute(new Runnable() {
                        @Override
                        public void run() {
                            connect(ip,mac);
                        }
                    });
//                  new ScanThread(i).start();
                }
            }else{
                System.out.println("get ip fail,please retry");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//  static class ScanThread extends Thread {
//      int i;
//      public ScanThread(int index) {
//          i=index;
//      }
//
//      @Override
//      public void run() {
//          System.out.println("start install, wait a moment");
//              try {
//                  System.out.println("scan ip: " + ips.get(i));
//                  String ip=ips.get(i);
//                  Socket ss = new Socket();
//                  ss.connect(new InetSocketAddress(ip, 5555), 1000);
//                  System.out.println("find ip success: " + ip);
//                  ss.close();
//
//                  String ret = runCmd("adb connect " + ip);
//                  System.out.println(ret);
//                  install(i, System.getProperty("user.dir") + "\\"+ fileName);
////                    new InstallThread(i, System.getProperty("user.dir") + "\\"
////                            + fileName).start();
//              } catch (IOException e) {
//              }
//          System.out.println("over");
//
//      }
//  }

    private static String runCmd(String cmd) {
        try {
            System.out.println(cmd);
            Process pro = Runtime.getRuntime().exec("cmd /c " + cmd);
            InputStreamReader isr = new InputStreamReader(pro.getInputStream());
            BufferedReader br = new BufferedReader(isr);
            StringBuilder builder = new StringBuilder();
            String str;
            while ((str = br.readLine()) != null) {
                builder.append(str + "\n");
            }
            br.close();
            isr.close();
            System.out.println("runcmd"+builder.toString());
            return builder.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "not good";
    }
//private static void install(int index,String mApkPath){
//  String mIp=ips.get(index);
//  System.out.println("uninstall on ip:"+ mIp+" waiting...");
//  try{
//      String ret1 = runCmd("adb -s " + mIp + ":5555 uninstall "
//              + mainIntent.subSequence(0, mainIntent.indexOf("/")));
//      System.out.println("uninstall "+ret1);
//  }catch(Exception e){
//      System.out.println("mainIntent null--");
//  }
//  System.out.println("install on ip: " + mIp+" waiting...");
//  String ret = runCmd("adb -s " + mIp + ":5555 install -r "
//          + mApkPath);
//
//  if (ret != null && ret.indexOf("Success") != -1) {
//      System.err.println("install success: " + mIp + " mac: "
//              + macs.get(index));
//
//      try {
//          Thread.sleep(1000);
//      } catch (InterruptedException e) {
//          e.printStackTrace();
//      }
//
//      if (mainIntent != null) {
//          runCmd("adb -s " + mIp + ":5555 shell am start "
//                  + mainIntent);
//          System.out.println("open app success---------"+mIp+" mac "+macs.get(index));
//      }
//
//  } else {
//      System.out.println(ret);
//
//  }
//
//  runCmd("adb disconnect " + mIp);
//}
    private static void uninstall(String ip,String mac){
        System.out.println("uninstall on ip:"+ ip+" waiting...");
        try{
            String ret1 = runCmd("adb -s " + ip + ":5555 uninstall "
                    + mainIntent.subSequence(0, mainIntent.indexOf("/")));
            System.out.println("uninstall "+ip+ret1);
            install(ip,mac);
        }catch(Exception e){
            System.out.println("uninstall "+ip+"fail");
        }
    }
    private static void connect(String ip,String mac){

            System.out.println("scan ip: " + ip);
            Socket ss = new Socket();
            try {
                ss.connect(new InetSocketAddress(ip, 5555), 1000);
                System.out.println("find ip success: " + ip);
                ss.close();
                String ret = runCmd("adb connect " + ip);
                System.out.println(ret);
                uninstall(ip,mac);
            } catch (IOException e) {
            }
            System.out.println("over");

    }
        private static void install(String ip,String mac){
                System.out.println("install on ip: " + ip+" waiting...");
                String cmd = runCmd("adb -s " + ip + ":5555 install -r "
                        + System.getProperty("user.dir") + "\\"+ fileName);

                if (cmd != null && cmd.indexOf("Success") != -1) {
                    System.err.println("install success: " + ip + " mac: "
                            + mac);
//
//                  try {
//                      Thread.sleep(1000);
//                  } catch (InterruptedException e) {
//                      e.printStackTrace();
//                  }

                    if (mainIntent != null) {
                        runCmd("adb -s " + ip + ":5555 shell am start "
                                + mainIntent);
                        System.out.println("open app success---------"+ip+" mac "+mac);
                    }

                } else {
                    System.out.println(cmd);

                }

                runCmd("adb disconnect " + ip);
    //          new InstallThread(i, System.getProperty("user.dir") + "\\"
    //                  + fileName).start();
            } 
//  static class InstallThread extends Thread {
//      private String mIp;
//      private String mApkPath;
//      private int mIndex;
//
//      public InstallThread(int i, String apkPath) {
//          this.mIp = ips.get(i);
//          this.mApkPath = apkPath;
//          this.mIndex=i;
//      }
//
//      @Override
//      public void run() {
//          
//          String ret = runCmd("adb -s " + mIp + ":5555 install -r "
//                  + mApkPath);
//
//          if (ret != null && ret.indexOf("Success") != -1) {
//              System.err.println("install success: " + mIp + " mac: "
//                      + macs.get(mIndex));
//
////                try {
////                    Thread.sleep(1000);
////                } catch (InterruptedException e) {
////                    e.printStackTrace();
////                }
//
//              if (mainIntent != null) {
//                  runCmd("adb -s " + mIp + ":5555 shell am start "
//                          + mainIntent);
//                  System.out.println("open app success---------"+mIp+" mac "+macs.get(mIndex));
//              }
//
//          } else {
//              System.out.println(ret);
//
//          }
//
//          runCmd("adb disconnect " + mIp);
//
//      }
//  }

//  public static String getMac(String ip) {
//      try {
//          String str = runCmd("arp -a");
//          if (str != null) {
//              if (str.indexOf(ip) > 1) {
//                  return str.substring(str.indexOf(ip) + 20,
//                          str.indexOf(ip) + 40).trim();
//              }
//
//          }
//      } catch (Exception e) {
//      }
//      return null;
//  }
    public static void getIpsAndMacs() {
        try {
            Process pro = Runtime.getRuntime().exec("cmd /c "+"arp -a|findstr 动态");
            InputStreamReader is=new InputStreamReader(pro.getInputStream());
            BufferedReader reader=new BufferedReader(new InputStreamReader(pro.getInputStream()));
            String str;
            ips=new ArrayList<String>();
            macs=new ArrayList<String>();
            while ((str = reader.readLine()) != null) {
                    ips.add(str.subSequence(0, 20).toString().trim());
                    macs.add(str.subSequence(20, 45).toString().trim());
            }
            reader.close();
            is.close();
            System.out.println("ips"+ips.toString());
            System.out.println("macs"+macs.toString());

        } catch (Exception e) {
        }
    }

}

install.bat

@echo off
color 2
java -jar installApk.jar "xxxxxxxxxx.apk" "xxxx/.xxxxxx.MainActivity"
pause

还存在的问题是:检测IP有的检测不到,估计有缓存;
有的时候执行命令后会卡住,没找到原因。
先记录下,以后解决吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值