由於項目需求,使用了hbuilderx 他們家的uniapp框架,這個框架是基於微信小程序和vue衍生出來的,只要你會微信小程序和vue基本上都是很容易上手,關於他們的api,可以自行去官網查看,這里只是為了記錄如何降低打包出來的apk的大小
本篇博客的前提是你的電腦Android的環境配置都已經配置好了,可以運行的情況下進行的,如何證明安裝成功了,你可以使用Android studio新建一個empty Activity之后直接運行看是否可以正常在模擬器上跑起來,接下來進入主題
你會發現,直接使用hbuilderx提供的sdk文件下的Hbuilder-Hello文件夾進行打包,如果你是直接用使用的hbuilderx uniapp的模板,打包出來的apk大約在50M左右,因為hbuilder-hello下幾乎把所有的第三方的包給你放進去了,在libs里面,所以導致你實際開發的時候,包會越來越大,如何減低apk的大小呢
第一步,打開Android studio ,點擊File - New - New Project 來到如下界面
選擇 No Activity
不選擇 Empty Activity 的原因是打包之后你會有兩個包一個是hello Android 一個是你真正的包,所以這里推薦選擇 No Activity
第二步,點擊next
注意:離線打包不支持kotlin 所以要選擇java,其余的應用名稱,包名,可以自己定
然后點擊finish,你會來到如下界面
為了統一官網配置,這里點擊project,你會看到你的目錄結構和Hbuilder-Hello的差不多
第三步開始配置
將你下載的離線打包的sdk中的HBuilder-Hello\app\libs下的
android-gif-drawable-release@1.2.17.aar
lib.5plus.base-release.aar
miit_mdid_1.0.10.aar
uniapp-release.aar
,這四個文件放到你新建的Android項目下的libs下面
注意對比目錄結構
接着,在app-src-main下新建assets文件夾,將HBuilder-Hello\app\src\main\assets文件夾下的data文件夾整個copy到新建的assets文件夾下
注意:不要管data文件夾下有什么不同,因為可能最新的sdk刪除了上圖中的兩個dat文件,你不用管,只用直接復制就好了
第四步,使用hbuilderx本地打包app資源,或者使用vscode直接yarn run build:app-plus進行打包資源,通過vscode打包的需要你自己改兩個名字,一個是將dist下的build改成的應用id,將app-plus改成www,如果是hbuilderx工具生成的app資源是無需做修改的,等到的目錄結構如下
然后在 上面新建的assets下新建apps文件夾,將上面的包放在assets/apps文件夾下
開始配置
要確保這三處的id一致
然后打開app/build.gradle文件
在dependencies下新增
implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
implementation "com.android.support:support-v4:28.0.0"
implementation "com.android.support:appcompat-v7:28.0.0"
implementation 'com.android.support:recyclerview-v7:28.0.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'
在 Android{}添加
aaptOptions {
additionalParameters '--auto-add-overlay'
ignoreAssetsPattern "!.svn:!.git:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"
}
然后修改對應的版本
完整文件是
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "30.0.0"
defaultConfig {
applicationId "com.example.testuniapp"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
implementation "com.android.support:support-v4:28.0.0"
implementation "com.android.support:appcompat-v7:28.0.0"
implementation 'com.android.support:recyclerview-v7:28.0.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 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
修改之后記得 Sync now
然后在app→src→main下配置AndroidManifest.xml文件
修改為
將application節點里的內容替換為
<activity
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" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
</activity>
<activity
android:name="io.dcloud.PandoraEntryActivity" android:launchMode="singleTask" android:configChanges="orientation|keyboardHidden|screenSize|mcc|mnc|fontScale|keyboard"
android:hardwareAccelerated="true" android:permission="com.miui.securitycenter.permission.AppPermissionsEditor"
android:screenOrientation="user" android:theme="@style/DCloudTheme" android:windowSoftInputMode="adjustResize">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="h56131bcf" />
</intent-filter>
</activity>
可以去官網直接復制
完整代碼是
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.testuniapp"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <!-- 添加內容--> <activity 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" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="io.dcloud.PandoraEntryActivity" android:launchMode="singleTask" android:configChanges="orientation|keyboardHidden|screenSize|mcc|mnc|fontScale|keyboard" android:hardwareAccelerated="true" android:permission="com.miui.securitycenter.permission.AppPermissionsEditor" android:screenOrientation="user" android:theme="@style/DCloudTheme" android:windowSoftInputMode="adjustResize"> <intent-filter> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <action android:name="android.intent.action.VIEW" /> <data android:scheme="h56131bcf" /> </intent-filter> </activity> </application> </manifest>
然后點擊右上角的運行按鈕運行,就可以快了的玩耍了
然后點擊 build APK
打包成功之后,你會發現你的包小了很多