Build Your App with Capacitor JS: A Comprehensive Guide
1. Introduction
Capacitor JS allows you to convert any web app (like Next.js) into a native Android or iOS app β without rewriting your frontend.
It wraps your app inside a native WebView and lets you access mobile features like Camera, Storage, Push Notifications, etc.
2. Prerequisites
Before starting, make sure you have:
| Tool | Purpose |
|---|---|
| Node.js (v18 or above) | Build your Next.js app |
| Android Studio | For building APK/AAB |
| Java JDK 11+ | Required for Android builds |
| Capacitor CLI | To integrate Capacitor |
| Git (optional) | Version control |
Check versions:
javascriptnode -v npm -v java -version
3. Create and Build a Next.js App
If you donβt already have a Next.js app, create one:
javascriptnpx create-next-app@latest portfolio cd portfolio
4. Install Capacitor
Install Capacitor core packages:
javascriptnpm install @capacitor/core @capacitor/cli
Initialise Capacitor in your project:
javascriptnpx cap init
When prompted:
-
App name: Portfolio
-
App ID: com.aditya.portfolio
-
Web directory:
out(for static exports)
5. Update Capacitor Config
Open capacitor.config.ts and set this:
javascriptimport type { CapacitorConfig } from '@capacitor/cli'; const config: CapacitorConfig = { appId: 'com.aditya.portfolio', appName: 'Portfolio', webDir: 'out', bundledWebRuntime: false, }; export default config;
6. Export Your Next.js App
Next.js no longer supports next export CLI directly β use the new config option.
In next.config.mjs, add:
javascript/** @type {import('next').NextConfig} */ const nextConfig = { output: 'export', // Required for static build }; export default nextConfig;
Then build:
javascriptnpm run build
After build, Next.js will generate a static out/ folder.
7. Add Android Platform
Now add Android support:
javascriptnpm install @capacitor/android npx cap add android
If you get an error like:
javascript[error] The web assets directory (.\public) must contain an index.html file.
π It means you havenβt built your app yet β run npm run build first.
Then sync your project:
javascriptnpx cap sync
8. Fix Common Errors
| Error | Cause | Fix |
|---|---|---|
β Could not find web assets directory | Missing out folder | Run npm run build again |
β Cannot run init for non-JSON config | You have capacitor.config.ts | Do not run npx cap init again |
β android platform has not been added | Android not linked | Run npx cap add android |
β Asset directory not found | Missing icon.png | See section below |
9. Add Custom App Icon and Splash Screen
Install Capacitor Assets tool:
javascriptnpm install @capacitor/assets --save-dev
Create an assets/ folder in your root:
javascript/portfolio/ ββ android/ ββ assets/ β ββ icon.png β ββ splash.png (optional)
Generate assets:
javascriptnpx capacitor-assets generate --android --iconBackgroundColor "#FFFFFF"
Sync changes:
javascriptnpx cap sync android
This replaces the default Capacitor icon with your custom one.
10. Open in Android Studio
javascriptnpx cap open android
Android Studio will open your native project.
If you see build errors:
-
Click File β Sync Project with Gradle Files
-
Click Build β Clean Project
-
Then Build β Rebuild Project
11. Build the Android App
Option 1: Debug APK
Quick test version:
javascriptBuild β Build Bundle(s)/APK(s) β Build APK(s)
Output:
javascriptandroid/app/build/outputs/apk/debug/app-debug.apk
Option 2: Release AAB (for Play Store)
javascriptBuild β Generate Signed App Bundle / APK
Follow the wizard:
-
Create or select your keystore
-
Enter passwords
-
Choose Android App Bundle (.aab)
-
Click Finish
Output:
javascriptandroid/app/build/outputs/bundle/release/app-release.aab
12. Publish Your App
π °οΈ Option 1 β Google Play Store
-
Go to Play Console
-
Pay a one-time $25 fee
-
Create a new app listing
-
Upload your
.aabfile -
Fill out:
-
Description
-
Screenshots
-
Privacy Policy
-
-
Submit for review
β Google will publish after 1β3 days.
π ±οΈ Option 2 β Publish on Your Website
If you want users to download directly:
-
Build signed APK:
javascriptBuild β Build APK(s) -
Find the file:
javascriptandroid/app/build/outputs/apk/release/app-release.apk -
Rename it (e.g.
portfolio-app.apk) -
Upload to your website:
javascript/downloads/portfolio-app.apk -
Add a download button:
javascript<a href="/downloads/portfolio-app.apk" download> <button>Download Android App</button> </a> -
Optional: Add instructions for users to enable Install from Unknown Sources.
π ΎοΈ Option 3 β As a Web App (PWA)
If you want to host it on your domain:
-
Add PWA support:
javascriptnpm install next-pwa -
Update
next.config.mjs:javascriptimport withPWA from 'next-pwa'; const nextConfig = withPWA({ dest: 'public', register: true, skipWaiting: true, output: 'export', }); export default nextConfig; -
Add a
public/manifest.json:javascript{ "name": "Portfolio", "short_name": "Portfolio", "start_url": "/", "display": "standalone", "background_color": "#ffffff", "theme_color": "#000000", "icons": [ { "src": "/icon.png", "sizes": "512x512", "type": "image/png" } ] } -
Deploy to:
-
Vercel
-
Netlify
-
Firebase Hosting
-
or your own domain
-
β Your site will become installable on Android and desktop browsers.
13. Pro Tips
-
Always rebuild after editing anything in Next.js before syncing:
javascriptnpm run build npx cap sync -
Keep a backup of your keystore file (itβs required for updates).
-
Use
adb install app-debug.apkto quickly test on your phone. -
Use
npx cap open androidwhenever you edit web assets.