如何使用Flutter發(fā)布安卓應(yīng)用
設(shè)置應(yīng)用的名稱,包名、應(yīng)用圖標和啟動
安卓的應(yīng)用資源配置在main/AndroidManifest.xml中設(shè)置,文件內(nèi)容如下:
<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生成的文件建議是大部分內(nèi)容可以保留不動,但是可以根據(jù)需要進行修改。
具體可能要修改的內(nèi)容如下:
| 屬性名 | 用途 | 說明 |
|---|---|---|
| package | 應(yīng)用包名 | 安卓應(yīng)用的唯一標識符,一般為com.xxxx.xxx格式 |
| android:label | 應(yīng)用顯示名稱 | 默認為工程名,需要根據(jù)實際情況修改 |
| android:icon | 應(yīng)用圖標 | 替換指定的圖標文件即可 |
| meta-data android:name |
資源名稱 | 不可更改,用于Flutter生成安卓插件 |
| meta-data value |
資源值 | 不可更改,用于Flutter生成安卓插件 |
替換應(yīng)用圖標
安卓提供了如下尺寸的圖標配置文件,在Flutter項目下的android/app/src/main/res對應(yīng)尺寸目錄下可以應(yīng)用圖標文件。
| 尺寸別名 | 圖標大小 | 屏幕尺寸 |
|---|---|---|
| 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 |
替換啟動頁
應(yīng)用啟動頁圖片在Flutter項目下的android/app/src/main/drawable下的launch_background.xml配置文件中,默認是一個白色底,xml問卷如下所示:
<?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>
注釋掉的部分可以用來設(shè)置啟動頁圖片,需要注意部分機型的尺寸未必和啟動頁圖片一致,因此可以設(shè)置啟動頁的背景色與啟動頁圖片邊緣一致。
<?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>
設(shè)置訪問權(quán)限
在android/app/src下的AndroidManifest.xml(注意不是src/profile文件夾下的AndroidManifest.xml文件)文件中設(shè)置應(yīng)用權(quán)限,如訪問網(wǎng)絡(luò),相冊,攝像頭等。開發(fā)環(huán)境是在android/src/debug的AndroidManifest.xml中設(shè)置。下面是一個示例的文件:
<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>
配置版本發(fā)布參數(shù)
在android/app/build.gradle文件檢查配置是否正確:
- applicaitonId:應(yīng)用唯一AppId,如com.lios.helloworld
- versionCode:應(yīng)用程序版本號
- versionName:版本號字符串
- minSdkVersion:指定最低的API級別
- targetSdkVersion:指定應(yīng)用程序設(shè)計運行的API級別
如下所示:
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中設(shè)置或在build.gradle中設(shè)置(優(yōu)先取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'
}
生成應(yīng)用簽名
創(chuàng)建keystore,如果之前創(chuàng)建過了,在key.properties中引入即可。
#其中~/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 您所在的城市或區(qū)域名稱是什么? [Unknown]: Coder 您所在的省/市/自治區(qū)名稱是什么? [Unknown]: Island 該單位的雙字母國家/地區(qū)代碼是什么? [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目錄下創(chuàng)建一個key.properties文件,用于引用密鑰庫信息:
storePassword={密鑰庫密碼} #
keyPassword={證書密碼}
keyAlias=key #對應(yīng)命令行的-alias后的別名
storeFile=/Users/lag/key.jks #對應(yīng)命令生成的key.jks的據(jù)對路徑
修改配置文件
在build.gradle文件中,在android下增加以下內(nèi)容:
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
以上就是如何使用Flutter發(fā)布安卓應(yīng)用的詳細內(nèi)容,更多關(guān)于Flutter發(fā)布安卓應(yīng)用的資料請關(guān)注腳本之家其它相關(guān)文章!
- VS Code開發(fā)React-Native及Flutter 開啟無線局域網(wǎng)安卓真機調(diào)試問題
- Android原生項目集成Flutter解決方案
- Android如何在原生App中嵌入Flutter
- 詳解Flutter 調(diào)用 Android Native 的方法
- Flutter 和 Android 互相傳遞數(shù)據(jù)的實現(xiàn)
- 解決flutter 錯誤: 程序包androidx.lifecycle不存在問題
- Android studio 切換flutterSDK之后報錯及解決辦法(推薦)
- Flutter下Android Studio配置gradle的方法
- Android Studio 下 Flutter 開發(fā)環(huán)境搭建過程
- Android Studio下Flutter環(huán)境搭建圖文教程
- Flutter調(diào)用Android和iOS原生代碼的方法示例
相關(guān)文章
Android應(yīng)用創(chuàng)建桌面快捷方式代碼
這篇文章主要為大家詳細介紹了Android應(yīng)用創(chuàng)建桌面快捷方式代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Android中實現(xiàn)自動生成布局View的初始化代碼方法
這篇文章主要介紹了Android中實現(xiàn)自動生成布局View的初始化代碼方法,本文使用解析layout 布局文件的方法實現(xiàn)需求,需要的朋友可以參考下2014-10-10
基于Vert.x和RxJava 2構(gòu)建通用的爬蟲框架的示例
這篇文章主要介紹了基于Vert.x和RxJava 2構(gòu)建通用的爬蟲框架的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
android利用ContentResolver訪問者獲取手機聯(lián)系人信息
這篇文章主要介紹了android利用ContentResolver訪問者獲取手機聯(lián)系人信息,非常具有實用價值,需要的朋友可以參考下。2017-02-02
Android通過HTTP協(xié)議實現(xiàn)上傳文件數(shù)據(jù)
這篇文章主要為大家詳細介紹了Android通過HTTP協(xié)議實現(xiàn)上傳文件數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09

