打包背景:

因为使用uni-app云打包的app,在安卓4.4上运行不了,故而需要使用本地打包的方式解决此问题。

话不多说,直接上手操作,具体如下:

一、开发环境准备

参考官网操作 :开发环境 | uni小程序SDK

需要注意,App 离线 SDK版本不要使用最新的,我目前使用“Android-SDK@3.6.4.81503_20220926” 此版本是可以正常打包运行的,不过官网已经找不到此版本下载链接,如果需要,可以私信我,我转发给你,或者是在官网的QQ群里面问,里面会有很多热心的大佬,说不定刚好他们手上有此版本,这样你就可以问他们给了,我也是通过官方群问群里大佬拿到的,在此感谢 群里 “初学者”提供的SDK版本

二、系统环境变量设置

1、新增系统变量:ANDROID_HOME ,设置目录到类似目录“C:\Users\gxsyh\AppData\Local\Android\Sdk”

接着在系统变量Path上新增5个变量:

C:\Program Files\Java\jre-1.8\bin 【这里要看自己实际安装目录】

%ANDROID_HOME%\platform-tools

%ANDROID_HOME%\emulator

%ANDROID_HOME%\tools

%ANDROID_HOME%\tools\bin

如下图

2、用户变量设置

新增Android_Home变量,并设置

C:\Users\gxsyh\AppData\Local\Android\Sdk 和C:\Users\gxsyh\AppData\Local\Android\Sdk\platform-tools

最后修改Path路径变量

C:\Program Files\Java\jre-1.8\bin 和 %Android_Home%

三、导入工程项目

1、先解压Android-SDK@3.6.4.81503_20220926,并导入项目HBuilder-HelloUniApp【对应 一的第三步:App 离线 SDK 下载】

路径说明:

HBuilder-HelloUniApp:专门用来做打包的,注意打包必须使用此环境

**HBuilder-Integrate-AS:**调试的版本,打正式包不要使用此项目

需要注意,目录不能有中文名称

2、Uni-app本地打包资源生成

点击 发行-》原生App本地打包-》生成本地App资源包

3、复制uni-app生成的资源包到项目apps目录

目录地址例如:D:\01-MyWoekFile\03-HBuilderX\01-build\Android-SDK@3.6.4.81503_20220926\HBuilder-HelloUniApp\app\src\main\assets\apps

四、证书设置

1、登录后台 ,找到云证书【云证书在uni-app生成云打包会自动生成】

登录 (dcloud.net.cn)

2、AndroidManifest.xml文件设置

路径:D:\01-MyWoekFile\03-HBuilderX\01-build\Android-SDK@3.6.4.81503_20220926\HBuilder-HelloUniApp\app\src\main

3、设置dcloud_control.xml

路径:D:\01-MyWoekFile\03-HBuilderX\01-build\Android-SDK@3.6.4.81503_20220926\HBuilder-HelloUniApp\app\src\main\assets\data

4、设置strings.xml 的名称

路径:D:\01-MyWoekFile\03-HBuilderX\01-build\Android-SDK@3.6.4.81503_20220926\HBuilder-HelloUniApp\app\src\main\res\values

5、设置 build.gradle 阿里下载路径【也可以不用设置】

maven() {

url 'https://maven.aliyun.com/repository/jcenter'

}

maven(){

url 'https://maven.aliyun.com/repository/google'

}

maven {

url 'https://maven.aliyun.com/repository/public'

}

maven {

url 'https://maven.aliyun.com/repository/mapr-public'

}

如图:

6、打开云证书并下载证书

7、设置 build.gradle 证书信息配置节点 signingConfigs

例如下面

signingConfigs {

config {

keyAlias '__UNI__159FB9C'

keyPassword 'T1IsdfLh'

storeFile file('D:\\01-MyWoekFile\\03-HBuilderX\\01-build\\Android-SDK@3.6.4.81503_20220926\\HBuilder-Integrate-AS\\simpleDemo\\3051be6ebb0c37e9dbf61cf76feb758f.keystore')

storePassword 'T1IsdfLh'

v1SigningEnabled true

v2SigningEnabled true

}

}

如图

8、设置 build.gradle 文件 android 的节点

接着设置Gradle 和环境依赖版本

8、build.gradle 打包 低版本文件例子

apply plugin: 'com.android.application'

android {

compileSdkVersion 29

buildToolsVersion '29'

defaultConfig {

applicationId "uni.UNI__B98FD4F"

minSdkVersion 19

targetSdkVersion 19

versionCode 100

versionName "1.0.0"

multiDexEnabled true

ndk {

abiFilters 'x86', 'armeabi-v7a', 'arm64-v8a'

}

manifestPlaceholders = [

"apk.applicationId" : "com.android.HelloH5",

"GETUI_APPID" : "unipush的appid",

"plus.unipush.appid" : "unipush的appid",

"plus.unipush.appkey" : "unipuish的appkey",

"plus.unipush.appsecret": "unipush的secrety"

]

compileOptions {

sourceCompatibility JavaVersion.VERSION_1_8

targetCompatibility JavaVersion.VERSION_1_8

}

}

signingConfigs {

config {

keyAlias '__UNI__B98FD4F'

keyPassword '你的实际密码'

storeFile file('C:\\Users\\gxsyh\\Downloads\\c38d5128bb2728d20c3cfebfdcd578eb.keystore')

storePassword '你的实际密码'

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.txt'), 'proguard-rules.pro'

}

}

lintOptions {

checkReleaseBuilds false

abortOnError false

}

//使用uniapp时,需复制下面代码

/*代码开始*/

aaptOptions {

additionalParameters '--auto-add-overlay'

//noCompress 'foo', 'bar'

ignoreAssetsPattern "!.svn:!.git:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"

}

/*代码结束*/

}

repositories {

flatDir {

dirs 'libs'

}

}

dependencies {

implementation fileTree(include: ['*.jar'], dir: 'libs')

implementation fileTree(include: ['*.aar'], dir: 'libs')

implementation 'androidx.appcompat:appcompat:1.0.0'

implementation 'androidx.legacy:legacy-support-v4:1.0.0'

/*uniapp所需库-----------------------开始*/

implementation 'androidx.recyclerview:recyclerview:1.0.0'

implementation 'com.facebook.fresco:fresco:2.5.0'

implementation "com.facebook.fresco:animated-gif:2.5.0"

/*uniapp所需库-----------------------结束*/

// 基座需要,必须添加

implementation 'com.github.bumptech.glide:glide:4.9.0'

implementation 'com.alibaba:fastjson:1.1.46.android'

}

五、开始打包app

1、先编译,没有错误才能打包

2、编译通过,开始打包

3、打包成功

路径:D:\01-MyWoekFile\03-HBuilderX\01-build\Android-SDK@3.6.4.81503_20220926\HBuilder-HelloUniApp\app\debug

4、打包兼容安卓4.4操作系统设置【就是最低版本兼容19】

六、打包常见错误处理

1、设置最小兼容版本是19,打包却提示是21

解决办法:直接点击提示文件,打开后修改最低版本号为19,如下图

C:\Users\gxsyh.gradle\caches\transforms-3\2f4e82feceb3692362bfb1e4a7791b5d\transformed\jetified-oaid_sdk_1.0.25

2、打包在安卓4.4项目上面跑的话,必须要选择.apk打包

3、打包后安装,运行提示编译版本不一致

解决办法,打开源码,新增以下代码

"compatible": {

"ignoreVersion": true //true表示忽略版本检查提示框,HBuilderX1.9.0及以上版本支持

},

如图

七、AndroidManifest.xml配置文件例子

xmlns:tools="http://schemas.android.com/tools"

package="uni.UNIA410556">

android:name="io.dcloud.application.DCloudApplication"

android:allowClearUserData="true"

android:icon="@drawable/icon"

android:label="@string/app_name"

android:largeHeap="true"

android:debuggable="true"

>

android:name="io.dcloud.PandoraEntry"

android:configChanges="orientation|keyboardHidden|keyboard|navigation"

android:label="@string/app_name"

android:launchMode="singleTask"

android:hardwareAccelerated="true"

android:theme="@style/TranslucentTheme"

android:screenOrientation="user"

android:windowSoftInputMode="adjustResize" >

android:name="UMENG_APPKEY"

android:value="友盟appkey" />

android:name="UMENG_CHANNEL"

android:value="友盟渠道" />

android:name="com.baidu.lbsapi.API_KEY"

android:value="百度key"/>

android:name="com.baidu.location.f"

android:enabled="true"

android:process=":remote"/>

android:value="高德key"/>

android:name="QQ_APPID"

android:value="QQ的appid" />

android:name="com.tencent.tauth.AuthActivity"

android:launchMode="singleTask"

android:noHistory="true">

android:name="com.tencent.connect.common.AssistActivity"

android:screenOrientation="portrait"

android:theme="@android:style/Theme.Translucent.NoTitleBar" />

android:name="SINA_REDIRECT_URI"

android:value="微博的回调地址" />

android:name="SINA_SECRET"

android:value="微博的secret" />

android:name="SINA_APPKEY"

android:value="_微博的appkey" />

android:name="WX_SECRET"

android:value="微信的secret" />

android:name="WX_APPID"

android:value="微信的appid" />

android:name=".wxapi.WXEntryActivity"

android:exported="true"

android:label="@string/app_name"

android:launchMode="singleTop">

android:name=".wxapi.WXPayEntryActivity"

android:exported="true"

android:launchMode="singleTop" />

android:name="WX_APPID"

android:value="微信的appid" />

android:name="MIUI_APPID"

android:value="_%小米分享的APPID%" />

android:name="MIUI_APPSECRET"

android:value="%小米分享的appSecret%" />

android:name="MIUI_REDIRECT_URI"

android:value="%小米分享的回调地址%" />

android:name="io.dcloud.PandoraEntryActivity"

android:launchMode="singleTask"

android:configChanges="orientation|keyboardHidden|screenSize|mcc|mnc|fontScale|keyboard|smallestScreenSize|screenLayout|screenSize"

android:hardwareAccelerated="true"

android:permission="com.miui.securitycenter.permission.AppPermissionsEditor"

android:screenOrientation="user"

android:theme="@style/DCloudTheme"

android:windowSoftInputMode="adjustResize">

android:name="android.intent.category.DEFAULT" />

android:name="android.intent.category.BROWSABLE" />

android:name="android.intent.action.VIEW" />

android:scheme=" " />

android:name="dcloud_appkey"

android:value="4d4f86e31e0b1dd0dc26965d6f9683dd" />

需要注意

推荐链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。