yunshuipiao / Potato

Read the fucking source code for the Android interview
Apache License 2.0
80 stars 12 forks source link

Android Adb Shell Command Record #62

Open yunshuipiao opened 5 years ago

yunshuipiao commented 5 years ago

Android Adb Shell Command Record

[TOC]

本篇文章用于收集一些 Android 开发中常用的 Adb 命令。

当前 Activity

在接触一个陌生项目中,经常需要知道当前的 Activity 的信息。那么可以使用命令:

adb shell dumpsys activity top, 可以的输出内容如下:(内容很多)

SW-MacBook-Pro:other zp$ adb shell dumpsys activity top
// 当前任务
TASK com.ziipin.softkeyboard.uzbekistan id=3486
    // 当前 Activity
  ACTIVITY com.ziipin.softkeyboard.uzbekistan/com.ziipin.setting.FeedActivity 6f615b pid=10300
    Local Activity 7553594 State:
    // 状态
      mResumed=true mStopped=false mFinished=false
      mChangingConfigurations=false
            // 配置信息
      mCurrentConfig={1.0 ?mcc?mnc zh_CN ldltr sw360dp w360dp h616dp 480dpi nrml long port finger -keyb/v/h -nav/h s.5 themeResource=null}
      mLoadersStarted=true
      Active Fragments in 1b4b297:
        #0: ReportFragment{243c84 #0 android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag}
          mFragmentId=#0 mContainerId=#0 mTag=android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag
          mState=5 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
          mAdded=true mRemoving=false mResumed=true mFromLayout=false mInLayout=false
          mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
          mRetainInstance=false mRetaining=false mUserVisibleHint=true
          mFragmentManager=FragmentManager{1b4b297 in HostCallbacks{bf62c6d}}
          mHost=android.app.Activity$HostCallbacks@bf62c6d
          Child FragmentManager{2a850a2 in ReportFragment{243c84}}:
            FragmentManager misc state:
              mHost=android.app.Activity$HostCallbacks@bf62c6d
              mContainer=android.app.Fragment$1@987e133
              mParent=ReportFragment{243c84 #0 android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag}
              mCurState=5 mStateSaved=false mDestroyed=false
      Added Fragments:
        #0: ReportFragment{243c84 #0 android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag}
      FragmentManager misc state:
        mHost=android.app.Activity$HostCallbacks@bf62c6d
        mContainer=android.app.Activity$HostCallbacks@bf62c6d
        mCurState=5 mStateSaved=false mDestroyed=false
    ViewRoot:
      mAdded=true mRemoved=false
      mConsumeBatchedInputScheduled=false
      mConsumeBatchedInputImmediatelyScheduled=false
      mPendingInputEventCount=0
      mProcessInputEventsScheduled=false
      mTraversalScheduled=false      mIsAmbientMode=false
      android.view.ViewRootImpl$NativePreImeInputStage: mQueueLength=0
      android.view.ViewRootImpl$ImeInputStage: mQueueLength=0
      android.view.ViewRootImpl$NativePostImeInputStage: mQueueLength=0
    Choreographer:
      mFrameScheduled=false
      mLastFrameTime=257936331 (165 ms ago)
    View Hierarchy:
            // 布局信息
      com.android.internal.policy.PhoneWindow$DecorView{226c4bf V.E...... R....... 0,0-1080,1920}
        android.widget.LinearLayout{52e278c V.E...... ........ 0,0-1080,1920}
          android.view.ViewStub{5ea9cf0 G.E...... ......I. 0,0-0,0 #10203b7 android:id/action_mode_bar_stub}
          android.widget.FrameLayout{6fbaed5 V.E...... ........ 0,72-1080,1920}
            android.support.v7.widget.FitWindowsLinearLayout{62c86ea V.E...... ........ 0,0-1080,1848 #7f090012 app:id/action_bar_root}
              android.support.v7.widget.ViewStubCompat{2562269 G.E...... ......I. 0,0-0,0 #7f09001d app:id/action_mode_bar_stub}
              android.support.v7.widget.ContentFrameLayout{50ceddb V.E...... ........ 0,0-1080,1848 #1020002 android:id/content}
                android.widget.LinearLayout{95c3078 V.E...... ........ 0,0-1080,1848 #7f09027a app:id/root}
                  com.ziipin.areatype.ZiipinToolbar{7d56351 V.E...... ........ 0,0-1080,132 #7f09030b app:id/toolbar}
                    android.widget.LinearLayout{bc427b6 V.E...... ........ 0,0-1080,132}
                      android.support.v7.widget.AppCompatImageView{3628cb7 V.ED..C.. ........ 0,0-120,132 #7f09030c app:id/toolbar_back}
                      android.support.v7.widget.AppCompatTextView{45fdc24 V.ED..... ........ 156,35-266,96 #7f09030f app:id/toolbar_text}
                      android.support.v4.widget.Space{9bbe38d I.ED..... ......I. 266,0-930,0}
                      android.widget.FrameLayout{630942 V.E...... ........ 930,0-1080,132 #7f09030e app:id/toolbar_menu_parent}
                        android.support.v7.widget.AppCompatTextView{35ed4ee G.ED..C.. ......I. 0,0-0,0 #7f090310 app:id/toolbar_text_menu}
                        android.support.v7.widget.AppCompatImageView{2093d53 V.ED..C.. ........ 0,0-150,132 #7f09030d app:id/toolbar_icon}
                  android.support.v7.widget.RecyclerView{fd09690 VFED..... ........ 0,132-1080,1632 #7f090115 app:id/feed_recycler}
                    android.widget.LinearLayout{aec7e9a V.E...... ........ 48,48-1032,695}
                      android.widget.LinearLayout{a6103cb V.E...... ........ 0,0-984,647}
                        android.support.v7.widget.AppCompatTextView{478a7a8 V.ED..... ........ 0,48-984,105 #7f090097 app:id/content_title}
                        android.support.v7.widget.AppCompatEditText{e64c2c1 VFED..CL. .F...... 0,135-984,271 #7f090094 app:id/content}
                        android.support.v7.widget.AppCompatTextView{683ea66 V.ED..... ........ 0,319-984,376 #7f090092 app:id/contact_title}
                        android.support.v7.widget.AppCompatEditText{87a11a7 VFED..CL. ........ 0,406-984,542 #7f090091 app:id/contact}
                        android.support.v7.widget.AppCompatTextView{b849654 V.ED..... ........ 0,590-984,647 #7f090292 app:id/screen_title}
                    android.support.constraint.ConstraintLayout{89e75bb V.E...C.. ........ 48,743-344,1039}
                      android.support.v7.widget.AppCompatImageView{cf94ad8 V.ED..... ........ 0,0-296,296 #7f090113 app:id/feed_image}
                      android.support.v7.widget.AppCompatImageView{2555d8f G.ED..C.. ......I. 0,0-0,0 #7f090114 app:id/feed_item_delete}
                  android.support.v7.widget.AppCompatButton{602de31 VF.D..C.. ........ 48,1632-1032,1782 #7f0902dd app:id/submit}
        android.view.View{c659ade V.ED..... ........ 0,0-1080,72 #102002f android:id/statusBarBackground}
    Looper (main, tid 1) {2b57015}
      Message 0: { when=+327ms callback=android.widget.Editor$Blink target=android.widget.Editor$Blink }
      (Total messages: 1, polling=false, quitting=false)
    Local FragmentActivity 7553594 State:
      mCreated=truemResumed=true mStopped=false mReallyStopped=false
    FragmentManager misc state:
      mHost=android.support.v4.app.FragmentActivity$HostCallbacks@c74981c
      mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@c74981c
      mCurState=5 mStateSaved=false mStopped=false mDestroyed=false

可以获取当前 Activity 的一些信息。

查看启动耗时

在分析程序的冷启动时,经常需要获取程序的启动时间,可以使用如下命令:

adb shell am start -W -S package/avtivity,比如:

adb shell am start -W -S com.ziipin.softkeyboard.uzbekistan/com.ziipin.MainActivity

可以从上一步获取 package/activity。-W:等待启动完成 -S: 杀死当前 App 并启动

SW-MacBook-Pro:other zp$ adb shell am start -W -S com.ziipin.softkeyboard.uzbekistan/com.ziipin.MainActivity
Stopping: com.ziipin.softkeyboard.uzbekistan
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.ziipin.softkeyboard.uzbekistan/com.ziipin.MainActivity }
Status: ok
Activity: com.ziipin.softkeyboard.uzbekistan/com.ziipin.setting.GuideActivity
// 表示当前 Activity 的启动时间 
ThisTime: 1131 
// 应用自身启动耗时=ThisTime+应用application等资源启动时间
TotalTime: 4171
// 系统启动应用耗时=TotalTime+系统资源启动时间
WaitTime: 4190
Complete

安装Apk

adb install -r path/xxx.apk

-r: 表示允许覆盖安装

-d: 表示允许降级覆盖安装

卸载应用

adb uninstall <应用包名>
adb uninstall -k <应用包名> *# 如果加 -k 参数,为卸载应用但是保留配置和缓存文件*

其中 <应用包名> 可以通过 adb shell pm list packages 命令查看:

TAG 、Log级别过滤日志

adb logcat < filter-spec>
其中 < filter-spec>可以由多个 < tag>[:priority] 组成

例: adb logcat MainActivity:D HomeFragemt:I 只输出TAG为MainActivity的Debug以上和 TAG为HomeFragemt的Info 以上的log日志,屏蔽其他的log日志;

清空日志 adb logcat -c

内核日志 adb shell dmesg

查看手机分辨率

adb shell dumpsys window displays

清楚应用数据和缓存

在开发过程中,经常需要清楚数据,还原应用,可以使用:

adb shell pm clear pkgname

通过adb 命令启动重启service ,发送广播来进行调试

重启service:

adb shell am force-stop com.xiaopeng.privacyservice
adb shell am start-foreground-service -a com.xiaopeng.privacyservice.PrivacyService

可以写成批处理文件:

@echo off
echo " --- begin --- "
adb shell am force-stop com.xiaopeng.privacyservice
adb shell am start-foreground-service -a com.xiaopeng.privacyservice.PrivacyService
echo " --- end ---"
@REM pause

带参数发送广播:

adb shell am start -a android.intent.action.VIEW -d "xiaopeng://com.xiaopeng.privacyservice?action=showdialog\&type=201\&flag=0"

参考资料:http://nky1989.com/2017/07/12/android-adb-shell-and-param/

调试修改[content provider]:参数见文档

 adb shell content query --uri content://com.xiaopeng.privacyservice.provider/protocol
 adb shell content update --uri content://com.xiaopeng.privacyservice.provider/protocol --bind date:s:10

查看文件的最后几行并过滤:

tail -f main.txt | grep RemoteMessageP

未完待续