Android一些常用命令

开发调试常用命令列表

记录并收集一些开发调试过程中有效的命令,主要是Android相关。

1 通用命令

1.1 iptables

  • 限制上行和下行速率
    可用于模拟弱网模式
iptables -I OUTPUT 1 -m hashlimit --hashlimit-name testup --hashlimit-above 5/s -j DROP
iptables -I INPUT 1 -m hashlimit --hashlimit-name testdown --hashlimit-above 5/s -j DROP
  • 设置丢包率
iptables -I INPUT 1  -m statistic --mode random  --probability 0.8 -j DROP
  • 查看所有规则
iptables --list
  • 清空所有规则
iptables -F

1.2 find

  • 查找大文件
busybox find [PATH] -size +10000k
  • 按条件查找并执行命令
find /root -size -5557c -size +5555c -exec ls -ld {} \;
  • 结合xargs
find . -name "*.java" | xargs grep -HRn 'AudioFormat.ENCODING_PCM_16BIT'
  • 查看指定目录层级
busybox find /sdcard/test/ -maxdepth 4
  • notepad lazzy pattern
    用lazy pattern模式正则表达式搜索,"text":".?"*

1.3 mount

  • 挂载虚拟机虚拟文件系统
sudo mount -t vboxsf share /mnt
  • 挂载ci文件系统(for ubuntu)
mount.cifs -o username=username /////////////////path to ci package

1.4 计算器

echo | awk '{print (126+39+35+18+12+12+11+11+0.1)/4}'

1.5 sed

分析日志

// 查找识别结果
sed -n 's/.*,\("text":"[^"]*"\),.*/\1/p' log.txt
sed -n 's/.*SrSolution: ISS_SR_MSG_RESULT.*,\("text":"[^"]*"\),.*/\1/p' log.txt

2.adb命令

2.1 包管理相关

  • 查看应用包名信息
dumpsys package com.test.test
  • 根据组件名称打开对应的Activity
// 打开Android原生设置页面
am start -n com.android.settings/.Settings
  • 打开指定uri对应的Activity
am start "intent:#Intent;action=android.media.action.MEDIA_PLAY_FROM_SEARCH;\
S.android.intent.extra.focus=vnd.android.cursor.item%2Fartist;\
S.android.intent.extra.artist=Adele;end"
am start "intent:#Intent;action=android.intent.action.VIEW;\
category=android.intent.category.BROWSABLE;launchFlags=0x10200000;\
component=com.retton.h5/.WebActivity;\
S.title=%E6%97%A5%E6%9C%9F%2F%E6%97%B6%E9%97%B4;\
S.url=http%3A%2F%2Fwww.pingwest.com;end"
  • startService
am startservice -a com.test.VoiceKey -e StartFrom HomeVoiceButton
  • 外部调用
adb shell "am startservice -a com.test.dictionary -e param test -e data '"'[{"group":"","location":{"city":"合肥","country":"中国","province":"安徽"},"name":"张小三","phoneNumber":"13019001001","teleOper":"联通","type":"工作"}]'"'"
adb shell "am startservice ... '"'JSON_TEXT'"'"

2.2 logcat

  • 过滤指定文本
    查看识别结果
adb logcat | sed -n 's/.*intent.*,"text":"\([^"]*\)",.*/\1/p'
  • 常用
logcat | busybox grep `ps | busybox grep "com.package.name$" | busybox awk -F " " '{print $2}'`
logcat | busybox grep `ps | busybox grep "com.package.name$" | busybox awk 'NR%2==1{T=$2;next}{print T"|"$2}'
logcat | grep -E `ps |grep account| busybox awk 'NR%2==1{T=$2;next}{printf T"|"$2}'`
adb logcat -vtime -d > ~/Desktop/dump.log

2.3 网络抓包

tcpdump -i usb0 -s0 -w /data/local/tmp/`date "+%Y%m%d%H%M%S"`.pcap
tcpdump -i usb0 -s0 -vv tcp port 80 and host hostname

2.4 monkey命令

monkey -p com.package.name --pct-syskeys 0 100000

2.5 系统命令

  • wifi控制
svc wifi [enable|disable]
  • 数据流量控制
svc data [enable|disable]
  • 按键注入
input keyevent [--longpress] 
input keyevent HOME
input keyevent BACK

2.6 系统分析

  • 抓取CPU占用信息
echo 'H' | busybox top -d 1 | busybox tee /sdcard/top_`date "+%Y%m%d%H%M%S"`.log
  • 生成native trace
    执行之后在/data/tombstones中生成native栈信息
debuggerd -b [pid]
  • 生成java虚拟机栈信息
    在/data/anr/traces.txt中生成虚拟机各线程调用栈信息
kill -3 [pid]
  • 获取视图树
uiautomator dump /sdcard/dump.xml
  • 获取CPU主频信息
cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq
  • 查看虚拟机OOM ADJ
cat /proc/PID/oom_adj

3.签名工具

3.1 列出密钥库中的条目

keytool -list -v -keystore debug.keystore

3.2 打印APK解压出来的证书内容

通过此方法能查看APK的签名信息,包括SHA1等。

keytool -printcert -file META-INF/CERT.RSA

3.3 使用keystore给unsigned apk签名

jarsigner -verbose -keystore [keystorePath] -signedjar [apkOut] [apkIn] [alias]

3.4 验证APK的签名状态

jarsigner -verbose -verify -certs [apk]

3.5 查看APK信息

aapt dump badging {PATH}.apk

3.6 系统签名导入keystore

将一般ROM的系统签名(pk8私钥和pem公钥)导入到keystore中方便在ide或ci中自动化构建,步骤如下:

  • pk8私钥转成pem格式
openssl pkcs8 -inform DER -nocrypt -in platform.pk8 -out key.pem
  • 导出p12证书
openssl pkcs12 -export -in platform.x509.pem -inkey key.pem -out platform.p12 -password pass:test -name test
  • 导入keystore
keytool -importkeystore -deststorepass test -destkeystore test.jks -srckeystore platform.p12 -srcstoretype PKCS12 -srcstorepass test
  • 检证keystore签名列表
keytool -list -v -keystore test.jks

4.git

4.1 gitbash

Windows系统通过gitbash默认push文件到android系统时会出现文件系统解析错误,导致找不到目标路径,需在目标系统路径前增加“/”:

adb push myfile //{path}/myfile

4.2 git

#删除对文件的跟踪,会修改commit id,只在本地使用
git filter-branch --tree-filter 'rm -f testme.txt' HEAD

忽略merge日志
git log --no-merges

查看从v2.6.12开始include/scsi和drivers/scsi目录中的改动的日志
git log v2.6.12.. include/scsi drivers/scsi

导出两周前开始的修改记录到文件gitk,“--”是为了避免和名为gitk的分支冲突
git log --since="2 weeks ago" -- gitk

查看test分支有但release分支没有的提交记录,并显示提交记录修改的路径信息
git log --name-status release..test

查看指定文件的修改记录,包含该文件被重命名等操作前的修改记录
git log --follow builtin/rev-list.c

本地有而远端没有的提交记录
git log --branches --not --remotes=origin

本地master分支有而远端master分支没有的提交记录
git log master --not --remotes=*/master

git log -p -m --first-parent

Shows the history including change diffs, but only from the “main branch” perspective, skipping commits that come from merged branches, and showing full diffs of changes introduced by the merges. This makes sense only when following a strict policy of merging all topic branches when staying on a single integration branch.

git log -L '/int main/',/^}/:main.c

Shows how the function main() in the file main.c evolved over time.

你可能感兴趣的:(Android一些常用命令)