安卓的應用資源配置在main/AndroidManifest.xml中設置,文件內容以下:javascript
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.gesture_demo">
<!-- io.flutter.app.FlutterApplication is an android.app.Application that calls FlutterMain.startInitialization(this); in its onCreate method. In most cases you can leave this as-is, but you if you want to provide additional functionality it is fine to subclass or reimplement FlutterApplication and put your custom class here. -->
<application android:name="io.flutter.app.FlutterApplication" android:label="gesture_demo" android:icon="@mipmap/ic_launcher">
<activity android:name=".MainActivity" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below. This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data android:name="flutterEmbedding" android:value="2" />
</application>
</manifest>
複製代碼
Flutter生成的文件建議是大部份內容能夠保留不動,可是能夠根據須要進行修改。
具體可能要修改的內容以下:html
屬性名 | 用途 | 說明 |
---|---|---|
package | 應用包名 | 安卓應用的惟一標識符,通常爲com.xxxx.xxx格式 |
android:label | 應用顯示名稱 | 默認爲工程名,須要根據實際狀況修改 |
android:icon | 應用圖標 | 替換指定的圖標文件便可 |
meta-data android:name |
資源名稱 | 不可更改,用於Flutter生成安卓插件 |
meta-data value |
資源值 | 不可更改,用於Flutter生成安卓插件 |
安卓提供了以下尺寸的圖標配置文件,在Flutter項目下的android/app/src/main/res對應尺寸目錄下能夠應用圖標文件。java
尺寸別名 | 圖標大小 | 屏幕尺寸 |
---|---|---|
mipmap-mdpi | 48x48 | 320×480 |
mipmap-hdpi | 72x72 | 480×800,480×854 |
mipmap-xhdpi | 96x96 | 1280*720,720p |
mipmap-xxhdpi | 144x144 | 1920*1080,1080p |
mipmap-xxxhdpi | 192x192 | 3840×2160,4k |
應用啓動頁圖片在Flutter項目下的android/app/src/main/drawable下的launch_background.xml配置文件中,默認是一個白色底,xml問卷以下所示:android
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" />
<!-- You can insert your own image assets here -->
<!-- <item> <bitmap android:gravity="center" android:src="@mipmap/launch_image" /> </item> -->
</layer-list>
複製代碼
註釋掉的部分能夠用來設置啓動頁圖片,須要注意部分機型的尺寸未必和啓動頁圖片一致,所以能夠設置啓動頁的背景色與啓動頁圖片邊緣一致。ios
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 背景色 -->
<item android:drawable="@android:color/white" />
<!-- 啓動頁圖片,也能夠添加其餘元素 -->
<item>
<bitmap android:gravity="center" android:src="@mipmap/launch_image" />
</item>
</layer-list>
複製代碼
在android/app/src下的AndroidManifest.xml(注意不是src/profile文件夾下的AndroidManifest.xml文件)文件中設置應用權限,如訪問網絡,相冊,攝像頭等。開發環境是在android/src/debug的AndroidManifest.xml中設置。下面是一個示例的文件:緩存
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.animation_demo">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application android:name="io.flutter.app.FlutterApplication" android:label="動畫演示" android:icon="@mipmap/ic_launcher">
<activity android:name=".MainActivity" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below. This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data android:name="flutterEmbedding" android:value="2" />
</application>
</manifest>
複製代碼
在android/app/build.gradle文件檢查配置是否正確:bash
以下所示:markdown
android {
compileSdkVersion 28
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.animation_demo"
minSdkVersion 16
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
複製代碼
這裏面能夠看到versionCode和versionName是從flutterVersionCode和flutterVersionName中引入的,其中這兩個變量在build.gradle上面有定義。先從local.properties中讀取,若沒有再在該文件中定義,所以能夠在localProperties中設置或在build.gradle中設置(優先取local.properties中的值)。網絡
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
複製代碼
建立keystore,若是以前建立過了,在key.properties中引入便可。app
#其中~/key.jks是將keystore文件key.jks存儲在~/目錄下
keytool -genkey -v -keystore ~/key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key
複製代碼
按提示輸入密碼和組織信息便可。
輸入密鑰庫口令:
再次輸入新口令:
您的名字與姓氏是什麼?
[Unknown]: lag
您的組織單位名稱是什麼?
[Unknown]: island-coder
您的組織名稱是什麼?
[Unknown]: RD
您所在的城市或區域名稱是什麼?
[Unknown]: Coder
您所在的省/市/自治區名稱是什麼?
[Unknown]: Island
該單位的雙字母國家/地區代碼是什麼?
[Unknown]: CN
CN=lag, OU=island-coder, O=RD, L=Coder, ST=Island, C=CN是否正確?
[否]: Y
正在爲如下對象生成 2,048 位RSA密鑰對和自簽名證書 (SHA256withRSA) (有效期爲 10,000 天):
CN=lag, OU=island-coder, O=RD, L=Coder, ST=Island, C=CN
[正在存儲/Users/lag/key.jks]
複製代碼
在android目錄下建立一個key.properties文件,用於引用密鑰庫信息:
storePassword={密鑰庫密碼} #
keyPassword={證書密碼}
keyAlias=key #對應命令行的-alias後的別名
storeFile=/Users/lag/key.jks #對應命令生成的key.jks的據對路徑
複製代碼
在build.gradle文件中,在android下增長如下內容:
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile = file(keystoreProperties['storeFile'])
storePassword = keystoreProperties['storePassword']
}
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.release
}
}
複製代碼
在項目目錄下,運行下面的命令:
flutter build apk
複製代碼
默認按release打包,生成的apk在build.app/outputs/apk/app-release.apk下。
修改AndroidManifest.xml文件後,flutter打包可能存在緩存,此時運行下面的命令,清除掉緩存再次打包便可。
flutter clean
複製代碼