Flutterで作成したAndroidアプリをGoogle Playに公開する方法についてまとめました。
keystoreファイル(署名するときの設定ファイル)の作成
keystoreファイル(署名するときの設定ファイル)を作成し、「android\app\key.jks」に配置します。
keytool -genkey -v -keystore key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key
android/keystore.properties を新規作成し、以下のように中身を記述します。
storePassword=ストアのパスワード keyPassword=キーのパスワード keyAlias=エイリアス名 storeFile=keystoreファイルのパス
build.gradleファイルの編集
android/app/build.gradle を以下のように編集します。
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
// 追加①
def keystorePropertiesFile = rootProject.file("keystore.properties")
android {
compileSdkVersion 30
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "jp.co.orust.kinkyu"
minSdkVersion 16
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true // 追加②
}
// ここから追加③
signingConfigs {
release {
if (keystorePropertiesFile.exists()) {
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}
}
}
// ここまで追加③
buildTypes {
release {
signingConfig signingConfigs.release // 変更①
}
}
}
flutter {
source '../..'
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
- password
- keystoreファイル作成時に設定したパスワード
- alias-name
- エイリアス名
- key.js
- 作成したkeystoreファイルのパス
AndroidManifest.xmlの編集
AndroidManifest.xmlにもApp Bundle のパッケージ名(xxx.yyy.zzzなど)を挿入します。
■android\app\src\main\AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.yyy.zzz">
APKファイルのリリースビルド
以下のコマンドをAndroid Studioのターミナルで実行すると、「build\app\outputs\apk\release\app-release.apk 」にapkファイルが作成されます。
flutter build apk --release
Android App BundleでGoogle Playに公開
Android App Bundleとは、ユーザー毎に最適なapkを作成(ビルド)し配信する仕組みです。
Android App Bundle の形式のファイルの拡張子は .aabで、32bit、64bitのAPKなどがすべて入っており、Google Playはそこから必要なリソースを取り出してapkをユーザーに再構築して配信します。
そのため、ユーザー側には必要なリソースだけが含まれるapkがインストールされるので、アプリサイズの削減につながる利点があります。
ただし、 Android App Bundle を利用するためにはGoogle Play側でapkの再構築と署名を行うため、 Google Play App Signing を使う必要があります。

Flutterでは、バージョン1.7からAndroid App Bundleに対応しています。
flutter build appbundle --release
【補足】バージョン更新
アプリの更新をするときは、アプリのバージョン番号とバージョンコードの値も上げてやる必要があります。
変更は、pubspec.yamlで行います。
name: アプリ名 description: A new Flutter application. publish_to: 'none' # Remove this line if you wish to publish to pub.dev version: バージョン番号+バージョンコード

コメント