This comprehensive guide covers essential tips and techniques for Android development, including Java environment setup, USB debugging, wireless debugging, and build management.
Managing multiple Java versions is crucial for Android development. Here's how to set it up:
- Install OpenJDK and jenv:
# Install OpenJDK 11 (recommended for Android development)
brew install openjdk@11
# Install jenv for Java version management
brew install jenv
- Configure your shell environment (add to
~/.zshrc
or~/.bashrc
):
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"
- Add Java versions to jenv:
# Add OpenJDK 11
jenv add /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home
# Verify installation
jenv versions
# Set global Java version
jenv global 11.0
# Set local version for a specific project
jenv local 11.0
-
Enable Developer Options on your Android device:
- Go to Settings → About Phone
- Tap "Build Number" seven times
- Developer Options will appear in Settings
-
Enable USB Debugging:
- Go to Settings → Developer Options
- Enable "USB Debugging"
- Connect your device via USB
- Accept the debugging authorization prompt on your device
-
Verify connection:
# List connected devices
adb devices
# Check detailed device information
adb shell getprop ro.product.model
- Connect via USB first, then enable wireless debugging:
# Ensure device and computer are on same network
# Connect device via USB
# Enable TCP/IP mode on default port (5555)
adb tcpip 5555
# Get device IP address
adb shell ip addr show wlan0
# Connect to device wirelessly
adb connect <device-ip>:5555
# Verify connection
adb devices
- For React Native development:
# Start Metro bundler
npx react-native start
# Forward connection from mobile to local
adb reverse tcp:8081 tcp:8081
-
Using Android Studio:
- Open your project in Android Studio
- Select "Debug" configuration
- Click "Run" (⌘R on Mac, F5 on Windows)
-
Using Command Line:
# Build debug APK
./gradlew assembleDebug
# Install on connected device
adb install app/build/outputs/apk/debug/app-debug.apk
- Configure signing:
- Create
android/app/keystore.properties
:
- Create
storeFile=your-key.keystore
storePassword=your-store-password
keyAlias=your-key-alias
keyPassword=your-key-password
- Build release version:
# Generate release APK
./gradlew assembleRelease
# Location of generated APK
cd android/app/build/outputs/apk/release
- Test release build:
# Install release version
adb install app-release.apk
# View logs for specific package
adb logcat | grep "com.yourpackage"
# Filter by log level
adb logcat *:E # Show only errors
# Save logs to file
adb logcat > logfile.txt
- Metro Bundler Issues:
# Clear Metro bundler cache
npx react-native start --reset-cache
# Clear Gradle cache
cd android && ./gradlew clean
- Device Connection Issues:
# Reset ADB
adb kill-server
adb start-server
# Restart device USB debugging
# Toggle USB debugging off and on in device settings
-
Using Android Studio:
- Open Android Studio
- Go to View → Tool Windows → Profiler
- Select your running app
- Monitor memory usage in real-time
-
Using Command Line:
# Dump memory info
adb shell dumpsys meminfo <package-name>
# Capture heap dump
adb shell am dumpheap <process-name> /data/local/tmp/heap.hprof