Computer >> Hướng Dẫn Máy Tính >  >> Hệ Thống >> Android

Hướng Dẫn Toàn Diện ADB: Cài Driver & Làm Chủ Lệnh Cho Thiết Bị Android

Bài viết khám phá cách dùng ADB kiểm soát chi tiết khi cài đặt, test, chẩn đoán, quản lý một hoặc nhiều thiết bị/emulator.

Vài năm đầu làm software developer, chủ yếu Android SDK, không biết ADB (Android Debug Bridge/adb) là gì, làm gì, khi nào dùng.

Thú vị, không phải mục tiêu chuyên nghiệp thúc đẩy học ADB. Là Nexus 6 boot loop muốn cứu. Với vấn đề đó, Android Studio và Gradle hữu ích như túi trà chống nước.

Bài viết dành cho 2 loại người:

  • Quen CLI, Shell, Processes, Client-Server Model
  • Chưa quen CLI, Shell, Processes, Client-Server Model

Nhóm 1 có thể skip section "Cách Làm Việc Với ADB."

Nhóm 2: giả định như mình junior, biết ít về CLI, Shell, ADB. Section đầu là giới thiệu mềm dẻo và glossary thuật ngữ cơ bản, giải thích đơn giản nhất.

Chuẩn Bị

Học chủ đề quan trọng để hiểu ADB hoạt động và dùng như thế nào.

Một số người sợ CLI do Vim enthusiasts hay Unix SysAdmins phán xét. Sẽ thấy mình thú nhận CLI không ideal cho não bộ, nên có thể thích góc nhìn này.

Command Line

Đơn giản: command line là interface (cách gửi/nhận thông tin) đến máy tính chỉ dùng lines of text.

Quan trọng: CLI không phải chương trình, mà một số chương trình cung cấp CLI (và có thể GUI).

Đã từng gõ vào Windows Command Prompt (hoặc MS-DOS nếu 90s kid), Mac Terminal, GNOME Terminal Linux. Chúng dùng CLI.

Lợi ích/khuyết điểm CLI phụ thuộc người dùng và vấn đề giải quyết. Mình không thích CLI trừ khi làm việc gần như hàng ngày.

Não mình không thích ghi nhớ shorthand text commands bí ẩn (khó học đọc nhỏ vì cùng lý do), phải rely trên implicit memory dựa trên repetition (muscle memory) và cheat sheets.

Những ai sẵn sàng bỏ thời gian dù struggle (như mình), hoặc giỏi nhớ thứ này, sẽ học to được hiệu quả hơn trong CLI vs GUI.

Nhiều operation hoàn thành trong fraction of time so với point-and-click qua menus/screens. Cũng có thể viết scripts - file chứa series text commands - tăng hiệu quả hơn.

Cách Dùng ADB Shell

Giả định quen khái niệm Operating System (OS): Android, iOS, Windows, Mac, Linux, Unix-like khác.

Tại sao OS liên quan ADB? Giải thích ưu tiên clarity over precision: Android OS based on Linux, Linux based on Unix.

Kết quả: dùng ADB lấy Unix Shell của device/emulator đang làm việc. Cho flexibility, capabilities, control lớn bằng cách trực tiếp tương tác shell.

Shell là thuật ngữ chung cho chương trình tương tác với OS. Giống vỏ rùa bảo vệ và cho truy cập rùa (outermost layer), shell OS vừa bảo vệ vừa cung cấp truy cập inner workings. Mình ngạc nhiên "Shell" không phải acronym esoteric.

Đừng overthink. Đọc bài này trên máy tính, bạn đã dùng shell để đến đây.

Shell cung cấp CLI hoặc GUI hoặc cả hai. Dùng để create/update/delete/move files, start programs, access OS services.

Cách Dùng ADB Client Và ADB Server

Giải thích hơi imprecise nhưng dễ hiểu. Sửa định nghĩa sau.

Clients và Servers đều là computers. Phân biệt dựa trên role. Ví dụ: máy tính bạn (desktop, laptop, phone) là Client của freeCodeCamp Server, serve bạn trang HTML này.

Nói chung, client là điều dùng thứ khác, server là điều bị dùng. Đừng overthink, Client-Server Model mô tả rất nhiều thứ trong và ngoài computing.

Khi nói Clients/Servers là "computers", không đúng trong context sau. Lập trình viên/engineer nên nghĩ Clients/Servers là processes (process = running program).

Có nghĩa Client process và Server process thường chạy trên máy riêng, nhưng cũng ok chạy cùng máy. Chiếm distinct locations trong memory space, khác biệt chỉ communicate dùng IPC (inter-process communication) thay vì network connection.

ADB dùng Server process, cho phép multiple developers (multiple clients) manage multiple Android devices/emulators.

Enterprise setting: Server process trên remote computer (communicate qua network), nhưng setup Server local cho Client. Đơn giản hơn nghĩ.

ADB Daemon Là Gì?

Nếu skip ahead, đã giải thích process = running program. Daemon = process chạy background, user không trực tiếp tương tác.

Ví dụ: mở web browser, actual work quản lý network connections connect Internet bởi NetworkManager Daemon (không phải browser process).

Mỗi Android device (physical/emulated), nếu config đúng, có ADB Daemon (adbd) execute commands từ Server process.

Tóm lại: Client issue command → Server → ADBD → execute trên device.

Cách Dùng ADB Cho Phát Triển Android

Phần còn lại khám phá:

  • Drivers và config cần thiết dùng ADB trên system
  • Dùng ADB với physical devices và emulators
  • Basic commands dùng ADB CLI
  • Glance advanced usage dùng Android device Shell qua ADB

Trước tiên, xác định CLI tool dùng tương tác ADB. Windows: prefer PowerShell, Command Prompt cũng ok. Linux/Mac: default Terminal.

Dùng gì làm việc được.

Bài viết giải thích chi tiết, nhưng có video tutorial ngắn gọn:

Video tutorial cho adb.

Hiểu CLI Examples

Bài nhiều commands input CLI. Phần thay đổi tình huống viết trong angle brackets.

KHÔNG include angle brackets trong CLI command.

Ví dụ viết adb pair <ip-address>:<port>, bạn thay bằng actual value: adb pair 192.168.0.1:5554.

ADB Drivers & Configuration

Đảm bảo latest (hoặc recent) Android SDK Platform-Tools. Nếu không dùng Android Studio (AS), click link download standalone package cho OS.

Nếu có AS, download/update package dùng SDK Manager.

Thường có toolbar icon AS mở SDK Manager, nhưng họ đổi icon gần như mỗi hotfix.

Không tìm thấy: File → Settings, search bar gõ "SDK", tìm menu "Android SDK".

Hướng Dẫn Toàn Diện ADB: Cài Driver & Làm Chủ Lệnh Cho Thiết Bị Android
Setting hệ thống showing Android SDK Platform Tools installed

Bước tiếp theo thay đổi theo variables. Như thảo luận section Preliminaries, ADB dùng Client-Server Model cho flexibility.

Cụ thể, bạn có thể có:

  • Multiple Clients interact với remote Server
  • Server local (cùng máy) với một Client
  • Variety physical devices/emulators hook lên same server

Advanced config multiple Clients và nhiều devices quá scope bài này.

One Server manage up to 16 emulators và nhiều physical devices (within reason), không cần advanced config.

Phần còn lại, nhiều nhất làm việc 1 physical device và 1 emulator cho single ADB server process.

Cấu Hình ADB Emulator

Có thể không cần config thêm, nhưng có thể cần enable Developer Options trên emulator. Sẽ biết ngay khi chạy first ADB commands.

Nếu muốn enable feature trên emulator, research cách làm cho Android version cụ thể.

USB Debugging – Cấu Hình Physical Device

Không dùng physical device, skip section. Tuy nhiên lưu ý vẫn có thể cần enable Developer Options.

Để tiếp tục, cần config USB Debugging hoặc WiFi Debugging trên Android device và development machine.

Cả 2 case: bắt đầu enable Developer Options trên device. Research cách làm cho Android version cụ thể.

USB Debugging

Đảm bảo enable USB Debugging trên Android device qua Developer Options. Link trên mô tả process, thay đổi across Android OS versions.

Trước khi tiến, Windows users cần download USB Driver. Ubuntu users cần extra steps. Mac/Chrome OS: good to go.

Sau khi USB Debugging enabled, connect Android device via USB cable.

WiFi Debugging

Nếu multiple physical devices hoặc thiếu USB cables, chọn WiFi Debugging.

Developer Options trên Android device, enable Wireless debugging. Prompt về Allow debugging trên network device đang connect, allow (giả định network phù hợp).

Thời gian làm việc với CLI. Tìm platform-tools directory trong Android SDK installation directory.

Giả sử có AS, cách nhanh locate: File → Settings, search "SDK". Menu "Android SDK" show SDK install directory, chứa platform-tools.

Ví dụ dưới: copy path Android SDK directory, mở Windows PowerShell. Gõ commands:

Change Directory:

cd <path-to-SDK-directory>

List Files and Directories:

ls
Hướng Dẫn Toàn Diện ADB: Cài Driver & Làm Chủ Lệnh Cho Thiết Bị Android

Gõ cd platform-tools navigate đến directory. Các bước sau giả sử device chạy Android OS 11+.

Nếu device Android 10 hoặc thấp hơn, hướng dẫn chi tiết tại link.

Khi đã trong platform-tools directory, ready pair Android device với development machine:

1. Trong Wireless debugging submenu Settings → System → Developer options, chọn Pair device with pairing code.

2. CLI tool ở platform-tools directory, gõ:

adb pair <IP address>:<Port>

IP address và Port từ dialog trên Android device sau khi chọn Pair device with pairing code (không include angle brackets).

Lưu ý: Có thể cần prepend adb call với symbols/commands khác tùy CLI tool, OS, access controls. Ví dụ: mình gõ .\adb pair <IP address>:<Port> dùng PowerShell Windows.

3. Nếu CLI ok, prompt nhập pairing code hiển thị cùng dialog cho IP/Port.

4. Sau nhập pairing code, operation successful nếu nhận message:

Successfully paired to <IP Address>:<Port> [guid=<Some GUID>]

5. Windows/Linux, cũng chạy command dùng IP Address & Port từ Wireless debugging preferences menu (không phải dialog Pair device):

adb connect <IP Address>:<Port>

Sau đó nhận notification trên phone chỉ connected.

Cách Dùng ADB: Commands

Giả sử config đúng device và development machine, dùng ADB tool.

Trước khi tiến, navigate directory chứa adb dùng CLI tool (trừ khi vừa làm steps trước setup WiFi debugging).

Làm ngay, hoặc check section trước để locate folder.

Xem Thiết Bị Đang Kết Nối Server

Khởi động adb server bằng cách gọi bất kỳ command ADB trừ adb kill-server. Server process chạy hay không, gõ:

adb devices

Hướng Dẫn Toàn Diện ADB: Cài Driver & Làm Chủ Lệnh Cho Thiết Bị Android

Screenshot: gọi adb devices khi Android phone connected server. Sau kill server via adb kill-server, gọi devices restart server.

Lại, nếu ADB server không chạy, gọi hầu như bất kỳ ADB command sẽ start server lại (trừ adb kill-server). Có explicit adb start-server nhưng thực tế chưa bao giờ cần.

Vì server reset, devices không return items. Trước next example, phải dùng lại adb pair và adb connect (Windows/Linux) như section trước.

Khởi động emulator dùng PowerShell và emulator program trong subdirectory platform-tools gọi "emulator".

Có thể dùng AVD Manager hoặc AS start emulator follow along.

Nhiều connected devices, option hữu ích cho adb devices là -l, cho more info devices.

Dưới đây several entries physical Android device, emulator attached specific port:

Hướng Dẫn Toàn Diện ADB: Cài Driver & Làm Chủ Lệnh Cho Thiết Bị Android

Gửi Commands Đến Specific Device

Tránh brick phone, gửi commands đến emulator. Phải prepend option -s, theo sau serial number target device, trước command.

Serial number là first set characters describe connected device sau devices command.

Ví dụ: emulator serial number là "emulator" theo sau port emulator attached.

Hướng Dẫn Toàn Diện ADB: Cài Driver & Làm Chủ Lệnh Cho Thiết Bị Android

Mũi tên đỏ khác trỏ serial number phone (cheated for obvious reasons).

Tự nhiên, chỉ có 1 device connected (bất kỳ loại), không cần option -s.

Cài APK (App) Lên Device

Cài test APK lên emulator chạy dùng adb install.

Tương đương AS và Gradle cài debug APK. Test APK cần option -t sau install:

adb -s <device-serial-number> install -t <path-to-APK>

Hướng Dẫn Toàn Diện ADB: Cài Driver & Làm Chủ Lệnh Cho Thiết Bị Android

Lưu ý: Android OS yêu cầu APK phải signed trước khi cài (dù chỉ test/debug APK).

Giải pháp: build và run app cài trong AS, sign với generated debug certificate. Có nhiều cách sign APK khác, explore tại link.

ADB Còn Làm Gì Khác?

Trước khi xem advanced usage, khuyên thử adb --help. Như customary CLI programs, help command print documentation mô tả commands/options tool.

Vui vì ADB documentation khá legible và useful, không phải lúc nào CLI programs cũng vậy.

Advanced ADB Usage Tips

Lãng phí time cover mọi usage/command ADB trong bài.

Tránh hiểu lầm: dùng ADB cài APK và làm những gì AS/Gradle làm cho bạn không recommended (trừ có lý do tốt).

Với đó, ADB làm nhiều thứ difficult/impossible không có nó.

Section Preliminaries nhắc ADB hook shell device. Kết thúc bài, xem cách dùng shell commands và tìm thêm info.

Nếu không biết shell là gì, có thể skip section giải thích.

Cách Dùng ADB Shell

Gửi command đến device shell qua ADB đơn giản. Nhớ nếu multiple devices connected, follow với -s <device-serial-number> direct command đến specific device.

Single shell command: dùng adb shell (big surprise), theo sau actual command trên device shell:

adb shell ls

Output:

Hướng Dẫn Toàn Diện ADB: Cài Driver & Làm Chủ Lệnh Cho Thiết Bị Android

Như đề cập, ls hiển thị list files/directories tại CLI current directory. Happen to be Android device root directory cho đến khi move.

Nếu plan nhiều commands via Shell, start interactive Shell session. Command đơn giản:

adb shell

Trong interactive Shell session, gõ Shell commands trực tiếp không cần adb shell <command>.

Khi muốn quit interactive Shell session: gõ exit hoặc Ctrl + D.

Hướng Dẫn Toàn Diện ADB: Cài Driver & Làm Chủ Lệnh Cho Thiết Bị Android

Có variety commands/utilities dùng via Shell. ActivityManager (am) Android device hữu ích test components (Activities, Services, BroadcastReceivers) app dưới different circumstances.

Giả sử muốn launch straight vào particular Activity, nhưng Activity không designated launcher Activity trong manifest.

Vẫn cần thêm attribute android:exported=”true” cho mỗi <activity/> entry manifest muốn launch (giả sử không phải launcher Activity).

Sau đó dùng command:

am start -n <app-package-id>/<activity-name>

Lưu ý <activity-name> phải include packages, relative to package-id, nằm trong đó. Xem output dưới example launch Activity nested trong several packages.

Hướng Dẫn Toàn Diện ADB: Cài Driver & Làm Chủ Lệnh Cho Thiết Bị Android

Further Reading

Mục tiêu bài: introduce, explain, guide ADB usage theo từ mình (văn chương cho phép).

Đến đây phải làm contrived examples hoặc carbon copy docs, không hứng thú.

Thay vào đó, khuyến khích visit documentation, brief look cool things dùng tools như Activity Manager, Package Manager, Policy Manager, others.

Liên hệ social media:

https://www.instagram.com/rkay301/
https://www.facebook.com/wiseassblog/
https://twitter.com/wiseass301
https://wiseassblog.com/