vue项目通过Android Studio打包成android应用

10 阅读2分钟

软件安装地址:developer.android.google.cn/studio 版本2021.1(新版本操作不太一样)

项目打包依赖:@capacitor/android @capacitor/ios

npm i @capacitor/android @capacitor/ios

打开项目控制台:

第一步初始化环境:npx cap init

第二步生成android文件:npx cap add android

生成后需要更改三个文件:

android/gradle/wrapper/gradle-wrapper.properties(配置软件里构建工具下载地址,自动生成的地址很难下载,需要改成国内镜像):distributionUrl=services.gradle.org/distributio… 改成 mirrors.huaweicloud.com/gradle/grad…

android/app/src/main/AndroidManifest.xml:

在<uses-permission android:name="android.permission.INTERNET" /> 下面一行加上 <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> 用于下一次部署后自动下载更新用

android/app/src/main/res/values/strings.xml:

<string name="app_name">你的app名称</string>
<string name="title_activity_main">你的app名称</string>
<string name="package_name">app标识(用于识别app唯一性的,两个项目如果用同样的标识最后apk会互相覆盖不能同时安装)</string>
<string name="custom_url_scheme">app标识</string>

第三步将项目代码打包成dist文件夹:npm run build(以后每次更新服务器代码前都需要先打包)

第四步同步最新的dist包:npx cap sync(每次重新打包都需要执行,否则app就是上一次的代码)

第五步打开android studio开始构建apk:npx cap open android

软件打开后会自动下载依赖工具,所有工具下载完后可以通过File/New/Image Asset更改app的图标,打开File/Project Structure版本改成4.2.1/7.2,最后打开File/Settings/Build/Build Tools/Gradle/Gradle JDk选择下载JDK版本11,基础打包配置就完成了

选择目录Gradle Scripts下第四个文件build.gradle,将versionName改成1.0.0(版本号,默认是1.0,后面每次打包都需要改此处的版本号),然后选择顶部工具栏Build/Generate Signed Bundle or APK => 选择APK => 配置key => 项目目录\android\app\release(成功后会在此目录生成apk包)

服务器部署目录下创建更新xml文件(和apk同级)update.xml:

<update>

<version>1.0.0(每次版本更新修改同步版本号)</version>

<url>服务器项目访问地址/app-release.apk</url>

</update>

同级创建一个文件夹WEB-INF,下面创建web.xml文件(用于告诉手机端有包更新):

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -->

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true">

<filter>

<filter-name>CorsFilter</filter-name>

<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>

<init-param>

<param-name>cors.allowed.origins</param-name>

<param-value>*</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>CorsFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

</web-app>