企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # Android原生工程搭建 准备工作:到 [Android 离线SDK - 正式版](https://nativesupport.dcloud.net.cn/AppDocs/download/android) 下载最新的SDK ## 创建工程 首先创建一个空的Android工程: File | New | New Project... ![](https://kan.xiaoyulive.top/uniapp/001.png) 选择Empty Activity ![](https://kan.xiaoyulive.top/uniapp/002.png) 为自己的工程取个名字,并配置包名、工程路径等,点击Finish。创建好的工程结构: ![](https://kan.xiaoyulive.top/uniapp/003.png) 修改 `app/build.gradle` ```groovy apply plugin: 'com.android.application' android { compileSdkVersion 28 buildToolsVersion '28.0.3' defaultConfig { applicationId "com.lizhiboxue.test" minSdkVersion 23 targetSdkVersion 28 versionCode 100 versionName "1.0.0" multiDexEnabled true ndk { abiFilters 'x86','armeabi-v7a' } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.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: ['*.aar', '*.jar'], exclude: []) implementation "com.android.support:support-v4:28.0.0" implementation "com.android.support:appcompat-v7:28.0.0" /*uniapp所需库-----------------------开始*/ 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" /*uniapp所需库-----------------------结束*/ // 基座需要,必须添加 implementation 'com.github.bumptech.glide:glide:4.9.0' implementation 'com.alibaba:fastjson:1.1.46.android' } ``` 包含以下改动: 1. 将其 targetSdkVersion 配置为28以下(为了适配Android Q,详见[适配Android10 / Android Q(API 29)](https://ask.dcloud.net.cn/article/36199)) 2. 将其 targetSdkVersion 配置为21以上,建议此属性值设为23,``io.dcloud.PandoraEntry` 作为apk入口时 必须设置 `targetSDKVersion>=21` 沉浸式才生效 3. minSdkVersion必须是19以上,uniapp才起作用 4. 添加 `ndk.abiFilters`,以支持指定的架构 5. 添加`aaptOptions`节点 6. 修改`dependencies` ## 引入依赖 首先导入`HBuilder-Integrate-AS`项目中的`simpleDemo`模块 ![](https://kan.xiaoyulive.top/uniapp/004.png) ![](https://kan.xiaoyulive.top/uniapp/005.png) 可以看到以下结构: ![](https://kan.xiaoyulive.top/uniapp/006.png) 然后将`simpleDemo`模块中的`libs`复制到`app`模块中 ## 引入资源 先将我们自己创建的项目中的src删掉,再将`simpleDemo`中的src复制覆盖到app模块中,可以看到以下目录结构: ![](https://kan.xiaoyulive.top/uniapp/007.png) 接下来分别进行分析 ### 启动图及APP名称 位于 `res`下的目录结构: ![](https://kan.xiaoyulive.top/uniapp/008.png) 在`drawable`下: - `icon.png` 为应用的图标。 - `push.png` 为推送消息的图标。 - `splash.png` 为应用启动页的图标。 在`values/string.xml`中,里面配置了应用的名称: ```xml <resources> <string name="app_name">uniapp-android</string> </resources> ``` ### 应用配置 在`assets`中,apps下存放了所有的应用,以 `应用名/www` 的目录结构存放。 ![](https://kan.xiaoyulive.top/uniapp/009.png) 可以看出,`www`目录存放的都是一些编译后的网页资源(其实没有编译也行) 其中 `data` 目录中的 `dcloud_control.xml` 可以指定特定的应用: ```xml <hbuilder> <apps> <app appid="HelloH5" appver=""/> </apps> </hbuilder> ``` ### AndroidManifest.xml `AndroidManifest.xml`为应用程序清单,需要将启动页面指定为`io.dcloud.PandoraEntry` ```xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="io.dcloud.simple"> <application android:allowBackup="true" android:allowClearUserData="true" android:icon="@drawable/icon" android:label="@string/app_name" android:largeHeap="true" 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: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> ``` 之后集成各种资源的时候,还需要在 `AndroidManifest.xml` 中进行权限配置等操作。 ## 启动程序 一切配置完毕,直接运行即可 ![](https://kan.xiaoyulive.top/uniapp/010.png) ![](https://kan.xiaoyulive.top/uniapp/011.png)