[TOC]
# Android原生工程搭建
準備工作:到 [Android 離線SDK - 正式版](https://nativesupport.dcloud.net.cn/AppDocs/download/android) 下載最新的SDK
## 創建工程
首先創建一個空的Android工程:
File | New | New Project...

選擇Empty Activity

為自己的工程取個名字,并配置包名、工程路徑等,點擊Finish。創建好的工程結構:

修改 `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`模塊


可以看到以下結構:

然后將`simpleDemo`模塊中的`libs`復制到`app`模塊中
## 引入資源
先將我們自己創建的項目中的src刪掉,再將`simpleDemo`中的src復制覆蓋到app模塊中,可以看到以下目錄結構:

接下來分別進行分析
### 啟動圖及APP名稱
位于 `res`下的目錄結構:

在`drawable`下:
- `icon.png` 為應用的圖標。
- `push.png` 為推送消息的圖標。
- `splash.png` 為應用啟動頁的圖標。
在`values/string.xml`中,里面配置了應用的名稱:
```xml
<resources>
<string name="app_name">uniapp-android</string>
</resources>
```
### 應用配置
在`assets`中,apps下存放了所有的應用,以 `應用名/www` 的目錄結構存放。

可以看出,`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` 中進行權限配置等操作。
## 啟動程序
一切配置完畢,直接運行即可


- uniapp項目搭建
- 通過cli創建uniapp項目
- uniapp平臺特性
- uniapp基礎
- 在uniapp中使用字體圖標
- uniapp全局變量的幾種實現方式
- uniapp自定義頁面返回邏輯
- uniapp進階
- 在網頁中打開uniapp應用
- uniapp狀態欄與導航欄
- 在uniapp中優雅地使用WebView
- uniapp Android離線打包
- Android原生工程搭建
- 在uni-app項目中集成Android原生工程
- uniapp熱更新和整包更新
- Android Q啟動白屏的問題
- uniapp原生插件開發與使用
- Android 原生插件使用
- uniapp基礎模塊配置
- uniapp定位及地圖
- uniapp第三方支付、登錄
- 常見問題及解決方案
- Android端常見問題解決方案
- H5端常見問題解決方案
- 微信小程序常見問題解決方案