文章目录
1、方式一:使用 Uniapp 默认示例(不推荐)
(1)相关环境准备
(2)导入打包项目
(3)打包Uniapp为App资源
(4)配置详细过程
2、方式二:自定义 Android 项目(推荐)
(1)创建空项目
(2)修改 build.gradle
(3)修改gradle.properties
(4)修改app/build.gradle
(5)准备证书文件 mykey.keystore
(6)修改app/AndroidManifest.xml
(7)修改 dcloud_control.xml
(8)复制打包资源
(9)连接测试
技术分享区
1、方式一:使用 Uniapp 默认示例(不推荐)
Uniapp 的官方文档有点简洁,对于没有接触过安卓的开发者来说,无疑是晴天霹雳,由于公司的App项目需要对接硬件,所以打包方式被迫变为了Android Studio 离线打包 Uniapp,这里我详细记录一下打包过程,方便以后使用。官方提供的App离线打包可以作为参考
(1)相关环境准备
Android Studio中文社区下载
App离线SDK下载
(2)导入打包项目
下载的App离线SDK下载提供了一个示例项目 导入示例项目,本文于2021年7月17日12:08:25编写,下载的都是最新的软件,编译项目时遇到一个问题,使用Android API 31的版本,总是报错 我直接把31改成了29就OK了,应该是官方的一个Bug 刷新一下配置,导入OK![【Uniapp】Uniapp 使用 Android Studio 实现离线打包_java_04](https://s2.51cto.com/images/blog/202112/17090343_61bbe1efbd4be70358.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
(3)打包Uniapp为App资源
uniapp打包自己的项目为App资源:HBuilder X > 发行 > 原生App-本地打包 > 生成本地打包App资源![【Uniapp】Uniapp 使用 Android Studio 实现离线打包_android_05](https://s2.51cto.com/images/blog/202112/17090343_61bbe1efd781f56720.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
(4)配置详细过程
将uniapp打包后的静态资源复制到这里替换示例项目的 修改dcloud_control.xml配置的appid 然后详细阅读文章
【Uniapp】Android平台签名证书(.keystore)生成指南得到了自己的mykey.keystore文件,并且拿到了SHA1秘钥,后面要使用到
登录 DCloud开发者中心,找到自己创建的Uniapp项目的appID,当然,通常情况下,公司开发的项目基本上都是管理人创建的,这里肯定不会有当前要打包的项目,所有直接新建一个应用,然后复制appID,修改自己项目里的appID即可(提示,manifest.json打开源码视图修改即可)
点击进入需要打包的项目,进行配置之后,拿到appKey 将生成的AppKey配置到项目的AndroidManifest.xml中 然后构建打包App 构建完成后,直接点击local就会自动打开构建后的安装包
中途遇到饶人的问题为打包之后,提示“appKey未定义或配置错误”,详细阅读我的文章,并仔细对比,你就会发现这个问题解决啦。到此,构建完成啦!
2、方式二:自定义 Android 项目(推荐)
(1)创建空项目
![【Uniapp】Uniapp 使用 Android Studio 实现离线打包_android_13](https://s2.51cto.com/images/blog/202112/17090344_61bbe1f0a4bf585049.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184) ![【Uniapp】Uniapp 使用 Android Studio 实现离线打包_maven_14](https://s2.51cto.com/images/blog/202112/17090344_61bbe1f0b55ee36184.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
(2)修改 build.gradle
ext {
// sourceCompatibility = sourceCompatibility
// targetCompatibility = targetCompatibility
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
maven {
url "https://maven.aliyun.com/repository/google"
}
maven {
url "http://maven.aliyun.com/nexus/content/repositories/releases"
}
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
maven {
url 'https://jitpack.io'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven {
url "https://maven.aliyun.com/repository/google"
}
maven {
url "http://maven.aliyun.com/nexus/content/repositories/releases"
}
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
maven {
url 'https://jitpack.io'
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
(3)修改gradle.properties
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app"s APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
android.enableJetifier=true
(4)修改app/build.gradle
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion '30.0.2'
defaultConfig {
applicationId "com.zhhy.spotorder.app.cashier"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
signingConfigs {
config {
keyAlias 'test'
keyPassword '111111'
storeFile file('mykey.keystore')
storePassword '111111'
v1SigningEnabled true
v2SigningEnabled true
}
}
buildTypes {
debug {
signingConfig signingConfigs.config
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
signingConfig signingConfigs.config
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
//使用uniapp时,需复制下面代码
aaptOptions {
additionalParameters '--auto-add-overlay'
//noCompress 'foo', 'bar'
ignoreAssetsPattern "!.svn:!.git:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.2.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation group: 'androidx.recyclerview', name: 'recyclerview', version: '1.2.0'
implementation 'com.facebook.fresco:fresco:1.13.0'
implementation "com.facebook.fresco:animated-gif:1.13.0"
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.alibaba:fastjson:1.1.46.android'
implementation 'com.gitee.Agx58694:sunmi-printing:1.0.9'
}
(5)准备证书文件 mykey.keystore
【Uniapp】Android平台签名证书(.keystore)生成指南
(6)修改app/AndroidManifest.xml
(7)修改 dcloud_control.xml
(8)复制打包资源
使用 HbuilderX 生成 App 打包资源 复制到 main/assets/apps 下面![【Uniapp】Uniapp 使用 Android Studio 实现离线打包_java_16](https://s2.51cto.com/images/blog/202112/17090344_61bbe1f0ebd8e74400.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
(9)连接测试
先用USB连接的你手机,或者需要调试的终端,直接运行看效果,通常情况下都没有问题,最常见的问就是appKey 未配置或配置错误,那么请参照第一节:配置详细过程仔细检查你的配置,那就OK了
技术分享区
![【Uniapp】Uniapp 使用 Android Studio 实现离线打包_maven_17](https://s2.51cto.com/images/blog/202112/17090345_61bbe1f1087ab49740.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
|