此文基于MUI、HBuilderX、H5+開發(fā)的APP本地打包,不適用uniapp。
同款iOS的在這里: MUI、HBuilderX、H5+開發(fā)APP本地打包(iOS)
為什么本地打包
使用HBuilderX開發(fā)完項目后打包-發(fā)布, 通常情況下直接選擇【app云打包】直接在線打包然后下載安裝包,簡單、方便。
一般情況下這種方式滿足了大部分需求,但同時也有幾個問題:
- 1、項目代碼提交官方后臺(官方保證不保留任何用戶代碼,個人感覺也沒啥問題)。
- 2、云打包有文件大小限制,超過40M的文件打包需要提交申請,然后付費。對于我們測試某個問題,一天打包很多次也是不少的費用。
我們的APP包很大,如果云打包,不合適。所以必須自己本地打包。
工具準備
- 開發(fā)工具 :
HBuilderX(版本2.4.6.20191210) - 安卓打包SDK:
Android-SDK@2.4.6.71983_20191211,版本要和HBuilderX保持一致,一般都是更新到官方最新版就可以了。 - 打包工具:Android Studio 3.5.2
操作步驟
1、Android Studio創(chuàng)建一個工程,刪除如下無用文件

2、復制SDK->libs->lib.5plus.base-release.aar文件,及其他所需的庫文件到原生工程工程的app->libs目錄下

3、新建如下目錄層級,導入有HBuilderx導出的APP資源文件

apps.xxxx.www: xxx為HB中appid,appid為應用資源manifest.json文件中id節(jié)點的值,必須保持一致否則APP無法運行。
4、導入data目錄文件,并修改dcloud_control.xml文件的apps->app->appid屬性的值改為當前應用manifest.json文件id節(jié)點的值

5、替換res->drawble目錄應用的圖標文件

6、修改配置文件
AndroidManifest.xml,
build.gradle,
這一步至關重要,稍有不慎APP或無法運行或 閃退、或相關功能不起作用、無法獲取相關權限。最終配置如下;
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gech.app">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<application
android:name="io.dcloud.application.DCloudApplication"
android:allowClearUserData="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:largeHeap="true"
>
<provider
android:name="io.dcloud.common.util.DCloud_FileProvider"
android:authorities="com.genertech.jzapp.dc.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/dcloud_file_provider" />
</provider>
<activity
android:name="io.dcloud.PandoraEntryActivity"
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" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:configChanges="orientation|screenSize"
android:windowSoftInputMode="stateAlwaysHidden"
android:theme="@style/DeviceDefault.Light"
android:name="com.dmcbig.mediapicker.PickerActivity" />
<activity android:configChanges="orientation|screenSize"
android:windowSoftInputMode="stateAlwaysHidden"
android:theme="@style/DeviceDefault.Light"
android:name="com.dmcbig.mediapicker.PreviewActivity"/>
</application>
</manifest>
使用到的權限:
- 訪問網絡
- 訪問相機和相冊
- 存儲權限
- 應用內安裝權限
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.gech.app"
minSdkVersion 25
targetSdkVersion 29
versionCode 108
versionName "1.0.8"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
lintOptions {
disable 'GoogleAppIndexingWarning'
checkReleaseBuilds false
abortOnError true
}
}
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(include: ['*.aar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// implementation 'androidx.multidex:multidex:2.0.0'
// implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'com.nineoldandroids:library:2.4.0'
implementation 'com.github.bumptech.glide:glide:4.5.0'
}
引入第三方庫、APP配置相關、文件依賴。
遇到的問題及解決方法
遇到的問題大多都是權限配置的問題,剛開始不太熟悉用到一些功能也想不起去配置權限。
- 1、訪問相冊時沒有返回按鈕及導航欄相關,默認HB選擇圖片的樣式很不友好。
解決:
libs引入如下文件

application中設置如下:
<activity android:configChanges="orientation|screenSize"
android:windowSoftInputMode="stateAlwaysHidden"
android:theme="@style/DeviceDefault.Light"
android:name="com.dmcbig.mediapicker.PickerActivity" />
<activity android:configChanges="orientation|screenSize"
android:windowSoftInputMode="stateAlwaysHidden"
android:theme="@style/DeviceDefault.Light"
android:name="com.dmcbig.mediapicker.PreviewActivity"/>
2、選擇圖片點擊相機無法彈出,沒有提示獲取訪問相機的權限請求。
解決:
application引入權限即可
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus" />
3、APP版本升級時,下載新版本無法安裝,權限問題。
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
同時application添加provider
<provider
android:name="io.dcloud.common.util.DCloud_FileProvider"
android:authorities="com.gech.jzapp.dc.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/dcloud_file_provider" />
</provider>
4、無法js文件加密
在線打包時可以選擇js原生混合實現(xiàn)主要的js文件加密,由于官方加密算法的保密限制離線打包不在支持,目前還沒有找到合適的實現(xiàn)方案。
總結
類似本地打包操作這種嚴重依賴平臺的操作要多閱讀官方文檔,但是由于版本區(qū)別、文檔更新不及時錯誤很難避免,實際遇到問題要時刻保持關注,多方搜集。
使用系統(tǒng)功能時如果不起作用,考慮是否需要申請用戶權限
Android Studio打包時注意gradle版本問題,由于網絡問題可能會出現(xiàn)編譯失敗等問題,直接下載到本地使用更加方便。HB的版本和離線打包的sdk的打包盡量保持一致,如果不一致可能會出現(xiàn)莫名其妙的問題。
參考
我是Light413,一名iOS開發(fā)者,喜歡前端及后臺開發(fā)。我在簡書,簡書就是我的陣營,我喜歡在這里學習,在這里分享。以后文章會不斷的更新,介紹關于iOS開發(fā)學習總結 , 介紹基于H5開發(fā)中感悟記錄總結。感覺有用就點贊哈,喜歡就大膽的關注。