Android command-line tools allow you to develop, build, test, and manage Android applications directly from your terminal without depending entirely on the Android Studio graphic interface. These tools are highly efficient for automated testing, continuous integration (CI/CD) pipelines, and rapid development.
Here is a comprehensive guide to locating, setting up, and using the essential Android command-line utilities. 🛠️ Environment Setup & Locating Tools
Before running commands, your terminal must know where the tools are located. If you have Android Studio installed, your SDK files are already downloaded. Locate your SDK path:
Open Android Studio, go to File > Project Structure > SDK Location.
Alternatively, find the standalone cmdline-tools package on the Android Studio Tools Page if you are setting up a headless build server.
Configure your Environment Variables:Add the SDK paths to your system’s PATH variable so you can invoke commands globally. Update your shell configuration file (e.g., /.bashrc or /.zshrc):
export ANDROID_HOME=\(HOME/Android/Sdk export PATH=\)PATH:\(ANDROID_HOME/cmdline-tools/latest/bin export PATH=\)PATH:\(ANDROID_HOME/platform-tools export PATH=\)PATH:$ANDROID_HOME/emulator Use code with caution. 📦 Component Management with sdkmanager
The sdkmanager tool allows you to view, install, update, and uninstall packages for the Android SDK. List available and installed packages: sdkmanager –list Use code with caution. Install specific platform tools or build API levels: sdkmanager “platform-tools” “platforms;android-34” Use code with caution. Update all installed packages: sdkmanager –update Use code with caution. 🏗️ Building Apps with Gradle Wrapper (gradlew)
You do not need Android Studio to compile your app code into an installable APK. The local Gradle wrapper (gradlew) located in your project root handles your builds. Compile a Debug APK: ./gradlew assembleDebug Use code with caution. Compile a Release Bundle (AAB): ./gradlew bundleRelease Use code with caution. Clean the project build cache: ./gradlew clean Use code with caution. 📲 Device Interaction with Android Debug Bridge (adb)
The adb tool is a versatile command-line component that lets you communicate with connected physical Android devices or running emulators. List connected devices: adb devices Use code with caution. Install an APK file onto a device: adb install path/to/your/app.apk Use code with caution. Launch a specific app activity: adb shell am start -n com.example.package/.MainActivity Use code with caution. View real-time device logs (Logcat): adb logcat Use code with caution. Transfer files to the device: adb push local_file.txt /sdcard/ Use code with caution. 🖥️ Managing Emulators via emulator
You can spin up and manage Android Virtual Devices (AVDs) straight from your terminal prompt. List your created virtual devices: emulator -list-avds Use code with caution. Launch a virtual device: emulator -avd Pixel_7_API_34 Use code with caution.
Launch an emulator with no graphical window (useful for background testing): emulator -avd Pixel_7_API_34 -no-window -no-audio Use code with caution. 🔏 Optimization and Signing (zipalign & apksigner) Command-line tools | Android Studio
Leave a Reply