Flutter アプリを Google Play にリリースするための App Bundle をコマンドラインでビルドする

flutter の一覧に戻る

結論

アップロード鍵の生成

android/app 配下に、キーファイル名とアリアスを指定して keytool コマンドで鍵を作成します
例としてキーファイル名を key.jks、アリアスをkeyとすると、以下のように作成します

cd android/app # android/app 配下に移動
keytool -genkey -v -keystore key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key 

生成時に指定したパスワードは後で使うので控えておきます

build.gradle に release 用の signingConfigs を追加

android/build.gradle に signingConfigs に release 用の設定を追加し、作成したアップロード鍵のファイル名、アリアス、生成時に指定したパスワードを設定します
例としてファイル名が key.jks、アリアスが key、パスワードが password の場合、以下のように追加します


 
 
 
 
 
 


    signingConfigs {
        release {
            storeFile file("key.jks")
            storePassword "password"
            keyAlias "key"
            keyPassword "password"
        }
    }

そして、使用する signingConfig を debug から relese に変更します






 
 



    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
            signingConfig signingConfigs.release
        }
    }
}

署名付き app bundle のビルド

./gradlew bundleRelease 

署名付きの app bundle が build/app/outputs/bundle/releae/app.aab に生成されます
これを Google Play にリリースします

(参考)gradle の可能なタスクの一覧の確認

./gradlew tasks で、gradle のタスクの一覧を確認できます
コマンドを忘れた時に、探すのに便利です

MacBook-Air:android takeyuki$ ./gradlew tasks
Starting a Gradle Daemon (subsequent builds will be faster)

> Task :tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Android tasks
-------------
androidDependencies - Displays the Android dependencies of the project.
signingReport - Displays the signing info for each variant.
sourceSets - Prints out all the source sets defined in this project.

Build tasks
-----------
assemble - Assembles all variants of all applications and secondary packages.
assembleAndroidTest - Assembles all the Test applications.
assembleDebug - Assembles all Debug builds.
assembleProfile - Assembles all Profile builds.
assembleRelease - Assembles all Release builds.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
bundleDebug - Creates all Debug bundles.
bundleProfile - Creates all Profile bundles.
bundleRelease - Creates all Release bundles.
clean - Deletes the build directory.
cleanBuildCache - Deletes the build cache directory.
compileDebugAndroidTestSources
compileDebugSources
compileDebugUnitTestSources
compileProfileSources
compileProfileUnitTestSources
compileReleaseSources
compileReleaseUnitTestSources
extractDebugAnnotations - Extracts Android annotations for the debug variant into the archive file
extractProfileAnnotations - Extracts Android annotations for the profile variant into the archive file
extractReleaseAnnotations - Extracts Android annotations for the release variant into the archive file

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Cleanup tasks
-------------
lintFix - Runs lint on all variants and applies any safe suggestions to the source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'android'.
components - Displays the components produced by root project 'android'. [incubating]
dependencies - Displays all dependencies declared in root project 'android'.
dependencyInsight - Displays the insight into a specific dependency in root project 'android'.
dependentComponents - Displays the dependent components of components in root project 'android'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'android'. [incubating]
projects - Displays the sub-projects of root project 'android'.
properties - Displays the properties of root project 'android'.
tasks - Displays the tasks runnable from root project 'android' (some of the displayed tasks may belong to subprojects).

Install tasks
-------------
installDebug - Installs the Debug build.
installDebugAndroidTest - Installs the android (on device) tests for the Debug build.
installProfile - Installs the Profile build.
installRelease - Installs the Release build.
uninstallAll - Uninstall all applications.
uninstallDebug - Uninstalls the Debug build.
uninstallDebugAndroidTest - Uninstalls the android (on device) tests for the Debug build.
uninstallProfile - Uninstalls the Profile build.
uninstallRelease - Uninstalls the Release build.

Verification tasks
------------------
check - Runs all checks.
connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.
connectedCheck - Runs all device checks on currently connected devices.
connectedDebugAndroidTest - Installs and runs the tests for debug on connected devices.
deviceAndroidTest - Installs and runs instrumentation tests using all Device Providers.
deviceCheck - Runs all device checks using Device Providers and Test Servers.
lint - Runs lint on all variants.
lintDebug - Runs lint on the Debug build.
lintProfile - Runs lint on the Profile build.
lintRelease - Runs lint on the Release build.
lintVitalRelease - Runs lint on just the fatal issues in the release build.
test - Run unit tests for all variants.
testDebugUnitTest - Run unit tests for the debug build.
testProfileUnitTest - Run unit tests for the profile build.
testReleaseUnitTest - Run unit tests for the release build.

To see all tasks and more detail, run gradlew tasks --all

To see more detail about a task, run gradlew help --task <task>

事の発端、なぜコマンドラインでビルドするのか

Android Studio の GUI から App Bundle の作成が gray out されてしまうという謎の現象にあってしまい、stackoverflow で聞いてもわからず、issue report しても拉致があかず、work around として、要は Android Studio が悪いので、Android Studio を使わないでコマンドラインでビルドする方法を探していました

references


Last Updated: 2/17/2020, 10:53:18 PM