Flutter 如何发布安卓应用?

设置应用的名称,包名、应用图标和启动

安卓的应用资源配置在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

  1. applicaitonId:应用惟一AppId,如com.lios.helloworld
  2. versionCode:应用程序版本号
  3. versionName:版本号字符串
  4. minSdkVersion:指定最低的API级别
  5. targetSdkVersion:指定应用程序设计运行的API级别

以下所示: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
复制代码
相关文章
相关标签/搜索