Fastlane Configuration For Android Application
In this article, I will talk about the processes of uploading a version to the Play Store using fastlane from any CI/CD job of an Android project.
What is fastlane used for?
Fastlane is an open source platform aimed at simplifying Android and iOS deployment. Fastlane lets you automate every aspect of your development and release workflow [1].
- How to setup Fastlane on an existing project?
- How to upload release version to Play Store using Fastlane?
- How to integrate fastlane into JenkinsFile?
I will explain the installation process for Mac.
How to setup Fastlane on an existing project?
Open terminal on your Mac.
Let’s navigate to project folder.
Now next step is installation fastlane on project. I used Brew. Following command to terminal: brew install fastlane.
Now I have to init Fastlane to create files. Just type the following command to terminal: fastlane init
After this command fastlane ask to these questions:
Well done, fastlane config created successfully :)
You can check the Fastlane from the project folder.
Appfile is that contains general information of your application.
Fastfile is a file created for us to automate your application.
- Create Project Json File
You need to connect to Play Store with Project Json File. You can create json file with the following steps [1].
Add json file you created to Appfile in the fastlane folder as follows:
json_key_file("path/your/play-store-credentials.json")
package_name("com.test.fastlanedemoproject")
How to upload alpha versions to Play Store using Fastlane
In order to release a version to Play Store from the command line with Fastlane, we need to add lane to FastFile.
desc "Upload to Closed Test Alpha Track"
lane :upload_to_closed_test_alpha do
gradle(task: 'clean')
previous_build_number = google_play_track_version_codes(track: "alpha")[0]
current_build_number = previous_build_number + 1
gradle(
task: 'bundle',
build_type: 'Release',
print_command: false,
properties: {
"android.injected.signing.store.file" => "/AndroidKeystore.jks",
"android.injected.signing.store.password" => StorePassword,
"android.injected.signing.key.alias" => "YourAlias",
"android.injected.signing.key.password" => KeyPassword,
"android.injected.version.code" => current_build_number
}
)
upload_to_play_store(
track: "alpha",
skip_upload_apk: true,
aab:"/app/build/outputs/bundle/release/app-prod-release.aab")
end
There is an example lane added for the alpha version that I shared with you above.
In order to automatically increase the build version to be uploaded to Play Store, we need to do the following:
- get the latest build number from Play Store using google_play_track_version_codes()
previous_build_number = google_play_track_version_codes(track: "alpha")[0]
- Add 1 to build number
current_build_number = previous_build_number + 1
If you want to output .aab, you should set task: ‘bundle’.
Let’s examine the Properties.
You need to sign your output with properties. In the sample code I added above. If you want to hide your properties, you can use dotenv.
We upload the signed and generated output to the Play Store with the upload_to_play_store command.
upload_to_play_store:
- Generated application (.aab &.apk)
- Store Description Text
- Store Images
- Changelogs etc.
Back to terminal and just write fastlane upload_to_closed_test_alpha
After waiting, you will see a success message!
How to integrate fastlane into JenkinsFile?
To integrate fastlane into Jenkins, you can write the following similar commands in the application’s Jenkinsfile.
stage('Configure Fastlane Directory') {
steps {
script {
sh(script: "cp -r $WORKSPACE/fastlane $WORKSPACE/artifacts/fastlane")
}
}
}
stage('Create AAB and Deploy to Google Play Alpha Channel') {
steps {
script {
sh 'fastlane upload_to_closed_test_alpha'
}
}
}
This is how you can add the upload_to_closed_test_alpha that we wrote as lane to the Jenkins job with sh.
When we press the Build Now button on Jenkins Job, the new version will be set automatically and your version will be automatically released to the Play Store.
Thanks for reading this article! Hopefully this is helpful for you when setting up a CI/CD workflow to automate the Android application deployment process.
References:
[1] https://docs.fastlane.tools/getting-started/android/setup/