Building for Android
Set up Android Studio and build your Capacitor app for Android devices and emulators.
What you'll accomplish
Run your Capacitor app on an Android Emulator and physical device.
Prerequisites
- Android Studio installed
- Completed Next.js + Capacitor setup
Requirements
Unlike iOS, Android development works on any operating system.
Install Android Studio
- Go to developer.android.com/studio
- Download Android Studio
- Run the installer
First-Time Setup
When Android Studio opens:
- Choose Standard installation
- Select a theme (Darcula or Light)
- Wait for component downloads (several GB)
- Accept all license agreements
Install SDK Components
Go to Tools → SDK Manager and ensure these are installed:
SDK Platforms:
- Android 14 (API 34) or latest
SDK Tools:
- Android SDK Build-Tools
- Android SDK Platform-Tools
- Android Emulator
- Android SDK Command-line Tools
Opening Your Project
From your Capacitor project:
bashnpx cap open android
Or in Android Studio: File → Open → select your android folder.
Wait for Gradle sync to complete (first time takes several minutes).
Project Structure
android/
├── app/
│ ├── src/
│ │ └── main/
│ │ ├── java/ # Kotlin/Java code
│ │ ├── assets/
│ │ │ └── public/ # Your web files (synced)
│ │ ├── res/ # Icons, layouts, strings
│ │ └── AndroidManifest.xml
│ └── build.gradle # App dependencies
├── gradle/
└── build.gradle # Project dependencies
Running on Emulator
Step 1: Create an Emulator
- Go to Tools → Device Manager
- Click Create Device
- Choose a phone (e.g., Pixel 7)
- Select a system image (e.g., API 34)
- Download if needed (click the download icon)
- Click Finish
Step 2: Start Emulator
In Device Manager, click the Play button next to your emulator.
Wait for it to boot (first time is slow).
Step 3: Run Your App
Click the Run button (green play icon) in Android Studio toolbar.
Or press Shift + F10.
Android Emulator uses hardware acceleration. If it's slow:
Windows/Linux: Enable Intel HAXM or AMD Hypervisor macOS: Usually works automatically with Hypervisor.framework
Check: File → Settings → Appearance & Behavior → System Settings → Android SDK → SDK Tools → Intel x86 Emulator Accelerator (HAXM)
Running on Physical Device
Step 1: Enable Developer Mode
On your Android device:
- Go to Settings → About Phone
- Tap Build Number 7 times
- You'll see "You are now a developer!"
Step 2: Enable USB Debugging
- Go to Settings → System → Developer Options
- Enable USB Debugging
- (Optional) Enable Install via USB
Step 3: Connect Device
- Connect your device with a USB cable
- On the device, tap Allow when prompted
- Check Always allow from this computer
Step 4: Run on Device
- Your device appears in the device dropdown
- Select it and click Run
Configuring Your App
App Icons
Replace icons in android/app/src/main/res/:
res/
├── mipmap-mdpi/ # 48x48
├── mipmap-hdpi/ # 72x72
├── mipmap-xhdpi/ # 96x96
├── mipmap-xxhdpi/ # 144x144
├── mipmap-xxxhdpi/ # 192x192
└── mipmap-anydpi-v26/ # Adaptive icons
Use Android Asset Studio to generate all sizes.
Display Name
Edit android/app/src/main/res/values/strings.xml:
xml<resources> <string name="app_name">My Cool App</string> <string name="title_activity_main">My Cool App</string> </resources>
App Colors
Edit android/app/src/main/res/values/styles.xml:
xml<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorPrimary">#FF6200EE</item> <item name="colorPrimaryDark">#FF3700B3</item> <item name="colorAccent">#FF03DAC5</item> </style>
Splash Screen
The splash screen uses android/app/src/main/res/drawable/splash.png.
Configure in capacitor.config.ts:
typescriptconst config: CapacitorConfig = { plugins: { SplashScreen: { launchAutoHide: true, androidScaleType: 'CENTER_CROP', backgroundColor: '#ffffff', }, }, };
AndroidManifest.xml
The manifest controls permissions and app behavior.
Location: android/app/src/main/AndroidManifest.xml
Common Permissions
xml<!-- Internet (usually already present) --> <uses-permission android:name="android.permission.INTERNET" /> <!-- Camera --> <uses-permission android:name="android.permission.CAMERA" /> <!-- Location --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- Storage (for older Android versions) --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Capacitor Plugins Add Permissions
When you install a Capacitor plugin and sync, it usually adds required permissions automatically.
Live Reload Development
Step 1: Find Your Local IP
Step 2: Configure Capacitor
typescript// capacitor.config.ts const config: CapacitorConfig = { server: { url: 'http://YOUR_LOCAL_IP:3000', cleartext: true, }, android: { allowMixedContent: true, // Allow HTTP during development }, };
Step 3: Start Development
Terminal 1:
bashnpm run dev
Terminal 2:
bashnpx cap sync android npx cap run android
Changes appear instantly!
Warning
Ensure your computer and device are on the same network. The device needs to reach your dev server.
Debugging
Chrome DevTools
- Run your app on device/emulator
- Open Chrome on your computer
- Go to
chrome://inspect - Find your app under "Remote Target"
- Click inspect
Full web developer tools!
Logcat
In Android Studio:
- View → Tool Windows → Logcat
- Filter by your app (package name)
- See all logs including
console.logfrom your web code
Common Debug Commands
typescriptimport { Capacitor } from '@capacitor/core'; console.log('Platform:', Capacitor.getPlatform()); console.log('Is Android:', Capacitor.getPlatform() === 'android'); // Log to both web and native console.log('Debug:', { user, data });
Building APK/AAB
Debug APK (for testing)
In Android Studio:
- Build → Build Bundle(s) / APK(s) → Build APK(s)
- Find APK in
android/app/build/outputs/apk/debug/
Release AAB (for Play Store)
-
Generate a signing key (one time):
bashkeytool -genkey -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-key-alias -
Configure signing in
android/app/build.gradle -
Build → Generate Signed Bundle / APK
-
Choose Android App Bundle
Warning
Keep your keystore safe! If you lose it, you can't update your app on the Play Store. Back it up securely.
Common Issues
Frequently Asked Questions
Try:
- File → Invalidate Caches → Invalidate and Restart
- Delete
android/.gradlefolder and sync again - Ensure you have the correct SDK installed
Set ANDROID_HOME environment variable:
bashexport ANDROID_HOME=$HOME/Android/Sdk
Or create android/local.properties:
sdk.dir=/path/to/Android/Sdk
Check Logcat for errors. Common causes:
- Missing internet permission
- Invalid JavaScript (check Chrome DevTools)
- Incorrect manifest configuration
- Check if virtualization is enabled in BIOS
- Ensure HAXM is installed (Intel) or Hypervisor is enabled (AMD)
- Try a different API level image
- Delete and recreate the emulator
- Try a different USB cable (data, not charge-only)
- Enable USB debugging again
- Revoke USB debugging authorizations and reconnect
- Install device drivers (Windows)
Verify Your Understanding
You've completed this lesson if you can:
Next: Add native features to your app with Capacitor plugins.