安卓:Uniapp转app开发
本随笔会结合官方文档的内容进行个人的补充来完成
Uniapp官方文档链接:App离线打包SDK (dcloud.net.cn)
一、准备工作
1、下载SDK
选择合适的版本下载
2、导出uniapp项目
打包后的资源可以在Hbuilderx的控制台点击打开
也可以在项目目录下寻找 /unpackage/recourse/
文件夹名是你项目的AppId
3、获取Appkey
官方文档教程很详细,不多赘述,但是要注意appid,包名,appkey三者是正确对应的,appid在你创建了uniapp后就会生成,只要你用的同一个账号
二、开始工作
1、创建一个项目
方式一:新建空项目
打开Android studio新建一个空白项目
选择 Empty Activity 点击 Next。
Minimum API Level 建议选21及以上
Package name作为应用标志,涉及申请第三方平台账号,一般情况下不建议修改,包名一般设置反向域名,如:io.dcloud.HBuilder
点击 Finish 完成创建。
注意:App离线SDK不支持Kotlin
方法二:导入官方提供的
2.7.0之后提供HBuilder-Integrate-AS工程,可以直接导入HBuilder-Integrate-AS工程,直接运行simpleDemo项目即可。
2、配置文件
将准备工作下载的SDK里的libs下的这四个文件复制到app下的libs
新建一个文件夹assets,目录结构如下
从SDK里复制以下对应的三个文件dcloud_control,dcloud_error,dcloud_properties
将导出的Uniapp项目放到apps文件夹里
然后修改dcloud_control文件,appid和apps下项目目录下的mainfest.json里的对应
生成自定义基座,需要在根节点下添加debug="true"和syncDebug="true",如下图:
自定义基座支持热更新需要将debug-server-release.aar打入apk中,生成正式包时可以删除
三、配置工作
AndroidX 配置
需要在项目根目录下的gradle.properties文件中添加如下内容
1 2 | android.useAndroidX= true android.enableJetifier= true |
build.grade(app),需要修改添加部分已注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | plugins { id 'com.android.application' } android { //以下为我适用版本 compileSdk 29 defaultConfig { applicationId "com.example.test" minSdk 21 targetSdk 28 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } //配置appkey, 这里一定要放在buildTypes前面 signingConfigs { config { keyAlias 'test_alias' keyPassword 'yourpassword' storeFile file( '*test.keystore' ) storePassword 'yourpassword' v1SigningEnabled true v2SigningEnabled true } } buildTypes { //配置appkey debug { signingConfig signingConfigs.config } release { minifyEnabled false proguardFiles getDefaultProguardFile( 'proguard-android-optimize.txt' ), 'proguard-rules.pro' //配置appkey signingConfig signingConfigs.config } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } //额外配置 aaptOptions { additionalParameters '--auto-add-overlay' ignoreAssetsPattern "!.svn:!.git:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~" } } dependencies { implementation 'androidx.appcompat:appcompat:1.3.0' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' //官方文档要求添加的依赖 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' implementation 'androidx.recyclerview:recyclerview:1.0.0' implementation 'com.facebook.fresco:fresco:2.5.0' implementation "com.facebook.fresco:animated-gif:2.5.0" implementation 'com.github.bumptech.glide:glide:4.9.0' implementation 'com.alibaba:fastjson:1.1.46.android' } |
AndroidMainFest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | <?xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package = "com.example.test" > //修改为你自己的 <application android:allowBackup= "true" android:icon= "@drawable/icon" android:label= "@string/app_name" android:roundIcon= "@mipmap/ic_launcher_round" android:supportsRtl= "true" android:theme= "@style/Theme.yourapp" //修改为你的 > <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" android:exported= "true" > <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|smallestScreenSize|screenLayout|screenSize" android:hardwareAccelerated= "true" android:permission= "com.miui.securitycenter.permission.AppPermissionsEditor" android:screenOrientation= "user" android:theme= "@style/DCloudTheme" android:windowSoftInputMode= "adjustResize" android:exported= "true" > <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节点必须添加--> <provider android:name= "io.dcloud.common.util.DCloud_FileProvider" android:authorities= "yourpackage.dc.fileprovider" //示例:com.example.test android:exported= "false" android:grantUriPermissions= "true" > <meta-data android:name= "android.support.FILE_PROVIDER_PATHS" android:resource= "@xml/dcloud_file_provider" /> </provider> <meta-data android:name= "dcloud_appkey" android:value= "yourappkey" /> //前面根据你创建的对应的uniapp所生成的appkey </application> </manifest> |
如果集成离线SDK时需要重写application,必须继承自DCloudApplication,否则会导致SDK中业务逻辑无法正常运行。
将对应的application配置到Androidmanifest.xml中(如下),并添加tools:replace="android:name"以防出现冲突。
1 2 3 4 5 6 | <application android:name= "io.dcloud.test.TestApplication" android:icon= "@drawable/icon" android:label= "@string/app_name" tools:replace= "android:name" > </application> |
个性化配置
icon.png为应用的图标。
push.png为推送消息的图标。
splash.png为应用启动页的图标。
string.xml里可以修改你的应用名字(app_name)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!