软件编程
位置:首页>> 软件编程>> Android编程>> 如何使用Flutter发布安卓应用

如何使用Flutter发布安卓应用

作者:岛上码农  发布时间:2022-09-07 08:46:07 

标签:Flutter,发布应用,安卓
目录
  • 设置应用的名称,包名、应用图标和启动

  • 替换应用图标

  • 替换启动页

  • 设置访问权限

  • 配置版本发布参数

  • 生成应用签名

  • 修改配置文件

  • 打包

  • 注意事项

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

安卓的应用资源配置在main/AndroidManifest.xml中设置,文件内容如下:


<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生成的文件建议是大部分内容可以保留不动,但是可以根据需要进行修改。
具体可能要修改的内容如下:

属性名用途说明
package应用包名安卓应用的唯一标识符,一般为com.xxxx.xxx格式
android:label应用显示名称默认为工程名,需要根据实际情况修改
android:icon应用图标替换指定的图标文件即可
meta-data
              android:name
资源名称不可更改,用于Flutter生成安卓插件
meta-data
             value
资源值不可更改,用于Flutter生成安卓插件

替换应用图标

安卓提供了如下尺寸的图标配置文件,在Flutter项目下的android/app/src/main/res对应尺寸目录下可以应用图标文件。

尺寸别名图标大小屏幕尺寸
mipmap-mdpi48x48320×480
mipmap-hdpi72x72480×800,480×854
mipmap-xhdpi96x961280*720,720p
mipmap-xxhdpi144x1441920*1080,1080p
mipmap-xxxhdpi192x1923840×2160,4k

替换启动页

应用启动页图片在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>

注释掉的部分可以用来设置启动页图片,需要注意部分机型的尺寸未必和启动页图片一致,因此可以设置启动页的背景色与启动页图片边缘一致。


<?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文件检查配置是否正确:

  1. applicaitonId:应用唯一AppId,如com.lios.helloworld

  2. versionCode:应用程序版本号

  3. versionName:版本号字符串

  4. minSdkVersion:指定最低的API级别

  5. targetSdkVersion:指定应用程序设计运行的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中设置或在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中引入即可。


#其中~/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

来源:https://juejin.cn/post/6960633326488584228

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com