Summary of Android adb command usage

Keywords: Android shell simulator calculator

First, write in front

1. I belong to a rookie now. Every time I encounter a problem or a bug, I open android studio and run the code. Then I start to find the last breakpoint of the activity or fragment or log to debug, which makes it slow and time-consuming to locate the problem.

2. Look at those bulls, hit the terminal directly, click a few commands immediately, locate the problem, fast, accurate positioning.

3. Accumulate a little, and record every command you learn.

Second, basic command learning

1, adb devices: Get device list and device status

2, adb get-state: Get the state of the device

Explain:

  • device: Set up a normal connection
  • offline: the device is abnormal and the device is not responding
  • unknown: no equipment

3, adb kill-server, adb start-server: End the adb service, start the adb service, usually two commands used together.

4, adb install: Install applications, override installation using the - r option.

adb uninstall: Uninstall the application, followed by the package name of the application. Please distinguish it from the apk file name.

Adb-s <serialNumber> install <path-to-apk>: Specify the android device to install the APK file. There are simulators and mobile phones, which are commonly used.

Adb-s < serialNumber > uninstall < pkg_name >: ibid., meaning uninstall software.

Supplement:

1) adb installation software, if both simulator and real machine.

parameter
Meaning
-d Specify the current unique Android device connected via USB as the command target
-e Specify the current uniquely running simulator as the command target
-s <serialNumber> Specify the device/simulator with the corresponding serialNumber number as the command target

2) Control the behavior of installing APK. ADB install-r/-s/-d

parameter
Meaning
-r Overlay installation is allowed.
-s Install the application to sdcard.
-d Allow degraded override installation.


5, adb pull: Copy files or folders on Android devices to local locations.

1
adb pull sdcard/pull.txt ~/Downloads / copy the pull.txt file under Sdcard to the ~ / Downloads directory.

adb push: Push local files to android devices.

6, adb reboot: Restart the Android device. The original command, ah, I used to turn off the simulator every time before, and then open, which will be convenient later, you can use the command directly.

3. Advanced Order Learning

1, adb shell command

The adb command is the command of the adb program, while the adb shell is the command of the Android system that is invoked. These Android-specific commands are placed in the system/bin directory of the Android device, for example, when I type such a command from the command line:

From the above display, it is clear that this command does not exist in the bin directory. So the above prompt already tells you where our adb shell commands are, so start the simulator, copy the entire system/bin directory, and try one by one.

So does the adb pull command mentioned above work? The operation is as follows:
 

Open the bin file as follows:

 

Then we open the am file and see what's in it:

#!/system/bin/sh
#
# Script to start "am" on the device, which has a very rudimentary
# shell.
#
base=/system
export CLASSPATH=$base/framework/am.jar
exec app_process $base/bin com.android.commands.am.Am "$@"
Take another look at the pm file:
# Script to start "pm" on the device, which has a very rudimentary
# shell.
#
base=/system
export CLASSPATH=$base/framework/pm.jar
exec app_process $base/bin com.android.commands.pm.Pm "$@" 


Let's take a look at the adb shell commands that are commonly used.

2, pm command: Package Manager, you can get some application information installed on Android devices. Source code of pm Pm.java , Running adb shell pm directly can get help information for this command.

1) pm list package: lists the applications installed in the equipment. (without any options: list package names for all applications)

xdhywj@xdhywjdeMBP ~ » adb shell                                          
root@vbox86p:/ # pm list package
package:com.example.firstapplication
package:com.awesomeproject
package:com.example.alertdialogcancel
package:com.example.android.livecubes
package:com.example.xdhywj.my_listactivity
package:com.android.providers.telephony
package:com.example.xdhywj.observertest
package:com.android.providers.calendar
package:com.android.providers.media
package:com.example.xdhywj.myapplication_test1
package:com.android.wallpapercropper
......

2) ADB shell PM list package-s: list system applications.

xdhywj@xdhywjdeMBP ~ » adb shell pm list packages -s
package:com.example.android.livecubes
package:com.android.providers.telephony
package:com.android.providers.calendar
package:com.android.providers.media
package:com.android.wallpapercropper
package:com.android.voicedialer
package:com.android.documentsui
package:com.android.galaxy4
package:com.android.externalstorage
package:com.android.htmlviewer
package:com.android.quicksearchbox
......

3) ADB shell PM list package-3: List third-party applications and find that not all of them are our own applications.
xdhywj@xdhywjdeMBP ~ » adb shell pm list packages -3
package:com.example.firstapplication
package:com.awesomeproject
package:com.example.alertdialogcancel
package:com.example.xdhywj.my_listactivity
package:com.example.xdhywj.observertest
package:com.example.xdhywj.myapplication_test1
package:com.sankuai.meituan
package:com.xdhywj.eventbusdemo
package:com.example.xdhywj.myspawn
package:com.xdhywj.myspawn
package:com.example.layoutinflatertest
package:com.example.xdhywj.servicetest
package:com.example.xdhywj.myview
package:com.example.spannabletext
......

4) ADB shell PM list package-f: list the application package name and the corresponding apk name and storage location.

xdhywj@xdhywjdeMBP ~ » adb shell pm list packages -f  
package:/data/app/com.example.firstapplication-2/base.apk=com.example.firstapplication
package:/data/app/com.awesomeproject-1/base.apk=com.awesomeproject
package:/data/app/com.example.alertdialogcancel-2/base.apk=com.example.alertdialogcancel
package:/system/app/CubeLiveWallpapers/CubeLiveWallpapers.apk=com.example.android.livecubes
package:/data/app/com.example.xdhywj.my_listactivity-2/base.apk=com.example.xdhywj.my_listactivity
package:/system/priv-app/TelephonyProvider/TelephonyProvider.apk=com.android.providers.telephony
package:/data/app/com.example.xdhywj.observertest-1/base.apk=com.example.xdhywj.observertest
package:/system/priv-app/CalendarProvider/CalendarProvider.apk=com.android.providers.calendar
......
So can we list the apk names and storage locations for third-party applications? The parameter is -3-f.

xdhywj@xdhywjdeMBP ~ » adb shell pm list packages -3 -f
package:/data/app/com.example.firstapplication-2/base.apk=com.example.firstapplication
package:/data/app/com.awesomeproject-1/base.apk=com.awesomeproject
package:/data/app/com.example.alertdialogcancel-2/base.apk=com.example.alertdialogcancel
package:/data/app/com.example.xdhywj.my_listactivity-2/base.apk=com.example.xdhywj.my_listactivity
package:/data/app/com.example.xdhywj.observertest-1/base.apk=com.example.xdhywj.observertest
package:/data/app/com.example.xdhywj.myapplication_test1-1/base.apk=com.example.xdhywj.myapplication_test1
package:/data/app/com.sankuai.meituan-2/base.apk=com.sankuai.meituan
......
5) ADB shell PM list package-i: list the application package name and its installation source, the results show examples (command last add FILTER: filter keywords, you can easily find the application you want)

xdhywj@xdhywjdeMBP ~ » adb shell pm list packages -i  
package:com.example.firstapplication  installer=null
package:com.awesomeproject  installer=null
package:com.example.alertdialogcancel  installer=null
package:com.example.android.livecubes  installer=null
package:com.example.xdhywj.my_listactivity  installer=null
package:com.android.providers.telephony  installer=null
package:com.example.xdhywj.observertest  installer=null
......

6) ADB shell PM list package-f-3-i meituan: find out the package name, apk storage location and installation source of the tripartite application. Two were found: one was the American League and the other was the Qin Emperor.

xdhywj@xdhywjdeMBP ~ » adb shell pm list package -f -3 meituan
package:/data/app/com.sankuai.meituan-2/base.apk=com.sankuai.meituan
package:/data/app/com.meituan.spawn-1/base.apk=com.meituan.spawn


7) pm path: Lists the. apk location for the corresponding package name. For example:

xdhywj@xdhywjdeMBP ~ » adb shell pm path com.sankuai.meituan
package:/data/app/com.sankuai.meituan-2/base.apk

8) pm dump: List the dump information for the specified application, which contains various information. For example:
xdhywj@xdhywjdeMBP ~ » adb shell pm dump com.sankuai.meituan
DUMP OF SERVICE package:
  Activity Resolver Table:
    Schemes:
        imeituan:
          26ac82 com.sankuai.meituan/com.meituan.android.travel.poi.TravelPoiListActivity
          57b03b com.sankuai.meituan/.topic.TopicActivity
          71af37 com.sankuai.meituan/.survey.SurveyActivity
          8112f9 com.sankuai.meituan/.review.KtvReviewActivity
          a90794 com.sankuai.meituan/com.meituan.android.travel.dealdetail.schedule.ScheduleDetailActivity
          e77dc4 com.sankuai.meituan/com.meituan.android.hotel.prepay.PrePayOrderCreateActivity
......
9)pm install/pm uninstall: Install / uninstall applications, where apk files are stored on Android devices, not used with adb install/uninstall.
3. am command: another huge command, am Source code Am.java . This command must be learned well. We will use it frequently. It can be started from the cmd console. Activities, services; send broadcast s, etc.

Emphasis: ADB shell a m start-n package n am E / package name + class name (- n class name, - a action,-d date,-m MIME-TYPE,-c category,-e extended data, etc.)

1) am start-n: am start-n package name / package name. {activity name}. For example, start the system's own calculator.

xdhywj@xdhywjdeMBP ~ » adb shell am start -n com.android.calculator2/com.android.calculator2.Calculator
Starting: Intent { cmp=com.android.calculator2/.Calculator }
#Or it could be
xdhywj@xdhywjdeMBP ~ » adb shell am start -n com.android.calculator2/.Calculator
Starting: Intent { cmp=com.android.calculator2/.Calculator }
#Start System Calendar
xdhywj@xdhywjdeMBP ~ » adb shell am start -n com.android.calendar/.LaunchActivity
Starting: Intent { cmp=com.android.calendar/.LaunchActivity }
#Start the browser
xdhywj@xdhywjdeMBP ~ » adb shell am start -n com.android.browser/com.android.browser.BrowserActivity
Starting: Intent { cmp=com.android.browser/.BrowserActivity }
......
#Open the Qin Emperor
xdhywj@xdhywjdeMBP ~ » adb shell am start -n com.meituan.spawn/.MainActivity
Starting: Intent { cmp=com.meituan.spawn/.MainActivity }
#Open the Welcome Interface of the Beauty Corps
xdhywj@xdhywjdeMBP ~ » adb shell am start -n com.package.name/.activity.Welcome
Starting: Intent { cmp=com.com.package.name/.activity.Welcome }
#Open the Home Page of the Beauty Corps
xdhywj@xdhywjdeMBP ~ » adb shell am start -n com.package.name/.activity.MainActivity
Starting: Intent { cmp=com.com.package.name/.activity.MainActivity }
2)am start -a: -a Represents action (android.intent.action.MAIN). Here is an example:
#Open the map interface in the vicinity of the American League by specifying action through-a
xdhywj@xdhywjdeMBP ~ » adb shell am start -a com.meituan.android.intent.action.near_poi_map -n com.sankuai.meituan/.common.map.NearPoiMap
Starting: Intent { act=com.meituan.android.intent.action.near_poi_map cmp=com.sankuai.meituan/.common.map.NearPoiMap }

3) am start-d:-d denotes data (android.intent.data). Here are a few examples:

First, take a look at Activeness with data attributes in the Manifest.xml file, as follows:

//Login interface:
<activity
    android:name="com.meituan.passport.LoginActivity"
    android:configChanges="orientation|keyboardHidden"
    android:screenOrientation="portrait"
    android:theme="@style/App.Passport">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:host="www.meituan.com"
            android:path="/signin"
            android:scheme="imeituan" />
    </intent-filter>
    <intent-filter>
    <action android:name="com.meituan.android.intent.action.login" />
    <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
  
//Urban Selection Interface
<activity
    android:name=".city.CityActivity"
    android:configChanges="orientation|keyboardHidden"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="adjustPan|stateHidden">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:host="www.meituan.com"
            android:path="/city/list"
            android:scheme="imeituan" />
    </intent-filter>
</activity>

Launching mode of landing interface:
#Open the login interface through data
xdhywj@xdhywjdeMBP ~ » adb shell am start -d imeituan://com.package.name/signin
Starting: Intent { dat=imeituan://www.meituan.com/signin }
  
#Open Store City Selection Interface through data
xdhywj@xdhywjdeMBP ~ » adb shell am start -d imeituan://www.meituan.com/city/list
Starting: Intent { dat=imeituan://www.meituan.com/city/list }
4) am start-e: When opening activity, take the extension parameter. Similar to extra: key - value.  
#It's equivalent to intent with extra s. The key and value correspond to the key value in intent, is it very convenient? Praise one
xdhywj@xdhywjdeMBP ~ » adb shell am start -d imeituan://www.meituan.com/city/list -e key value                  
Starting: Intent { dat=imeituan://www.meituan.com/city/list (has extras) }
  
#Jump to Payment Result Page
xdhywj@xdhywjdeMBP ~ » adb shell am start -n "com.com.package.name/.pay.PayResultActivity" -e bigOrderId 6782909
Starting: Intent { cmp=com.com.package.name/.pay.PayResultActivity (has extras) }
So when we pass intent, do we often use putStringExtra()? Put IntExtra, then how should we handle this type of extra? In fact, adb has already been designed for us. Is it very good?
putStringExtra():[-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
putIntExtra():[-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
putBooleanExtra():[--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
......
#Example: Call putIntExtra(): where key is bigOrderId and value is int.
am start -n "com.sankuai.meituan/.pay.PayResultActivity" --ei "bigOrderId" 6782909

4. Start Services

Pull it out separately, all of the above are start activities, in order not to confuse, start services separately.

1) Start a service, which runs in the background, so you can go to the list of applications. Parameter: - n represents the component. - a denotes action.

#Among them - a is the corresponding action, - ei:putIntExtra("level",3),-ei:putIntExtra("scale",100).
adb shell am startservice -n com.android.music/com.android.music.MediaPlaybackService
adb shell am startservice -a com.meituan.myservice
#Result
Starting service: Intent { cmp=com.android.music/.MediaPlaybackService }
2) Stop service. Take the configuration in the Android Manifest file as an example:
<service android:name="com.some.package.name.YourServiceSubClassName" android:permission="com.some.package.name.YourServiceSubClassName">
    <intent-filter>
        <action android:name="com.some.package.name.YourServiceSubClassName"/>
    </intent-filter>
</service>
Start service and stop service:
#Start service
adb shell am startservice com.some.package.name/.YourServiceSubClassName#Stop serviceadb shell am force-stop com.some.package.name

5. Start Broadcast

Also pull it out alone.

Send a system broadcast to simulate the low power environment of mobile phones

#Among them - a is the corresponding action, - ei:putIntExtra("level",3),-ei:putIntExtra("scale",100).
adb shell am broadcast -a android.intent.action.BATTERY_CHANGED --ei "level" 3 --ei "scale" 100

Fourth, check the size and density of mobile phones

1. Use adb to view cell phone size.

2. Use adb to check cell phone density

5. Clearing Application Data and Caching

6. Forced cessation of application

7. Check the Front Desk Activity



Posted by strangebeer on Thu, 04 Jul 2019 12:48:12 -0700