Back to Blog

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:

ToolPurpose
Node.js (v18 or above)Build your Next.js app
Android StudioFor building APK/AAB
Java JDK 11+Required for Android builds
Capacitor CLITo integrate Capacitor
Git (optional)Version control

Check versions:

javascript
node -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:

javascript
npx create-next-app@latest portfolio cd portfolio

4. Install Capacitor

Install Capacitor core packages:

javascript
npm install @capacitor/core @capacitor/cli

Initialise Capacitor in your project:

javascript
npx 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:

javascript
import 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:

javascript
npm run build

After build, Next.js will generate a static out/ folder.


7. Add Android Platform

Now add Android support:

javascript
npm 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:

javascript
npx cap sync

8. Fix Common Errors

ErrorCauseFix
❌ Could not find web assets directoryMissing out folderRun npm run build again
❌ Cannot run init for non-JSON configYou have capacitor.config.tsDo not run npx cap init again
❌ android platform has not been addedAndroid not linkedRun npx cap add android
❌ Asset directory not foundMissing icon.pngSee section below

9. Add Custom App Icon and Splash Screen

Install Capacitor Assets tool:

javascript
npm install @capacitor/assets --save-dev

Create an assets/ folder in your root:

javascript
/portfolio/ β”œβ”€ android/ β”œβ”€ assets/ β”‚ β”œβ”€ icon.png β”‚ └─ splash.png (optional)

Generate assets:

javascript
npx capacitor-assets generate --android --iconBackgroundColor "#FFFFFF"

Sync changes:

javascript
npx cap sync android

This replaces the default Capacitor icon with your custom one.


10. Open in Android Studio

javascript
npx 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:

javascript
Build β†’ Build Bundle(s)/APK(s) β†’ Build APK(s)

Output:

javascript
android/app/build/outputs/apk/debug/app-debug.apk

Option 2: Release AAB (for Play Store)

javascript
Build β†’ Generate Signed App Bundle / APK

Follow the wizard:

  1. Create or select your keystore

  2. Enter passwords

  3. Choose Android App Bundle (.aab)

  4. Click Finish

Output:

javascript
android/app/build/outputs/bundle/release/app-release.aab

12. Publish Your App

πŸ…°οΈ Option 1 β€” Google Play Store

  1. Go to Play Console

  2. Pay a one-time $25 fee

  3. Create a new app listing

  4. Upload your .aab file

  5. Fill out:

    • Description

    • Screenshots

    • Privacy Policy

  6. Submit for review
    βœ… Google will publish after 1–3 days.


πŸ…±οΈ Option 2 β€” Publish on Your Website

If you want users to download directly:

  1. Build signed APK:

    javascript
    Build β†’ Build APK(s)
  2. Find the file:

    javascript
    android/app/build/outputs/apk/release/app-release.apk
  3. Rename it (e.g. portfolio-app.apk)

  4. Upload to your website:

    javascript
    /downloads/portfolio-app.apk
  5. Add a download button:

    javascript
    <a href="/downloads/portfolio-app.apk" download> <button>Download Android App</button> </a>
  6. 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:

  1. Add PWA support:

    javascript
    npm install next-pwa
  2. Update next.config.mjs:

    javascript
    import withPWA from 'next-pwa'; const nextConfig = withPWA({ dest: 'public', register: true, skipWaiting: true, output: 'export', }); export default nextConfig;
  3. 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" } ] }
  4. 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:

    javascript
    npm run build npx cap sync
  • Keep a backup of your keystore file (it’s required for updates).

  • Use adb install app-debug.apk to quickly test on your phone.

  • Use npx cap open android whenever you edit web assets.



Based in Greater Noida, Indiaβ€’Currently