文章

Web App To Android native App

如何使用capaitor将web app 封装成android app,即封装成可安装的apk

Web APP到Android 原生APP

前言:

环境:OS:Windows 11

前端框架:Sveltekit

需求/准备:

  • 一个可正常构建的项目(即可正常通过执行 npm run build构建并打包成一个builddist的输出目录)
  • nodejs
  • android studio
  • android sdk
  • vscode ide(可选)

安装android studio并安装android sdk

访问官网:下载 Android Studio 和应用工具 - Android 开发者 | Android Developers

下载android studio

image-20240808220041619

image-20240808220100867

安装并启动android studio

image-20240808220820112

image-20240808220843176

image-20240808220849897

image-20240808220855438

image-20240808220904573

image-20240808220915832

打开SDK manager

image-20240808221237189

image-20240808221249489

选择一个安卓版本的sdk,这里我选择Android 14

image-20240808221406978

其他安装(可选)

image-20240808221457136

image-20240808221514899

image-20240808221529149

image-20240808221542378

image-20240808221644243

点击”Apply”确认安装

因网络问题无法正常下载sdk等解决方案

  • 打开设置

    image-20240808221842952

  • 找到代理设置,设置代理端口(与自己使用的代理软件监听端口一致)

    image-20240808221950198

    image-20240808222056667

在项目中安装capacitor

官方文档:[Capacitor Android DocumentationCapacitor Documentation (capacitorjs.com)](https://capacitorjs.com/docs/android)

在项目目录下执行npm install安装项目依赖(如果已安装可忽略)

1
npm install

image-20240808213939141

执行npm i @capacitor/core安装capacitor核心包

1
npm i @capacitor/core

执行npm i -D @capacitor/cli安装capacitor command line interface (命令行接口)包

1
npm i -D @capacitor/cli

执行npx cap init初始化capacitor配置,依次输入app名和项目ID,回车确认

1
npx cap init

image-20240808215213709

执行npm install @capacitor/android安装@capacitor/android

1
npm install @capacitor/android

image-20240808214245668

执行npx cap add android创建安卓项目,成功创建后将生成android项目目录

1
npx cap add android

image-20240808215557219

image-20240808215706026

构建项目并同步到安卓项目中

修改svelte.config.js配置文件,禁用预压缩,目的是在vite build过程中不要生成.gz.br压缩文件,避免在后续安卓构建过程中出现”Duplicate resources”的问题(因文件重名导致)

官方文档相关说明Static site generation • Docs • SvelteKit

precompress

If true, precompresses files with brotli and gzip. This will generate .br and .gz files.

// svelte.config.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import adapter from '@sveltejs/adapter-static';

import preprocess from 'svelte-preprocess';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	// Consult https://github.com/sveltejs/svelte-preprocess
	// for more information about preprocessors
	preprocess: preprocess(),

	onwarn: (warning, handler) => {
		if (warning.code.startsWith('a11y-')) {
			return;
		}
		handler(warning);
	},

	kit: {
		adapter: adapter({
			// default options are shown. On some platforms
			// these options are set automatically — see below
			pages: 'build',
			assets: 'build',
			fallback: 'index.html',
			precompress: false, // 禁用预压缩
			strict: true,
		}),

	}
};

export default config;

image-20240808225709443

执行npm run build构建web项目

1
npm run build

image-20240808225541397

image-20240808225550738

修改capacitor.config.tsweb项目目录配置

1
2
3
4
5
6
7
8
9
import type { CapacitorConfig } from '@capacitor/cli';

const config: CapacitorConfig = {
  appId: 'com.zpekii.app',
  appName: 'myapp',
  webDir: 'build'// 默认为"dist",因为sveltekit构建后生成的是"build"目录,所以我需要做相应更改以正确执行后续的拷贝操作
};

export default config;

执行npx cap sync同步到安卓项目中,此步骤将会把构建好的web项目拷贝到安卓项目指定目录下

1
npx cap sync

image-20240808230816735

执行成功后可见目录”/android/app/src/main/assetss/public/“下拷贝过来的web项目

在Android Studio中执行安卓apk打包

回到Android studio,打开安卓项目,打开项目文件后会自动执行build操作

image-20240808231225765

可点击下方”Build”查看build情况,若在build过程中遇到网络问题,可参照上面提供的方案尝试解决

image-20240808231336005

image-20240808231413195

打包成发布版本apk

image-20240808232501498

image-20240808232507805

image-20240808232515825

image-20240808232535833

image-20240808231508262

image-20240808231511006

image-20240808232156964

Apk生成位置: /android/app/build/outputs/apk/release/*.apk

生成的发布Apk是未签名的,不可直接安装,需要进行签名

**官方文档(需科学上网):[Sign your appAndroid StudioAndroid Developers](https://developer.android.com/studio/publish/app-signing#sign-apk)**

image-20240809121156622

image-20240809121233797

image-20240809121311962

image-20240809121622323

image-20240809121605244

image-20240809121702618

image-20240809121707206

image-20240809121825621

最后,即可使用真机或安卓模拟器安装已签名的apk进行测试或使用

本文由作者按照 CC BY 4.0 进行授权