導入
__CAPGO_KEEP_0__ このチュートリアルでは、Next.js 15プロジェクトを作成し、Capacitor 8を使用してiOSとAndroidのネイティブモバイルアプリケーションに変換する方法を説明します。 8.
このチュートリアルを終了すると、シミュレータ上で動作するワークショップモバイルアプリケーションを手に入れ、開発を続け、最終的にはApp StoreとGoogle Playに公開することができます。
所要時間: ~30分
作成するもの:
- App Routerを使用した新しいNext.js 15プロジェクト
- モバイル用の静的エクスポート設定
- Capacitor 8に必要なプラグイン
- ネイティブiOSおよびAndroidアプリ
- ライブリロード開発環境
既存のNext.jsアプリがある場合は Convert Your Next.js App to Mobile の代わりにご確認ください。
前提条件
インストールされていることを確認してください:
- Node.js 18+ (
node --version) - Bun )
curl -fsSL https://bun.sh/install | bash) - パッケージマネージャーで確認してください ( Xcode
- (macOS専用、iOS開発用) Android Studio
(Android開発用)
ステップ 1: 新しい Next.js プロジェクトを作成する
bunx create-next-app@latest my-mobile-app
新しい Next.js 15 プロジェクトを作成してください:
- TypeScript: Yes (recommended)
- ESLint: Yes
- Tailwind CSS: Yes (mobile stylingのための推奨)
src/directory: Yes- App Router: Yes (推奨)
- Import alias: Default (
@/*)
プロジェクトに移動してください:
cd my-mobile-app
ステップ 2: Next.js を静的エクスポート用に設定する
Capacitor は静的 HTML/JS/CSS ファイルが必要です。Next.js を静的エクスポート用に設定するには、 next.config.ts:
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {
output: 'export',
images: {
unoptimized: true,
},
// Ensure trailing slashes for proper routing in Capacitor
trailingSlash: true,
};
export default nextConfig;
なぜこれらの設定が必要なのか?
output: 'export'— 静的 HTML を生成し、Node.js サーバーが必要なくなるimages: { unoptimized: true }— Next.js の画像最適化を無効にする (サーバーが必要)trailingSlash: true— ネイティブ WebView で適切なルーティングが保証される
ステップ 3: モバイル スクリプトを追加する
モバイル開発用のスクリプトを更新してください: package.json ビルドをテストしてください:
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"mobile": "bun run build && bunx cap sync",
"mobile:ios": "bun run mobile && bunx cap open ios",
"mobile:android": "bun run mobile && bunx cap open android"
}
}
ビルドが正常に動作していることを確認してください:
bun run build
正常に動作していることを確認してください: out __CAPGO_KEEP_0__で管理する静的ファイルのディレクトリ。
ステップ 4: Capacitor 8 をインストールします。
Capacitor のコアパッケージをインストールします:
bun add @capacitor/core
bun add -D @capacitor/cli
ほとんどのモバイルアプリが必要とする基本的なプラグインをインストールします:
bun add @capacitor/app @capacitor/keyboard @capacitor/splash-screen @capacitor/status-bar @capacitor/preferences
これらのプラグインは何を実行するか:
- @capacitor/app — フォアグラウンド/バックグラウンド、アプリ内リンクのライフサイクルイベント
- @capacitor/keyboard — キーボードの動作を制御
- @capacitor/splash-screen — ネイティブのスプラッシュスクリーンを制御
- @capacitor/status-bar — デバイス ステータス バーのスタイルを設定
- @capacitor/preference — キー値ストレージ (localStorage と同様のネイティブ)
ステップ 5: Capacitor を初期化
Capacitor をプロジェクトの詳細と初期化
bunx cap init "My Mobile App" com.example.mymobileapp --web-dir out
置き換え:
"My Mobile App"アプリの表示名com.example.mymobileappアプリ ID (逆ドメイン表記)
これにより capacitor.config.ts. プラグインの構成で更新
import type { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
appId: 'com.example.mymobileapp',
appName: 'My Mobile App',
webDir: 'out',
plugins: {
SplashScreen: {
launchShowDuration: 2000,
launchAutoHide: true,
androidScaleType: 'CENTER_CROP',
splashFullScreen: true,
splashImmersive: true,
},
Keyboard: {
resize: 'body',
resizeOnFullScreen: true,
},
StatusBar: {
style: 'light',
},
},
};
export default config;
ステップ 6: ネイティブ プラットフォームを追加
プラットフォーム パッケージをインストール
bun add @capacitor/ios @capacitor/android
ネイティブプロジェクトを生成する
bunx cap add ios
bunx cap add android
これにより ios と android ネイティブプロジェクトが含まれるディレクトリが作成されます。
ステップ 7: ビルドと実行
プロジェクトをビルドし、ネイティブプラットフォームと同期する
bun run mobile
iOS シミュレータで開く:
bun run mobile:ios
または Android エミュレータで開く:
bun run mobile:android
Xcode (iOS) で:
- デバイス ドロップダウンからシミュレータを選択
- プレイ ボタンをクリックまたは
Cmd + R
Android Studio で:
- GradleのSyncを待ってください
- デバイスのドロップダウンからエミュレータを選択してください
- Runボタンをクリックするか
Shift + F10
ステップ8: Live Reloadを設定してください
開発のために、変更が即座にデバイスに表示されるようにLive Reloadを有効にします。
- ローカルIPアドレスを探してください:
# macOS
ipconfig getifaddr en0
# Windows
ipconfig
- 開発用のCapacitorを生成してください。Add to
capacitor.config.ts:
import type { CapacitorConfig } from '@capacitor/cli';
const devConfig: CapacitorConfig = {
appId: 'com.example.mymobileapp',
appName: 'My Mobile App',
webDir: 'out',
server: {
url: 'http://YOUR_IP_ADDRESS:3000',
cleartext: true,
},
plugins: {
// ... same plugin config
},
};
const prodConfig: CapacitorConfig = {
appId: 'com.example.mymobileapp',
appName: 'My Mobile App',
webDir: 'out',
plugins: {
// ... same plugin config
},
};
const config = process.env.NODE_ENV === 'development' ? devConfig : prodConfig;
export default config;
- デバイスサーバーを起動し、configをnativeにコピーしてください:
bun run dev &
NODE_ENV=development bunx cap copy
- Xcode/Android Studioで再構築してください
Next.jsのcodeの編集は、デバイスで即座に反映されます。
ステップ9: 最初のモバイルスクリーンを作成してください
簡単なモバイルフレンドリーなホームスクリーンを作成してみましょう。Update src/app/page.tsx:
'use client';
import { useEffect, useState } from 'react';
import { App } from '@capacitor/app';
import { Keyboard } from '@capacitor/keyboard';
export default function Home() {
const [appInfo, setAppInfo] = useState<{ name: string; version: string } | null>(null);
useEffect(() => {
// Get app info on mount
App.getInfo().then(setAppInfo).catch(console.error);
// Handle back button on Android
const backHandler = App.addListener('backButton', ({ canGoBack }) => {
if (!canGoBack) {
App.exitApp();
} else {
window.history.back();
}
});
// Hide keyboard when tapping outside inputs
const keyboardHandler = Keyboard.addListener('keyboardWillShow', () => {
document.body.classList.add('keyboard-open');
});
return () => {
backHandler.then(h => h.remove());
keyboardHandler.then(h => h.remove());
};
}, []);
return (
<main className="min-h-screen bg-linear-to-b from-blue-500 to-blue-700 flex flex-col items-center justify-center p-6 text-white">
<h1 className="text-4xl font-bold mb-4">My Mobile App</h1>
<p className="text-xl mb-8 text-center opacity-90">
Built with Next.js 15 + Capacitor 8
</p>
{appInfo && (
<div className="bg-white/20 rounded-lg p-4 backdrop-blur-sm">
<p className="text-sm">
{appInfo.name} v{appInfo.version}
</p>
</div>
)}
<div className="mt-12 space-y-4 w-full max-w-sm">
<button className="w-full py-4 px-6 bg-white text-blue-600 rounded-xl font-semibold text-lg shadow-lg active:scale-95 transition-transform">
Get Started
</button>
<button className="w-full py-4 px-6 bg-white/20 text-white rounded-xl font-semibold text-lg backdrop-blur-sm active:scale-95 transition-transform">
Learn More
</button>
</div>
</main>
);
}
Step 10: エアサイスターを定义」
パッシムパックはパッシムパックをがだです。パッシムパックを定义」エアサイスターを定义」パッシムパックをがだです。
プログ src/app/globals.css:
@tailwind base;
@tailwind components;
@tailwind utilities;
:root {
--sat: env(safe-area-inset-top);
--sar: env(safe-area-inset-right);
--sab: env(safe-area-inset-bottom);
--sal: env(safe-area-inset-left);
}
body {
padding-top: var(--sat);
padding-right: var(--sar);
padding-bottom: var(--sab);
padding-left: var(--sal);
}
/* Prevent text selection on mobile */
* {
-webkit-user-select: none;
user-select: none;
-webkit-tap-highlight-color: transparent;
}
/* Allow text selection in inputs */
input, textarea {
-webkit-user-select: auto;
user-select: auto;
}
/* Keyboard handling */
.keyboard-open {
--sab: 0px;
}
エアサイスターを定义」
パッシムパックは、エアサイスターを定义」、パッシムパックをがだです。
my-mobile-app/
├── android/ # Android native project
├── ios/ # iOS native project
├── out/ # Static build output
├── src/
│ ├── app/
│ │ ├── globals.css
│ │ ├── layout.tsx
│ │ └── page.tsx
│ └── ...
├── capacitor.config.ts # Capacitor configuration
├── next.config.ts # Next.js configuration
├── package.json
└── ...
パッシムパックは、エアサイスターを定义」、パッシムパックをがだです。
エアサイスターを定义」、パッシムパックをがだです。パッシムパックを定义」、エアサイスターを定义」パッシムパックをがだです。
エアサイスターを定义」、パッシムパックをがだです。パッシムパックを定义」、エアサイスターを定义」パッシムパックをがだです。
- エアサイスターを定义」、パッシムパックをがだです。パッシムパックを定义」、エアサイスターを定义」パッシムパックをがだです。 エアサイスターを定义」、パッシムパックをがだです。パッシムパックを定义」、エアサイスターを定义」パッシムパックをがだです。
ios/App/App/Assets.xcassetsエアサイスターを定义」、パッシムパックをがだです。パッシムパックを定义」、エアサイスターを定义」パッシムパックをがだです。android/app/src/main/res - エアサイスターを定义」、パッシムパックをがだです。パッシムパックを定义」、エアサイスターを定义」パッシムパックをがだです。 ネイティブプロジェクトでカスタマイズするか、または使用する
@capacitor/splash-screenconfig - Deep Links: アプリのURLスキームを設定する
機能を追加する
- カメラ:
bun add @capacitor/camera - 位置情報:
bun add @capacitor/geolocation - プッシュ通知:
bun add @capacitor/push-notifications - ファイルシステム:
bun add @capacitor/filesystem
ネイティブUIとトランジション
Konsta UIの代わりにCapgoプラグインを使用してネイティブモバイルのフィールを実現する
- @capgo/capacitor-native-navigation ---Liquid Glassタブバーとネイティブナビゲーションバー
- @capgo/capacitor-transitions ---ネイティブフィーリングのページ遷移
bun add @capgo/capacitor-native-navigation @capgo/capacitor-transitions
bunx cap sync
Tailwindのセーフエリアを追加するには @capgo/tailwind-capacitor:
bun add -D tailwind-capacitor
See Using @capgo/capacitor-native-navigation, Using @capgo/capacitor-transitions, および tailwind-capacitor Next.js用の設定のために
iOSレイアウトの問題を修正する(ビューポート、セーフエリア、水平オーバーフロー)
iOSでコンテンツが切り取られ、ずれ、水平方向にスクロールできるように見えている場合、追加する overflow-x: hidden またはビューポートタグを調整するだけでは通常解決しないことがあります。順序に従ってこれらのチェックを実行してください。
ビューポートメタタグが正しく適用されていることを確認してください
App Router (app/export viewport から app/layout.tsx:
import type { Viewport } from 'next';
export const viewport: Viewport = {
width: 'device-width',
initialScale: 1,
viewportFit: 'cover',
};
Pages Router (pages/ビューポートメタタグを pages/_app.tsx、ではなく _document.tsx.
iOSのセーフエリアを1つのルートラッパーからのみハンドルする
単一のアプリシェルを作成し、セーフエリアのパディングをそこに適用するのではなく、複数のネストされたコンポーネントに適用しないようにします:
html,
body,
#__next {
width: 100%;
min-height: 100%;
margin: 0;
padding: 0;
overflow-x: hidden;
}
* {
box-sizing: border-box;
}
.app-shell {
min-height: 100dvh;
width: 100%;
padding-top: env(safe-area-inset-top);
padding-right: env(safe-area-inset-right);
padding-bottom: env(safe-area-inset-bottom);
padding-left: env(safe-area-inset-left);
}
すべてのページコンテンツを .app-shell. iOS のセーフエリアの安全なパディングをヘッダー、モーダル、レイアウトラッパーに複数回適用すると、UI が切り取られたり大きすぎるように見えることがあります。
With @capgo/tailwind-capacitorで、同じパディングを表現することができます。 pt-safe pb-safe px-safe on その単一のシェル。
Set Capacitor iOS contentInset を never に設定します。
最初に capacitor.config.tsIn contentInsetMode: 'css', 自然のインセットを無効にして、CSS (またはネイティブナビゲーションの
const config: CapacitorConfig = {
appId: 'com.example.myapp',
appName: 'my-app',
webDir: 'out',
ios: {
contentInset: 'never',
},
};
Mixing Capacitor’s automatic content inset with CSS env(safe-area-inset-*) 余白は、ダブルスペースの原因となることが多い。
実際にオーバーフローを起こしている要素を探してください。
通常の原因は、固定ピクセル幅の要素や大きい要素を使用していることです。 100vw, Tailwind w-screen, 固定ピクセル幅の要素や大きい要素を使用している場合など min-width.
Safari Web インスペクターで実行してください:
[...document.querySelectorAll('*')]
.filter(el => el.scrollWidth > document.documentElement.clientWidth)
.map(el => ({
el,
tag: el.tagName,
class: el.className,
scrollWidth: el.scrollWidth,
clientWidth: document.documentElement.clientWidth,
}));
Tailwindの場合、 w-screen 可能な限り w-full 水平方向のオーバーフロー問題は、 100vw / w-screen安全エリアの余白が重複している場合や固定幅のコンテナが使用されている場合などではなく、
ビューポートのメタタグ自体が原因となることはまれです。
オーバー・ザ・エア更新 Capgo アプリの更新をストアへの再提出なしで送信する:
bunx @capgo/cli init
トラブルシューティング
ビルドが「モジュールが見つかりません」というエラーで失敗します。
実行 bun install 再度試してください。
iOS: 「署名のアイデンティティが見つかりません」 Xcodeを開き、Signing & Capabilitiesに移動し、開発チームを選択してください。
Android: 「SDK」の場所が見つかりません。
作成 android/local.properties と sdk.dir=/path/to/android/sdk
デバイスに表示されない変更があります
__CAPGO_KEEP_0__を確認してください bun run mobile 変更を加えた後、__CAPGO_KEEP_0__を実行してください。ライブリロードの場合、IPアドレスが正しいかどうかと、開発サーバーが実行中かどうかを確認してください。
リソース
- Capacitor 8 ドキュメント
- Next.js 15 ドキュメント
- Capgo - ライブアップデート
- capgo/capacitor-ネイティブナビゲーション
- capgo/capacitor-トランジション
- capgo/Tailwind-capacitor
アプリを配信する準備ができましたか? Capgoが更新を迅速に配信できるように助けます — 無料アカウントに登録してください 今日
Build a Next.js Mobile App from Scratch with Capacitor 8
あなたが Build a Next.js Mobile App from Scratch with Capacitor 8 CI/CDの自動化を計画するには Capgo CI/CD 製品ワークフローにおけるCapgo CI/CD Capgo Native Builds 製品ワークフローにおけるCapgo Native Builds Capgo Integrations 製品ワークフローにおけるCapgo Integrations CI/CD統合 CI/CD統合の実装詳細 GitHub アクション統合 GitHub アクション統合の実装詳細について。