【Android测试】【第三节】ADB——源码浅谈

本文简要探讨ADB作为手机与PC交互桥梁的角色,重点通过反编译ddmlib.jar来分析ADB源码。DDMS通过ddmlib.jar与ADB建立连接,借助jd-gui-0.3.3.windows.zip进行jar反编译,展示源码结构。在Java工程中引入ddmlib.jar和guava-15.0.jar,可以调用AndroidDebugBridge方法实现ADB操作,如获取IDEVICE对象以执行各种功能。
摘要由CSDN通过智能技术生成
前言


  由于本人精力平有限,所以这里简单的说说ADB源码。

  首先根据前面的理解,我们已经知道了ADB是“连接手机和PC的一个桥梁”,我们经常在PC端开发的时候,会用到eclipse这个工具,这里面有一个工具叫DDMS,如下图:

 

  是不是发现通过DDMS在PC端可以看到手机的一些信息,其实呢 它就是通过 “ddmlib.jar” 来建立起ADB的。因此我们今天就通过反编译 “ddmlib.jar” 来分析一下ADB源码。

 

反编译


  首先不得不吐槽一下百度经验的审核人员,我看到里面“反编译jar”的经验没有,于是呢我就写了一个提交了上去,结果提交了很多次,都给我打回了,真不知道这帮审核的人员是怎么想的,这种方便别人参考的内容难道不应该被通过吗?切

  好了,说正事吧。

  ddmlib.jar 放在 <SDk path>\tools\libs 的文件夹下。

  整个反编译的过程如下:

    1、下载jd-gui-0.3.3.windows.zip (我的微云链接:http://url.cn/Zz8sOj )

    2、解压之后打开,将要编译的jar导入:

    3、展开坐标的树形结构,就是源码啦

 

上源码


 

   1 /*
   2  * Copyright (C) 2007 The Android Open Source Project
   3  *
   4  * Licensed under the Apache License, Version 2.0 (the "License");
   5  * you may not use this file except in compliance with the License.
   6  * You may obtain a copy of the License at
   7  *
   8  *      http://www.apache.org/licenses/LICENSE-2.0
   9  *
  10  * Unless required by applicable law or agreed to in writing, software
  11  * distributed under the License is distributed on an "AS IS" BASIS,
  12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13  * See the License for the specific language governing permissions and
  14  * limitations under the License.
  15  */
  16 
  17 package org.athrun.ddmlib;
  18 
  19 
  20 import java.io.BufferedReader;
  21 import java.io.IOException;
  22 import java.io.InputStreamReader;
  23 import java.lang.Thread.State;
  24 import java.net.InetAddress;
  25 import java.net.InetSocketAddress;
  26 import java.net.UnknownHostException;
  27 import java.security.InvalidParameterException;
  28 import java.util.ArrayList;
  29 import java.util.Map;
  30 import java.util.regex.Matcher;
  31 import java.util.regex.Pattern;
  32 
  33 import org.athrun.ddmlib.Log.LogLevel;
  34 
  35 /**
  36  * A connection to the host-side android debug bridge (adb)
  37  * <p/>
  38  * This is the central point to communicate with any devices, emulators, or the
  39  * applications running on them.
  40  * <p/>
  41  * <b>{
    @link #init(boolean)} must be called before anything is done.</b>
  42  */
  43 public final class AndroidDebugBridge {
  44 
  45     /*
  46      * Minimum and maximum version of adb supported. This correspond to
  47      * ADB_SERVER_VERSION found in //device/tools/adb/adb.h
  48      */
  49 
  50     private final static int ADB_VERSION_MICRO_MIN = 20;
  51     private final static int ADB_VERSION_MICRO_MAX = -1;
  52 
  53     private final static Pattern sAdbVersion = Pattern
  54             .compile("^.*(\\d+)\\.(\\d+)\\.(\\d+)$"); //$NON-NLS-1$
  55 
  56     private final static String ADB = "adb"; //$NON-NLS-1$
  57     private final static String DDMS = "ddms"; //$NON-NLS-1$
  58     private final static String SERVER_PORT_ENV_VAR = "ANDROID_ADB_SERVER_PORT"; //$NON-NLS-1$
  59 
  60     // Where to find the ADB bridge.
  61     final static String ADB_HOST = "127.0.0.1"; //$NON-NLS-1$
  62     final static int ADB_PORT = 5037;
  63 
  64     private static InetAddress sHostAddr;
  65     private static InetSocketAddress sSocketAddr;
  66 
  67     private static AndroidDebugBridge sThis;
  68     private static boolean sInitialized = false;
  69     private static boolean sClientSupport;
  70 
  71     /** Full path to adb. */
  72     private String mAdbOsLocation = null;
  73 
  74     private boolean mVersionCheck;
  75 
  76     private boolean mStarted = false;
  77 
  78     private DeviceMonitor mDeviceMonitor;
  79 
  80     private final static ArrayList<IDebugBridgeChangeListener> sBridgeListeners = new ArrayList<IDebugBridgeChangeListener>();
  81     private final static ArrayList<IDeviceChangeListener> sDeviceListeners = new ArrayList<IDeviceChangeListener>();
  82     private final static ArrayList<IClientChangeListener> sClientListeners = new ArrayList<IClientChangeListener>();
  83 
  84     // lock object for synchronization
  85     private static final Object sLock = sBridgeListeners;
  86 
  87     /**
  88      * Classes which implement this interface provide a method that deals with
  89      * {
    @link AndroidDebugBridge} changes.
  90      */
  91     public interface IDebugBridgeChangeListener {
  92         /**
  93          * Sent when a new {
    @link AndroidDebugBridge} is connected.
  94          * <p/>
  95          * This is sent from a non UI thread.
  96          * 
  97          * @param bridge
  98          *            the new {
    @link AndroidDebugBridge} object.
  99          */
 100         public void bridgeChanged(AndroidDebugBridge bridge);
 101     }
 102 
 103     /**
 104      * Classes which implement this interface provide methods that deal with
 105      * {
    @link IDevice} addition, deletion, and changes.
 106      */
 107     public interface IDeviceChangeListener {
 108         /**
 109          * Sent when the a device is connected to the {
    @link AndroidDebugBridge}
 110          * .
 111          * <p/>
 112          * This is sent from a non UI thread.
 113          * 
 114          * @param device
 115          *            the new device.
 116          */
 117         public void deviceConnected(IDevice device);
 118 
 119         /**
 120          * Sent when the a device is connected to the {
    @link AndroidDebugBridge}
 121          * .
 122          * <p/>
 123          * This is sent from a non UI thread.
 124          * 
 125          * @param device
 126          *            the new device.
 127          */
 128         public void deviceDisconnected(IDevice device);
 129 
 130         /**
 131          * Sent when a device data changed, or when clients are
 132          * started/terminated on the device.
 133          * <p/>
 134          * This is sent from a non UI thread.
 135          * 
 136          * @param device
 137          *            the device that was updated.
 138          * @param changeMask
 139          *            the mask describing what changed. It can contain any of
 140          *            the following values: {
    @link IDevice#CHANGE_BUILD_INFO},
 141          *            {
    @link IDevice#CHANGE_STATE},
 142          *            {
    @link IDevice#CHANGE_CLIENT_LIST}
 143          */
 144         public void deviceChanged(IDevice device, int changeMask);
 145     }
 146 
 147     /**
 148      * Classes which implement this interface provide methods that deal with
 149      * {
    @link Client} changes.
 150      */
 151     public interface IClientChangeListener {
 152         /**
 153          * Sent when an existing client information changed.
 154          * <p/>
 155          * This is sent from a non UI thread.
 156          * 
 157          * @param client
 158          *            the updated client.
 159          * @param changeMask
 160          *            the bit mask describing the changed properties. It can
 161          *            contain any of the following values:
 162          *            {
    @link Client#CHANGE_INFO},
 163          *            {
    @link Client#CHANGE_DEBUGGER_STATUS},
 164          *            {
    @link Client#CHANGE_THREAD_MODE},
 165          *            {
    @link Client#CHANGE_THREAD_DATA},
 166          *            {
    @link Client#CHANGE_HEAP_MODE},
 167          *            {
    @link Client#CHANGE_HEAP_DATA},
 168          *            {
    @link Client#CHANGE_NATIVE_HEAP_DATA}
 169          */
 170         public void clientChanged(Client client, int changeMask);
 171     }
 172 
 173     /**
 174      * Initializes the <code>ddm</code> library.
 175      * <p/>
 176      * This must be called once <b>before</b> any call to
 177      * {
    @link #createBridge(String, boolean)}.
 178      * <p>
 179      * The library can be initialized in 2 ways:
 180      * <ul>
 181      * <li>Mode 1: <var>clientSupport</var> == <code>true</code>.<br>
 182      * The library monitors the devices and the applications running on them. It
 183      * will connect to each application, as a debugger of sort, to be able to
 184      * interact with them through JDWP packets.</li>
 185      * <li>Mode 2: <var>clientSupport</var> == <code>false</code>.<br>
 186      * The library only monitors devices. The applications are left untouched,
 187      * letting other tools built on <code>ddmlib</code> to connect a debugger to
 188      * them.</li>
 189      * </ul>
 190      * <p/>
 191      * <b>Only one tool can run in mode 1 at the same time.</b>
 192      * <p/>
 193      * Note that mode 1 does not prevent debugging of applications running on
 194      * devices. Mode 1 lets debuggers connect to <code>ddmlib</code> which acts
 195      * as a proxy between the debuggers and the applications to debug. See
 196      * {
    @link Client#getDebuggerListenPort()}.
 197      * <p/>
 198      * The preferences of <code>ddmlib</code> should also be initialized with
 199      * whatever default values were changed from the default values.
 200      * <p/>
 201      * When the application quits, {
    @link #terminate()} should be called.
 202      * 
 203      * @param clientSupport
 204      *            Indicates whether the library should enable the monitoring and
 205      *            interaction with applications running on the devices.
 206      * @see AndroidDebugBridge#createBridge(String, boolean)
 207      * @see DdmPreferences
 208      */
 209     public static synchronized void init(boolean clientSupport) {
 210         if (sInitialized) {
 211             throw new IllegalStateException(
 212                     "AndroidDebugBridge.init() has already been called.");
 213         }
 214         sInitialized = true;
 215         sClientSupport = clientSupport;
 216 
 217         // Determine port and instantiate socket address.
 218         initAdbSocketAddr();
 219 
 220         MonitorThread monitorThread = MonitorThread.createInstance();
 221         monitorThread.start();
 222 
 223         HandleHello.register(monitorThread);
 224         HandleAppName.register(monitorThread);
 225         HandleTest.register(monitorThread);
 226         HandleThread.register(monitorThread);
 227         HandleHeap.register(monitorThread);
 228         HandleWait.register(monitorThread);
 229         HandleProfiling.register(monitorThread);
 230         HandleNativeHeap.register(monitorThread);
 231     }
 232 
 233     /**
 234      * Terminates the ddm library. This must be called upon application
 235      * termination.
 236      */
 237     public static synchronized void terminate() {
 238         // kill the monitoring services
 239         if (sThis != null && sThis.mDeviceMonitor != null) {
 240             sThis.mDeviceMonitor.stop();
 241             sThis.mDeviceMonitor = null;
 242         }
 243 
 244         MonitorThread monitorThread = MonitorThread.getInstance();
 245         if
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值