[Flutter] iOS・Androidアプリをリリースビルドする方法【個人メモ】

Flutterで作成したソースコードをiOS・AndroidアプリとしてAppStore・GooglePlayStoreで公開した時の備忘録メモです。

リリース準備

Flutterのリリース準備方法は公式に記載されているので以下を参照します。

Preparing an iOS app for release – Flutter

Preparing an Android app for release – Flutter

まずはICON

公式に書いてますがflutter_launcher_iconsを利用します。

pubspec.yaml:

dev_dependencies:
  flutter_launcher_icons: "^0.8.0"

flutter_icons:
  android: "launcher_icon"
  ios: true
  image_path: "assets/img/icon.png"

AndroidとiOSでファイルを分ける場合は以下。

flutter_icons:
  android: "launcher_icon" 
  ios: true
  image_path: "assets/img/icon_iOS.png"
  image_path_android: "assets/img/icon_Google.png"
  image_path_ios: "assets/img/icon_iOS.png"

インストール

flutter pub get

アイコン画像を用意

画像を指定のフォルダに保存します。上記の場合は/assets/img/icon.png

512×512のpngファイルでOKでした。

アイコン画像の作成

以下のコマンドでアイコン用の画像ファイルが自動で作成されます。

flutter pub run flutter_launcher_icons:main

iOSのリリースファイルを作成

これまで通り、Xcodeで設定する必要があります。公式の手順に沿って作成します。

公式: Preparing an iOS app for release – Flutter

まずflutterで以下のコマンドを叩いて準備します。

$ flutter clean //必須ではないです
$ flutter build ios

Xcodeでビルド

Xcodeでビルドします。

Xcode Runner.xcworkspaceで、アプリのiosフォルダーを開きます。

  1. [Product > Scheme > Runner.を選択します。
  2. Product > Destination > Generic iOS Deviceを選択します。
  3. 選択してランナーを選択し、Xcodeのプロジェクトナビゲータで ランナーの設定でターゲットをサイドバーを表示します。
  4. アイデンティティのセクションでは、更新バージョンをパブリッシュしたいユーザー向けのバージョン番号に。
  5. Identity sectionで、ビルドIDを、App Store Connectでこのビルドを追跡するために使用される一意のビルド番号に更新します。各アップロードには、一意のビルド番号が必要です。
  6. 最後に、ビルドアーカイブを作成し、App Store Connectにアップロードします。
  7. Product > Archiveを選択して、ビルドアーカイブを作成します。
  8. Xcode Organizerウィンドウのサイドバーで、iOSアプリを選択し、作成したビルドアーカイブを選択します。
  9. [ Validate ]ボタンをクリックします。問題が報告された場合は、それらに対処し、別のビルドを作成します。アーカイブをアップロードするまで、同じビルドIDを再利用できます。
  10. アーカイブが正常に検証されたら、[ Upload to App Store… ]をクリックします 。
  11. App Store Connectのアプリの詳細ページの[Activities]タブでビルドのステータスを確認できます。

(参考)遭遇したエラー

validateで以下のエラー

Missing App Icon. An app icon measuring 1024 by 1024 pixels in PNG format must be included in the Asset Catalog of apps built for iOS, iPadOS, or watchOS. Without this icon, apps cannot be submitted for review. For details, see https://developer.apple.com/ios/human-interface-guidelines/icons-and-images/app-icon/.

1024×1024のiconが設定されていなかったのでxcodeのAppIconでdrag&dropで設定して解決

エラー DTDeviceKit: deviceType from 1a145968c2ae44e5cb4d2f3bb730526a13d27438 was NULL

xcodeで選択しているdeviceをgeneralにして閉じたらいけました。

VSCodeでデバッグビルドしようとしたらエラー

Automatically signing iOS for device deployment using specified development team in Xcode project: 
Xcode build done. 166.6s
Could not find the built application bundle at build/ios/iphoneos/Runner.app.
Error launching application on iPhone(FS9).
原因と解決策

display name を変えたことが原因 Runner に戻せば良い。

参考: https://stackoverflow.com/questions/57980900/flutter-could-not-find-the-built-application-bundle-at-build-ios-iphonesimulato

リリースビルドで起動時に落ちる問題

証明書をauto にしたら起動しました。

証明書関連に問題があった様子。。

Androidでリリースビルド

keyを公式の手順に沿って作成します。/Users/<user name>/key.jksが作成されます。

$ keytool -genkey -v -keystore ~/key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key

Macでkeytoolが実行できない場合:

/Applications/Android\ Studio.app/Contents/jre/jdk/Contents/Home/bin/keytool -genkey -v -keystore ~/key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key

ファイルを作成 /android/key.properties

このファイルはgit管理しないのを推奨

storePassword=<password from previous step>
keyPassword=<password from previous step>
keyAlias=key
storeFile=<location of the key store file, such as /Users/<user name>/key.jks>

keyを読み込む

android/app/build.gradle:

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdkVersion 29

// 中略    

signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            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 //debug -> releaseにする
        }
    }

ビルド番号などを更新

/android/app/build.gradle

applicationId "com.example.app"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true

以下のように/android/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.0'
}
// 中略

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.aquarept.amazon_barcode"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

  • コマンドでリリース用のバンドルファイルを作成
cd <app dir>
flutter build appbundle

リリースバンドルが以下に作成されます。

<app dir>/build/app/outputs/bundle/release/app.aab

build.gradle の minifyEnabled は 2019/12/16時点では falseにしないとビルドに失敗します。

コメントを残す

メールアドレスが公開されることはありません。