When the user runs the Monkeyrunner command at the command line to execute the test script, the ADB server may not be up yet, one of the main functions of the Androiddebugbridge class is to open a new process to start the ADB server. This allows our test scripts to send commands to the ADB server to drive the target device, such as installing or deleting the installation package for the application under test.
Monkeyrunner involves a series of calls and relates to different classes to do different things during the boot process.
Figure 8-3-1 The class relationships involved in starting Androiddebugbridge
The above class diagram lists the relationships of the key classes involved in starting Androiddebugbridge, as well as the key member methods and member variables that are designed for each class during the startup process, which we describe first before entering code analysis:
- Monkeyrunnerstarter: This class we have encountered in the previous section, it is monkeyrunner the jar package of the entry class. It has an options object that we have already analyzed. Our main analysis in this section is a series of calls initiated from the Monkeyrunnerstarter constructor to target Androiddebugbridge. It instantiates the Chimpchat class and saves the instance through the Monkeyrunner Setchimpchat method, which is stored in the Chimpchat member variable of the Monkeyrunner. What's the use of saving it? You should notice that the Monkeyrunner,chimpchat and Adbbackend classes have waitforconnection, which is usually called at the very beginning of our test script, The goal is to get a high-level abstract device object called Adbchimpdevice, which we'll describe in detail in section 6th, "Launching Monkey". Here we mainly explain that Monkeyrunner saved the Chimpchat instance, its Waitforconnection method can directly call Chimpchat class Waitforconnection method through the Chimpchat object.
- Chimpchat: The main function of this class is to combine the Adbbackend class to implement the creation of Androiddebugbridge. Monkeyrunnerstarter relies on this class to create an instance of the Adbbackend class and save the instance to the Mbackend member variable of the class. The purpose of the preservation is the same as the purpose of the above Monkeyrunner combination chimpchat, Thus its Waitforconnection method can invoke the Waitforconnection method of Adbbackend class by mbackend this object.
- Adbbackend: Responsible for creating Androiddebugbridge instances. It creates an instance of Androiddebugbridge when it instantiates itself and saves the instance to the member variable in bridge. This way, as long as the class with the Adbbackend instance is able to get the latest device list for Androiddebugbridge maintenance through bridge. For example, Adbbackend calls the Bridge.getdevices method when implementing the Waitforconnection method to obtain a list of all connected devices, and then uses the device serial number to locate the target device to connect
- Androiddebugbridge: This class has two important functions, one is to start the ADB server, and the other is to start the device monitoring thread devicemonitor. 2nd we will explain in the next section, the point of this section is to analyze the 1th to see how it started the ADB server. The member variables and member methods of the class that are listed are mainly related to starting the ADB server, such as the default_adb_host and Default_adb_port variables that specify the address and port that the ADB server will listen to by default. The Getadblaunchcommand method is to obtain the appropriate ADB start or Stop command string to start the ADB server (create a new process "adb start-server") or stop the ADB server ("adb kill-server")
Below we through the source code analysis to explain the principle. After processing the command-line arguments through the previous section, the next step in the Monkeyrunner Portal main function is to try to invoke the constructor of Monkeyrunnerstarter based on these parameters:
178 public static void Main (string[] args) {179 monkeyrunneroptions options = Monkeyrunneroptions.processoptions (args); 181 if (options = = null) {182 return;183 }184 185 186 Replacealllogformatters (Monkeyformatter.default_instance, Options.getloglevel ()); 187 188 MonkeyRunnerStarter Runner = new Monkeyrunnerstarter (options), 189 int error = Runner.run (); 191 192 system.exit (error); 193 }194}
Code 8-3-1 initialization Monkeyrunnerstarter
The options parameter is the Monkeyrunneroptions object created in the last section, based on all parameters, which holds all the parameters. Down we go into the Monkeyrunnerstarter constructor:
monkeyrunnerstarter (monkeyrunneroptions options) (map<string) , string> chimp_ options = new TreeMap (); chimp_options.put ("Backend", Options.getbackendname ()); this.options = options; this.chimp = Chimpchat.getinstance (chimp_options); Monkeyrunner.setchimpchat (this.chimp);
Code 8-3-2 monkeyrunnerstarter Constructors
Just a few lines of code from this method we can see what it actually does is to initialize the chimpchat according to ' backend ' and save the instance to Monkeyrunnerstarter's Chimp member variable, It also calls Monkeyrunner's static method Setchimpchat the Chimpchat object to the static member variable of the Monkeyrunner, why is it bound to be a static member variable? Because the 61st row saves the instance by calling a method of the Monkeyrunner class, not an instance, the method is definitely static, and the member function inside a static method is bound to be static. Everyone jumps in and monkeyrunner this class to see:
Wuyi static void Setchimpchat (Chimpchat chimp) ( chimpchat = chimp;
Code 8-3-3 monkeyrunner-setchimpchat
We return to continue to see how Chimpchat is started, first we monkeyrunnerstarter the 58th line of the constructor Optionsgetbackendname () is how to obtain the name of backend, From the previous section of the command line parameter analysis we can know that it is used by default ' adb ', so it is ' adb ', or other user-specified backend (in fact this situation does not support, continue to analyze we will be clear).
After obtaining the name of the backend, a 60-line chimpchat.getinstance is called to instantiate the Chimpchat:
getinstance public static chimpchat (map<string, string> options), { sadblocation = (String) Options.get ("Adblocation"); snoinitadb = boolean.valueof ((String) options.get ("noinitadb")). Booleanvalue (); Ichimpbackend Backend = Createbackendbyname ((String) options.get ("backend"); if (backend = = null) {n = null ; chimpchat chimpchat = new Chimpchat (backend); Turn chimpchat; *
Code 8-3-4 chimpchat-getinstance
The Chimpchat instantiation has two points, which is what we focus on in this section:
- Creating a backend based on backend's name is actually creating a androiddebugbridge-adb
- Call the constructor to save the backend to the Chimchat member variable.
Down we continue to see Chimpchat Androiddebugbridge how this backend was created, we go to the 51 line called Createbackendbyname this function:
ichimpbackend createbackendbyname (String backendname) ( "adb". Equals ( Backendname) { adbbackend return new (Sadblocation, SNOINITADB), and the return null;
Code 8-3-5 chimpchat-createbackendbyname
Here is the 77th line, this is why I said backend actually only support ' adb ', at least the temporary code is this way, if Google decides to support other updated backend in the future, it is another matter. This is still possible, after all, Google has left this interface.
adbbackend Public (String adblocation, Boolean noinitadb) !noinitadb {this.initadb = (60<); c4/>61 if (adblocation = = null) { this.initadb adblocation = findadb (); Ndroiddebugbridge.init (FALSE); This.bridge = Androiddebugbridge.createbridge (Adblocation, true);
Code 8-3-6 adbbackend Constructors
Before we create the Androiddebugbridge we need to determine the location of our ADB program, which is achieved through 62 lines, and we go in and findadb to see how it finds the ADB in our SDK:
the findadb of the private string () (), the string mrparentlocation = System.getproperty (" Com.android.monkeyrunner.bindir "); ( mrparentlocation! = null) && (Mrparentlocation.length ()! = 0)) Bayi { platformtools file = new file (new file (mrparentlocation). GetParent (), "Platform-tools"); C12/>if (Platformtools.isdirectory ()) {$ return Platformtools.getabsolutepath () + File.separator + SDKCONSTANTS.FN_ADB; The mrparentlocation + file.separator + sdkconstants.fn_adb; return sdkconstants.fn_adb; *
Code 8-3-7 adbbackend-findadb
First it finds the value of the property "Com.android.monkeyrunner.bindir" by looking up the system in the JVM. Remember that the previous section of the runtime environment initialization in Monkeyrunner this shell script is how it through the Java-D parameter to save this value into the JVM it? In fact it is the path to the bin (shell) file in your file system that holds the SDK Monkeyrunner, on my machine is "com.android.monkeyrunner.bindir:/users/apple/develop/ Sdk/tools ".
Find this path after the 82nd line of code to get its parent directory, that is, the SDK directory, plus the ' platform-tools ' this subdirectory, and then through 85 or 88 this line plus the name of the ADB, here the FN_ADB is the name of the ADB, Under Windows, an '. exe ' becomes ' adb.exe ', and the class Linux system is just ' adb '. In my machine, it's "users/apple/develop/sdk/platform-tools/adb."
OK, after finding the ADB's path, Adbbackend's constructor will call Androiddebugbridge's Createbridge static method according to this parameter:
265 public static Androiddebugbridge Createbridge () ... 271 Try 272 {273 sthis = new Androiddebugbridge (); 274 Sthis.start (); 275 } catch ( Invalidparameterexception e) {276 sthis = null; 277 } ... 297 return sthis; 298 } 299 }
Code 8-3-8 Androiddebugbridge-createbridge
The No. 273 line of Androiddebugbridge's constructor is to instantiate the androiddebugbridge,adb that really starts up is to call the 274-line start () This member method:
713 Boolean start () 714 {715 if (this.madboslocation! = null) && (Sadbserverport! = 0) && (( !this.mversioncheck) | | (!STARTADB ()))) {716 return false; 717 } 718 719 this.mstarted = true; 720 721 722 this.mdevicemonitor = New Devicemonitor (this); 723 This.mDeviceMonitor.start (); 724 725 return true; 726 }
Code 8-3-9 Androiddebugbridge-start
Here are a few important things to do:
- 715 row startadb: Open Androiddebugbridge
- 722-723 Line: Initialize the Android device monitor and start the Devicemonitor device monitoring thread.
In this section we'll look at the first startadb and see how it opens up Androiddebugbridge, and 2nd we'll describe in the next section.
943 synchronized Boolean startadb () 944 {945 if (this.madboslocation = = null) {946 LOG.E ("adb", "cannot s Tart adb when Androiddebugbridge was created without the location of the ADB. "); 947 948 return false; 949} 950 951 if (Sadbserverport = = 0) {952 LOG.W ("adb", "ADB server port for starting Androiddebugbridge is not set. "); 953 return false; 954} 955 956 957 int status =-1; 958 959 string[] Command = Getadblaunchcommand ("Start-server"); 960 String CommandString = Joiner.on (', '). Join (command); 961 try {962 log.d ("Ddms", String.Format ("Launching '%1 $ S ' to ensure ADB is running.", new object[] {COMMANDST Ring})); 963 Processbuilder processbuilder = new Processbuilder (command); 964 if (Ddmpreferences.getuseadbhost ()) {965 String adbhostvalue = Ddmpreferences.getadbhostvalue (); 966 if ((Adbhostvalue! = null) && (!adbhostvalue.isempty ())) 967 {968 map<string, string> env = Processbuilder.environment (); 969 Env.put ("Adbhost", Adbhostvalue); 970} 971} 972 Process proc = Processbuilder.start (); 973 974 arraylist<string> erroroutput = new ArrayList (); 975 arraylist<string> stdoutput = new ArrayList (); 976 status = Grabprocessoutput (proc, Erroroutput, Stdoutput, false); 977} catch (IOException IoE) {978 log.e ("Ddms", "unable to run ' adb ':" + ioe.getmessage ()); 979} 980 catch (Interruptedexception IE) {981 log.e ("Ddms", "unable to run ' adb ':" + ie.getmessage ()); 982} 983 984 985 if (status! = 0) {986 log.e ("Ddms", String.Format ("'%1 $ S ' failed--run manually if Necessary ", new object[] {commandstring})); 987 988 return false; 989} 990 LOG.D ("Ddms", String.Format ("'%1 $ S ' succeeded", new object[] {commandstring})); 991 return true; 992}Code 8-3-10 androiddebugbridge-startadb
Here's what you're doing:
- Ready to start the command string for DB server
- The ADB server specified by the Processbuilder startup command string
- Error handling
The command string is implemented by a 959-line getadblaunchercommand (' Start-server '):
994 Private string[] Getadblaunchcommand (String option) 995 {996 list<string> command = new ArrayList (4); 997 Command.add (this.madboslocation); 998 if (sadbserverport! = 5037) {999 command.add ("-P"); Command.add (integer.tostring (sadbserverport)) 1001 }1002 command.add (option); 1003 return (string[]) Command.toarray (New String[command.size ()]); 1004 }
Code 8-3-11 Androiddebugbridge-getadblaunchcommand
The whole function is to play the string combination, the last string is "Adb-p $port start-server", that is, the command line to open the ADB server string, and eventually break the string into a string array to return. Note here that the port default is the default listener port 5037 for the ADB server.
Startadb method The next step is to call the Java Processbuilder constructor to create an ADB server process after the command is obtained. Once created, the process can be started by 972 lines of ' Processbuilder.start () '.
So far the Androiddebugbridge starts the function of start () 1th "1". Startup Androiddebugbridge "is complete, the ADB server process is already running.
Note: More articles please pay attention to the public number: Techgogogo or personal blog http://techgogogo.com. Of course, you are also very welcome to pick up directly (ZHUBAITIAN1). This article by Heaven Zhuhai Branch Rudder original. Reproduced please consciously, whether the complaint rights to look at the mood.
8th Chapter 3 "Monkeyrunner Source Analysis" Monkeyrunner start run process-start Androiddebugbridge