一直沒有靜下心來自己了解一下環境變量,也懶得配置,真是一個不合格的程序員。今天在研究安卓sdk的時候,需要進行環境變量的配置,我用的是Mac,使用adb命令一直失敗,了解到是環境變量配置問題,所以對此進行了了解和研究
環境變量的概念:
家里又各種各樣的盒子,里面放着不同首飾,盒子的位置分別在不同地方。每次找東西,都要先找盒子,還不知道里面有沒有想要的東西,也不知道盒子放在哪里。 於是在一個下雨天,把每一個盒子里的位置記錄在大腦里,以后找什么東西,知道盒子位置,就不用一個一個線索的去先找盒子了。
path1中有一個app neckless
path2中有一個app ring
path3中有一個應用 chopsticks
path4中有一個應用 cup
其中path4和path3都有父級路徑kitchen.即kitchen/path3/chopsticks 和kitchen/path4/cup
在Mac中,通過vim命令: vim ~/.bash_profile(如果不存在,要創建)打開編輯頁面。進入后如果不能編輯,點擊按鈕i,切換到insert狀態進行編輯
添加內容 export PATH=path1/neckless:$PATH 注意等號前后不能有空格. $PATH也可以放在開頭。這樣neckless就已經添加進環境變量了。
可以通過export PATH = $PATH:path2/ring的方式將ring添加進去。
注意chopsticks和cup的上一層有共同路徑kitchen。 對此,我們可以將兩者的共同路徑做為一個變量: $KITCHEN_PATH=/kitchen
然后將兩者添加進PATH : export PATH = $PATH:$KITCHEN_PATH/path3:$KITCHEN_PATH/path4
按下esc鍵,輸入:wq進行保存並退出
在終端執行 source ~/.bash_profile生成環境變量
可以通過 echo $PATH來查看路徑是否已經生成。如果生成,path1234就可以直接在終端上運行,而不是需要去查找路徑找到app才能打開。
安卓SDK為例:
沒有配置環境的前提下在終端直接執行adb或者aapt只會提示command not found. 這是因為系統在默認已經登記的路徑下沒有找到這兩個應用。所以我們需要通過添加環境變量的方式將兩個路徑添加進去。
- 打開終端Terminal:鍵入vim ~/.bash_profile打開文件(如果文件不存在,通過touch ~/.bash_profile創建文件。):
2.鍵入E,此時的界面是不可編輯狀態,點擊鍵盤:i 切換到輸入模式
鍵入下面內容:
解釋一下:
ANDROID_PATH是一個保存上級路徑的變量,所以需要在調用時加上$。如果沒有$,系統會將其默認為路徑名稱。
所以 $ANDROID_PATH/tools就相當於保存了路徑:/Users/ankaihang/Downloads/android-sdk-macosx
如果沒有$,環境變量里就只有ANDROID_PATH/tools。
因為變量的保存方式是: export PATH=$PATH:path1:path2:path3
或者: export PATH=$PATH:path1
export PATH=$PATH:path2
export PATH=$PATH:path3
如果不添加PATH變量$PATH,那么PATH就會被最新的覆蓋。e.g. 在最后加上export PATH=path4. 那最后保存進去的路徑就只有path4了。
3. 點擊ESC退出輸入模式,輸入‘:wq’退出並保存
4. 在終端輸入source ~/.bash_profile更新環境變量
5. 可通過 echo $PATH查看是否保存成功。
如果路徑和應用所在路徑一致,可以在終端直接輸入adb,如果得到以下內容就說明環境變量配置成功了
Android Debug Bridge version 1.0.40
Version 4797878
Installed as /Users/ankaihang/Downloads/android-sdk-macosx/platform-tools/adb
global options:
-a listen on all network interfaces, not just localhost
-d use USB device (error if multiple devices connected)
-e use TCP/IP device (error if multiple TCP/IP devices available)
-s SERIAL use device with given serial (overrides $ANDROID_SERIAL)
-t ID use device with given transport id
-H name of adb server host [default=localhost]
-P port of adb server [default=5037]
-L SOCKET listen on given socket for adb server [default=tcp:localhost:5037]
general commands:
devices [-l] list connected devices (-l for long output)
help show this help message
version show version num
networking:
connect HOST[:PORT] connect to a device via TCP/IP [default port=5555]
disconnect [HOST[:PORT]]
disconnect from given TCP/IP device [default port=5555], or all
forward --list list all forward socket connections
forward [--no-rebind] LOCAL REMOTE
forward socket connection using:
tcp:<port> (<local> may be "tcp:0" to pick any open port)
localabstract:<unix domain socket name>
localreserved:<unix domain socket name>
localfilesystem:<unix domain socket name>
dev:<character device name>
jdwp:<process pid> (remote only)
forward --remove LOCAL remove specific forward socket connection
forward --remove-all remove all forward socket connections
ppp TTY [PARAMETER...] run PPP over USB
reverse --list list all reverse socket connections from device
reverse [--no-rebind] REMOTE LOCAL
reverse socket connection using:
tcp:<port> (<remote> may be "tcp:0" to pick any open port)
localabstract:<unix domain socket name>
localreserved:<unix domain socket name>
localfilesystem:<unix domain socket name>
reverse --remove REMOTE remove specific reverse socket connection
reverse --remove-all remove all reverse socket connections from device
file transfer:
push [--sync] LOCAL... REMOTE
copy local files/directories to device
--sync: only push files that are newer on the host than the device
pull [-a] REMOTE... LOCAL
copy files/dirs from device
-a: preserve file timestamp and mode
sync [all|data|odm|oem|product|system|vendor]
sync a local build from $ANDROID_PRODUCT_OUT to the device (default all)
-l: list but don't copy
shell:
shell [-e ESCAPE] [-n] [-Tt] [-x] [COMMAND...]
run remote shell command (interactive shell if no command given)
-e: choose escape character, or "none"; default '~'
-n: don't read from stdin
-T: disable PTY allocation
-t: force PTY allocation
-x: disable remote exit codes and stdout/stderr separation
emu COMMAND run emulator console command
app installation:
install [-lrtsdg] [--instant] PACKAGE
install-multiple [-lrtsdpg] [--instant] PACKAGE...
push package(s) to the device and install them
-l: forward lock application
-r: replace existing application
-t: allow test packages
-s: install application on sdcard
-d: allow version code downgrade (debuggable packages only)
-p: partial application install (install-multiple only)
-g: grant all runtime permissions
--instant: cause the app to be installed as an ephemeral install app
uninstall [-k] PACKAGE
remove this app package from the device
'-k': keep the data and cache directories
backup/restore:
to show usage run "adb shell bu help"
debugging:
bugreport [PATH]
write bugreport to given PATH [default=bugreport.zip];
if PATH is a directory, the bug report is saved in that directory.
devices that don't support zipped bug reports output to stdout.
jdwp list pids of processes hosting a JDWP transport
logcat show device log (logcat --help for more)
security:
disable-verity disable dm-verity checking on userdebug builds
enable-verity re-enable dm-verity checking on userdebug builds
keygen FILE
generate adb public/private key; private key stored in FILE,
public key stored in FILE.pub (existing files overwritten)
scripting:
wait-for[-TRANSPORT]-STATE
wait for device to be in the given state
State: device, recovery, sideload, or bootloader
Transport: usb, local, or any [default=any]
get-state print offline | bootloader | device
get-serialno print <serial-number>
get-devpath print <device-path>
remount remount partitions read-write
reboot [bootloader|recovery|sideload|sideload-auto-reboot]
reboot the device; defaults to booting system image but
supports bootloader and recovery too. sideload reboots
into recovery and automatically starts sideload mode,
sideload-auto-reboot is the same but reboots after sideloading.
sideload OTAPACKAGE sideload the given full OTA package
root restart adbd with root permissions
unroot restart adbd without root permissions
usb restart adb server listening on USB
tcpip PORT restart adb server listening on TCP on PORT
internal debugging:
start-server ensure that there is a server running
kill-server kill the server if it is running
reconnect kick connection from host side to force reconnect
reconnect device kick connection from device side to force reconnect
reconnect offline reset offline/unauthorized devices to force reconnect
environment variables:
$ADB_TRACE
comma-separated list of debug info to log:
all,adb,sockets,packets,rwx,usb,sync,sysdeps,transport,jdwp
$ADB_VENDOR_KEYS colon-separated list of keys (files or directories)
$ANDROID_SERIAL serial number to connect to (see -s)
$ANDROID_LOG_TAGS tags to be used by logcat (see logcat --help)