uniapp本地打包官方文檔命满,可以根據(jù)官方文檔中的sdk文件夾UniPlugin-Hello-AS
為基礎(chǔ)進(jìn)行改造歼疮,剛開(kāi)始摸索時(shí),也是一點(diǎn)點(diǎn)看文檔赡矢、看demo進(jìn)行搭建的吹散,畢竟一點(diǎn)安卓開(kāi)發(fā)的基礎(chǔ)也沒(méi)有刃唐,又因?yàn)闃I(yè)務(wù)需求必須要開(kāi)發(fā)一個(gè)推送功能module画饥,所以就先把本地測(cè)試環(huán)境搭建好,方便開(kāi)發(fā)module单山。
首先創(chuàng)建一個(gè)新的空項(xiàng)目(android studio默認(rèn)已經(jīng)安裝好,以下稱作“ad”),File=>New=>New Project=>Empty Activity
逐工,
接下來(lái)進(jìn)行一些項(xiàng)目最基礎(chǔ)的配置
然后
Finish
,ad會(huì)自動(dòng)下載配置依賴袒啼,慢慢等待......文件目錄如下蚓再,各個(gè)文件的具體功能,可以自行百度(主要是不專業(yè)娃属,怕說(shuō)錯(cuò)嘍) ↓↓↓↓
│ .gitignore
│ build.gradle
│ gradle.properties
│ gradlew
│ gradlew.bat
│ local.properties
│ settings.gradle
├─app
│ │ .gitignore
│ │ build.gradle
│ │ proguard-rules.pro
│ │
│ ├─libs
│ └─src
│ ├─androidTest
│ │
│ ├─main
│ │ │ AndroidManifest.xml
│ │ │
│ │ ├─java
│ │ │ └─com
│ │ │ └─fq
│ │ │ └─test
│ │ │ MainActivity.java
│ │ │
│ │ └─res
│ │
│ └─test
│
└─gradle
└─wrapper
gradle-wrapper.jar
gradle-wrapper.properties
以下是自己的個(gè)人理解:
settings.gradle文件
settings.gradle
文件是用于寫(xiě)項(xiàng)目之間的依懶關(guān)系,里面有項(xiàng)目的總名稱铐料,包含了哪些模塊等,
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
rootProject.name = "MyTest"
include ':app'
我不曉得里面的dependencyResolutionManagement
有什么用篓跛,所以根據(jù)uniapp給的demo愧沟,把這個(gè)刪除了盖奈,只留下了:
rootProject.name = "MyTest"
include ':app' // 用于增加子模塊,如果這個(gè)時(shí)候想加一個(gè)新的module 可以再加一個(gè) include ":<module名>"
gradle.properties
文件是配置gradle的钢坦,uniapp 本地開(kāi)發(fā)需要這三個(gè)配置
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
android.useAndroidX=true
android.enableJetifier=true
build.gradle文件
build.gradle
文件是配置gradle依賴下載源究孕,gradle構(gòu)建工具的版本,我這里改成了阿里的源
buildscript {
repositories {
maven {
url 'https://maven.aliyun.com/repository/google'
}
maven {
url 'https://maven.aliyun.com/repository/public'
}
maven {
url 'https://maven.aliyun.com/repository/jcenter'
}
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.2"
}
}
allprojects {
repositories {
maven {
url 'https://maven.aliyun.com/repository/google'
}
maven {
url 'https://maven.aliyun.com/repository/public'
}
maven {
url 'https://maven.aliyun.com/repository/jcenter'
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
最外圍的改動(dòng)就這些了爹凹,其他的操作都在app
這個(gè)主項(xiàng)目文件夾下
注意以下的操作都在app文件夾內(nèi)
刪除掉了src下所有文件夾
厨诸,新建了libs
與src
文件夾,
lib目錄內(nèi)容
其中libs
下是需要引入的依賴
以下為運(yùn)行必備的依賴
android-gif-drawable-release@1.2.23.aar
lib.5plus.base-release.aar
oaid_sdk_1.0.25.aar
uniapp-v8-release.aar
其他的依賴可以按照自己的實(shí)際需要放入禾酱,具體的增加哪些可以根據(jù)下載的sdk文件夾Feature-Android.xls查看build.gradle內(nèi)的內(nèi)容
// 聲明是Android程序微酬,
// com.android.application 表示這是一個(gè)應(yīng)用程序模塊,可直接運(yùn)行
// com.android.library 標(biāo)識(shí)這是一個(gè)庫(kù)模塊忙上,是依附別的應(yīng)用程序運(yùn)行
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "app包名" //注意確定自己的包名,不要有"test"之類的字眼,不然提交到應(yīng)用商城會(huì)過(guò)不去
minSdkVersion 21
targetSdkVersion 26
versionCode 79 // 對(duì)應(yīng)hbuilder的 -> 應(yīng)用版本號(hào)
versionName "1.1.9.0" // 對(duì)應(yīng)hbuilder的 -> 應(yīng)用版本名稱
// multiDexEnabled true
ndk {
abiFilters 'x86', 'armeabi-v7a', 'arm64-v8a', "x86_64" // 允許在哪些設(shè)備中運(yùn)行你的app
}
archivesBaseName = "fqApp-$versionName" // 構(gòu)建的apk名字
}
signingConfigs {
config { // 配置證書(shū)簽名,如何生成證書(shū) https://ask.dcloud.net.cn/article/35777
storeFile file('./lalala.keystore') // file中是你app證書(shū)的位置
storePassword '123456'
keyAlias 'lalala'
keyPassword '123456'
v1SigningEnabled true
v2SigningEnabled true
}
}
/**
* 代碼混淆
*/
buildTypes {
release {
signingConfig signingConfigs.config // 簽名配置
// zipAlignEnabled true // 是否支持zip
// shrinkResources true // 移除無(wú)用的resource文件
debuggable true
minifyEnabled true // 代碼混淆
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
// zipAlignEnabled true
debuggable true
signingConfig signingConfigs.config
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
//使用uniapp時(shí),需復(fù)制下面代碼
/*代碼開(kāi)始*/
aaptOptions {
additionalParameters '--auto-add-overlay'
ignoreAssetsPattern "!.svn:!.git:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"
}
/*代碼結(jié)束*/
/**
* 編譯配置
*/
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
lintOptions {
abortOnError false
}
}
dependencies {
implementation fileTree(include: ['*.jar','*.aar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
/*uniapp所需庫(kù)-----------------------開(kāi)始*/
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所需庫(kù)-----------------------結(jié)束*/
// 基座需要涕癣,必須添加
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'com.alibaba:fastjson:1.1.46.android'
// 添加uni-app插件
// implementation project(":fqPush")
}
src目錄
目錄截圖须蜗,以下是其中各個(gè)文件內(nèi)容循未,可以對(duì)照此圖,查看各個(gè)文件的位置
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="你的包名">
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<!-- app權(quán)限 根據(jù)實(shí)際情況增減 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_ADDED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_CHANGED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_INSTALL" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REPLACED" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<application
android:name="io.dcloud.application.DCloudApplication"
android:allowClearUserData="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:supportsRtl="true"
>
<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:configChanges="orientation|keyboardHidden|screenSize|mcc|mnc|fontScale|keyboard|smallestScreenSize|screenLayout|screenSize"
android:hardwareAccelerated="true"
android:launchMode="singleTask"
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>
<!--provider節(jié)點(diǎn)必須添加-->
<provider
android:name="io.dcloud.common.util.DCloud_FileProvider"
android:authorities="你的包名.dc.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/dcloud_file_provider" />
</provider>
//這里我用了高德地圖 所以需要加這個(gè) 不用可以不加,具體加哪些還是看上面提到的excel文件
<!-- 高德 開(kāi)始 -->
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="地圖的key"
>
</meta-data>
<service
android:name="com.amap.api.location.APSService"
>
</service>
<!-- 高德 結(jié)束 -->
// 離線打包的key俏站, 沒(méi)它可啟動(dòng)不了哦~
<meta-data
android:name="dcloud_appkey"
android:value="你的離線打包key" />
</application>
</manifest>
離線打包的key 從dcloud開(kāi)發(fā)者中心獲取
string.xml
<resources>
<string name="app_name">你的應(yīng)用名稱</string>
</resources>
proguard-rules.pro
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
#-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-dontoptimize
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class * extends io.dcloud.common.DHInterface.IPlugin
-keep public class * extends io.dcloud.common.DHInterface.IFeature
-keep public class * extends io.dcloud.common.DHInterface.IBoot
-keep public class * extends io.dcloud.common.DHInterface.IReflectAble
-keep class io.dcloud.feature.speech.** {*;}
-keep class io.dcloud.net.** {*;}
-keep class io.dcloud.common.constant.** {*;}
-keep class io.dcloud.common.sonic.** {*;}
-keep class io.dcloud.common.DHInterface.** {*;}
-keep class io.dcloud.common.util.** {*;}
-keep class io.dcloud.common.adapter.** {*;}
-keep class io.dcloud.feature.internal.reflect.** {*;}
-keep class io.dcloud.feature.internal.sdk.** {*;}
-keep class io.dcloud.feature.payment.** {*;}
-keep class io.dcloud.sdk.** {*;}
-keep class com.** {*;}
-keep class io.dcloud.nineoldandroids.** {*;}
-keep class vi.com.gdi.** {*;}
-keep class androidx.** {*;}
-dontwarn pl.droidsonroids.gif.**
-keepclasseswithmembers class * extends io.dcloud.js.geolocation.GeoManagerBase {
<methods>;
}
-keep class io.dcloud.share.AbsWebviewClient
-keepclasseswithmembers class io.dcloud.share.AbsWebviewClient {
<methods>;
}
-keep class io.dcloud.share.ShareAuthorizeView
-keepclasseswithmembers class io.dcloud.share.ShareAuthorizeView {
<methods>;
}
-keep class io.dcloud.share.IFShareApi
-keep public class * extends io.dcloud.share.IFShareApi
-keepclasseswithmembers class io.dcloud.share.IFShareApi {
<methods>;
}
-keepattributes Exceptions,InnerClasses,Signature,Deprecated, SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
-keep class io.dcloud.appstream.StreamAppManager
-keepclasseswithmembers class io.dcloud.appstream.StreamAppManager {
public protected <methods>;
}
-keep class io.dcloud.common.DHInterface.IReflectAble
-keep public class * extends io.dcloud.common.DHInterface.IReflectAble{
public protected <methods>;
public protected *;
}
-keep class **.R
-keep class **.R$* {
public static <fields>;
}
-keep public class * extends io.dcloud.common.DHInterface.IJsInterface{
public protected <methods>;
public protected *;
}
-keepclasseswithmembers class io.dcloud.EntryProxy {
<methods>;
}
-keep class * implements android.os.IInterface {
<methods>;
}
-keepclasseswithmembers class *{
public static java.lang.String getJsContent();
}
-keepclasseswithmembers class io.dcloud.appstream.StreamAppScriptEntry {
<methods>;
}
-keepclasseswithmembers class *{
public static void onReceiver1(android.content.Intent, android.content.Context);
}
-keepclasseswithmembers class *{
public static io.dcloud.share.AbsWebviewClient getWebviewClient(io.dcloud.share.ShareAuthorizeView);
}
-keepclasseswithmembers class *{
public java.lang.String exec(java.lang.String,java.lang.String,java.lang.String[]);
}
-keepattributes Exceptions,InnerClasses,Signature,Deprecated, SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keep public class * extends android.app.Application{
public static <methods>;
public *;
}
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
public static <methods>;
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-keepattributes Signature
-keep class io.dcloud.encryption.K {*;}
-dontwarn com.igexin.**
-keep class org.json.** { *; }
-dontwarn com.amap.**
-dontwarn org.apache.commons.**
-dontwarn com.sina.weibo.sdk.**
-keep class tv.danmaku.ijk.media.player.** {*;}
-keep class uni.** {*;}
-keep class pl.** {*;}
-keep class io.** {*;}
-keep class org.mozilla.**{*;}
-keep class androidtranscoder.**{*;}
-keep class XI.**{*;}
drawable-xxhdpi
該文件夾下的是應(yīng)用的基礎(chǔ)圖片,其實(shí)還有各種分辨率的万搔,但是沒(méi)有的話胖缤,安卓會(huì)使用當(dāng)前存在的進(jìn)行放大縮小,因?yàn)楫?dāng)時(shí)的目的僅僅是為了搭建一個(gè)本地環(huán)境哆料,所以只設(shè)置了drawable-xxhdpi文件夾。
icon.png // 應(yīng)用顯示在手機(jī)桌面上的圖標(biāo)
push.png // 應(yīng)用推送時(shí)的圖標(biāo)
splash.9.png // 應(yīng)用剛剛打開(kāi)時(shí)的圖片
apps
apps下是使用hbuilderx生成的文件夾
假如生成的是
__UNI__E34234
,直接把__UNI__E34234文件夾放到apps下
dcloud_uniplugins.json
該文件夾是添加module的蹋半,目前文件內(nèi)不需要加內(nèi)容
data
dcloud_control.xml
dcloud_error.html
dcloud_properties.xml
以上三個(gè)直接從UniPlugin-Hello-AS\app\src\main\assets\data文件夾內(nèi)復(fù)制粘貼
其中dcloud_control.xml
內(nèi)容(__UNI__E34234是隨便寫(xiě)的巡莹,它應(yīng)該跟你的uniapp應(yīng)用標(biāo)識(shí)一致)
<hbuilder>
<apps>
<app appid="__UNI__E34234" appver=""/>
</apps>
</hbuilder>
進(jìn)行完以上操作后捕儒,基本可以happy了
把手機(jī)連接電腦冰啃,調(diào)試模式,或者使用ad的AVD
打包的話刘莹,點(diǎn)擊后阎毅,下一步,填入證書(shū)就ok啦~
參考
因?yàn)橹鞍俣日伊撕枚嗟阃洌恢澜梃b了哪些文章扇调,所以就不列出來(lái)了
over~