uniapp的離線打包太麻煩,留個記錄方便以后使用。uniapp離線打包步驟:
1. 下載uniapp的 HTML 5+ SDK
下載地址 https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/103 。網頁打開后點如下圖片圈內的鏈接下載SDK
下載解壓之后的目錄如下:注意這個SDK是第2步中新建Android項目需要使用到的
2. 在Android studio中新建Android項目
刪除原生工程中Java目錄下系統默認創建的源代碼
將試圖切換至project,如下圖:
3. 將SDK目錄中如下圖所示的三個SDK 拷貝到新建的項目的libs目錄下,如圖所示
此時需要在下圖所示的文件中添加配置,如圖所示
還需要在上面這個文件中添加入下圖所示的配置
如下標黃的代碼就是需要添加的
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.gzrj.pses_mobile"
minSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true ndk { abiFilters 'x86', 'armeabi-v7a' }
}
buildTypes {
release {
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:*~" }
/*代碼結束*/
}
repositories { flatDir { dirs 'libs' } }
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(include: ['*.aar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
/*uniapp所需庫-----------------------開始*/ implementation 'com.android.support:recyclerview-v7:26.1.0' implementation 'com.alibaba.android:bindingx-core:1.0.3' implementation 'com.alibaba.android:bindingx_weex_plugin:1.0.3' implementation 'com.squareup.okhttp:okhttp:2.3.0' implementation 'com.squareup.okhttp:okhttp-ws:2.3.0' implementation 'com.alibaba:fastjson:1.1.46.android' implementation 'com.facebook.fresco:fresco:0.12.0' /*uniapp所需庫-----------------------結束*/ // 基座需要,必須添加 implementation 'com.github.bumptech.glide:glide:4.9.0'
}
4. 在app => src => main 文件夾下新建 assets文件夾,將下載的SDK中的assets文件夾下的data拷貝到 assets文件夾下
5.在assets目錄下新建apps => [appid] => www文件目錄。此處的appid是uniapp生成的appid(可以在uniapp的 配置中生成)
6.將在HbuilderX中開發的項目進行離線打包,並將打包好的www文件夾下資源拷貝到上一步新建的文件夾
7.將data下的dcloud_control.xml中的appid改為第5步中一樣的id
8.將新建的項目AndroidManifest.xml中的application節點替換成如下內容
<application android:name="io.dcloud.application.DCloudApplication" android:allowClearUserData="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:largeHeap="true" android:roundIcon="@mipmap/ic_launcher_round"> <activity android:name=".SplashActivity"> </activity> <activity android:name="io.dcloud.PandoraEntry" android:configChanges="orientation|keyboardHidden|keyboard|navigation" android:hardwareAccelerated="true" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="user" android:theme="@style/TranslucentTheme" android:windowSoftInputMode="adjustResize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
9.運行到android模擬器可以看到自己在uniapp開發的項目