Quote
Hello @all, It me again probably π. Today I want to continue contribute the way to bundle and build completely application mobile which can ready to delivery to another platform, such as Google Play (Android) or App Store (IOS)
You can follow and take a look other blog of me about Setup Environment for build android, that steps will require and provide your environment to becoming ready integrate with build-tools which we approach today, Fastlane
What is fastlane ?
Introduce
Info
fastlane is the easiest way to automate beta deployments and releases for your iOS and Android apps. π It handles all tedious tasks, like generating screenshots, dealing with code signing, and releasing your application.
Yeah, in my opinion, fastlane
is simple way to automatically your progress when meet or need build pattern for multiple mobile platform. Well, maybe you can work React Native
and I think fastlane
is a great choice when you think about building CI/CD
for your mobile applications
Reason why fastlane
?
- 100% opensource
- Integration with all extension which need build and release your mobile applications
- In house building, you can run this on your machine, no expose anything
- Support multiple platform,
Mac
,IOS
andAndroid
- Simple your task with
fastlane
alias, and build-tool will take other responsibility
And many more reason and benefit when you can choose this tool for instead of multiple extensions
Install fastlane
So basically, your host will not exist fastlane
, you need to install and export PATH
to binary file to in use.
Before install fastlane
, you need to make sure your host have require tools and package, which use to setup that one, such as
- Ruby (Recommend for Linux)
- Bundler : You can use
sudo gem install bundler
(Recommend MacOS). Read more: Documentation
After already these tools on your host, go to install fastlane
with command
That all you need, you can check fastlane
in your host with --version
flag
Reach to important part when you make the automation progress, fastlane
usually define like ruby
, because of the module belong ruby
π, It simple and do not make you confusing when work for, but at lease you need to understand Appfile
and Fastfile
, do they mean
Appfile
Info
The
Appfile
stores useful information that are used across all fastlane tools like your Apple ID or the application Bundle Identifier, to deploy your lanes faster and tailored on your project needs.From fastlane-docs
Appfile
of Android, really simple when generate with only json_key_file
and package_name
With the IOS, it will have more things, because IOS application really mess up and hard to define or generate
You can use multiple bundle identifiers per environment with defining for_platform
or for_lane
block declaration
When you call lane test
, the default values will be overwritten by for_lane
block
If you want to access Appfile
with Fastfile
, you can use try_fetch_value
Fastfile
Info
The
Fastfile
stores the automation configuration that can be run withfastlane
. TheFastfile
is used to configurefastlane
. Open it in your favorite text editor, usingRuby
syntax highlighting.From fastlane-docs
Basic lane
define can be
You can create multiple lane, and run it by specify case or all base on your decision and expectation
With Fastfile
, you will have some special block
, extensions which can be helpful to operating automation, such as
-
Blocks
before_all
andafter_all
, It organizes the simple operations you need to perform before or after the request lane and actions similar to the lane. Those blocks are really useful when you want to repeat any process, such astest
-
error
block, which get executed when an error occurs, in any of the blocks (before_all, the lane itself or after_all). Usually to announce notification abouterror_info
toslack
-
You can use
fastlane new_action
to define the new extension withruby
, for performing specify job or anything else. Usually put in on locationfastlane/actions/[action_name].rb
You can call extension on
Fastfile
withother_action
functionalSo i do not familiar with these configuration, but you can take a look and read more about this one. Documetation
-
You also import the other fastlane file from another repositories or template with
import
andimport_from_git
Note
You should import the other Fastfile on the top above your lane declarations. When defining a new lane fastlane will make sure to not run into any name conflicts. If you want to overwrite an existing lane (from the imported one), use the override_lane keyword.
-
require
can be used if you need to integration another 3rd party from gem
fastlane
cli
The last thing you need to concern when define and choose fastlane
for automation progress is fastlane
command line. You will need it to perform jobs which define in fastfile
Take a look with -h
flag with fastlane
command
It has a lots of stuff, but if you just want to execution fastfile
, so simple move to location where fastlane
directories exist, trigger command
fastlane
advanced
With fastlane
, it hasβt to stop that, fastlane can interact with multiple configuration, actions and environments. You can take a time and look for in Fastlane Advanced Docs. Some topics which can interest you, such ah
Setup project with fastlane
To setup fastlane
for android or ios, you need to navigate to ./android
or ./ios
, run the command init
for create and generate configuration base on the platform
It will ask you couple question, for supply the generate you can make decision for question, like
- Provide the package name for your application when asked (e.g. io.fabric.yourapp)
- Press enter when asked for the path to your json secret file
- Answer βnβ when asked if you plan on uploading info to Google Play via fastlane (we can set this up later)
After that, you can ./fastlane
directory and gemfile
is create, and inside fastlane
you can see it include two file Appfile
and Fastfile
I do not familiar with upload application to Google Play, but maybe on next time, I will guide you about that. Before that, you can read and do step by step like instruction, key.json
is result of last progress (Include: secret-key, name for authentication to Google Play Console). Read more at Documentaion: Setting up supply
When you applied all kind of things, you will have folder android
, like
Gradle Configuration (Android)
When you build android, you need to setup the environments for build and release applications, you can read and check about requirements at Requirements things for build android
Next, let take a look about gradle
to understand what things usually config for android
project. Always, when create project with Android Studio
, environments will generate gradle
configuration files. Read more: Medium - What is Gradle and why do we use it as Android developers?. So i can help you brief information about configuration
file, like
Settings.gradle
Info
You can read more about
settings
in official documentation, with this file you can understand about
rootProject.name
Β assigns a name to the build, which overrides the default behavior of naming the build after the directory itβs in. Itβs recommended to set a fixed name as the folder might change if the project is shared - e.g. as root of a Git repository.apply from
, represent the way how script plugins are applied. This is how itβs described in theΒ official docs. (Resource: StackOverFlow - what is mean of βapply fromβ in gradle?)include(projectPaths)
Adds the given projects to the build. Each path in the supplied list is treated as the path of a project to add to the build. Note that these path are not file paths, but instead specify the location of the new project in the project hierarchy. As such, the supplied paths must use the β:β character as separator (and NOT β/β).includeBuild(rootProject)
includes a build at the specified path to the composite build.
Note
So it mean, the file will define settings what gradle will target for, that usually
react-native
project usenode_modules
to add and manage package,gradle
needs to understandnative-plugin
and others for project, sosettings.gradle
born for this responsibility
build.gradle (root project)
Info
The project-level build.gradle file is used to define global configuration and settings for the entire project. It is also used to implement dependencies and plugins on top level. By default, all configurations and settings inside our project-level
build.gradle
should be inherited by all the modules in the project, andbuild.gradle
files inside modules are able to override them.
The most common things that are set inside project-level build.gradle are the plugins that are necessary for building the Android app, such as:
- Android Gradle,
- Kotlin,
- Hilt.
Sometimes you also have a need to add libraries that you would like to use across your modules, like:
- Android Support Library,
- Google Play Services Library.
When you talk a look the build.gradle, that include some module with some reason
repositories{}
Configures the repositories for this project.dependencies {}
Configures the dependencies for this project.buildscript {}
Configures the build script classpath for this project.ext {}
Configuration libraries for build progress for this project
build.gradle (app module)
Info
The last and probably the most used one is
build.gradle
inside our app module. It is used for defining the configurations and settings used for this particular module. Functionality-wise, it has similar options as project-levelbuild.gradle
, but it brings a separation of concerns. So all the libraries and frameworks, that we want to use in our Kotlin code that is placed inside our app module, should be defined in dependencies block from this gradle file. All the plugins that are required for these libraries to work should also be defined in the same gradle file.
Inside our app module build.gradle
, we can find three main sections:
- Plugins,
- Android,
- Dependencies.
- React: Usually configuration when
react-native
project
This is a place where we can do a lot of configuration changes to our app. The most common things developers set here are:
- The versions of minSdk, targetSdk and compileSdk.
- Version code and name of the app.
- Product flavors and build types.
You can consult StackOverFlow - How to change apk name by using gradle like this format? article for understand more way to configuration name
of release application when bundle. If not, it will set to app
default
gradle.properties
Info
Gradle provides multiple mechanisms for configuring the behavior of Gradle itself and specific projects.
This way will help you more intervention to gradle
when you applied gradle assemble
or gradlew assemble
with more configuration for react-native
Define fastlane
android project configuration
So target on this part, I will focus how the fastlane
work, and how can use fastlane
with gradle
to build completely .apk
. This steps will prepare for next release production when you want to force your application to Google Play
First of all, you can use the script to setup environment, just couple things requirements when i refer on Setup Environment for build android article
Copy and create setup-android-env.sh
script, you change mode of file and execute the script by some command
Requirement about the environment when build android, at least
- 2 CPU vCore
- 4 GB Ram
>=
30 GB Disk (Recommend : SSD Disk)
When you install fastlane
, java
and common essential things. You need to reload profile of Linux
machine for get new environment.
Warning
Just base on my knowledge, when you want to perform build Android on host machine, you need to specify the better way to applied to your
ci/cd
progress
- If you use
Azure Pipelines
, and you have to self-hosted agents, you need to make sure setANDROID_HOME
andPATH
insiderunsvc.sh
, which can help the agents understand variables which use for android build. Read more: StackOverFlow - ANDROID_HOME not set (VSTS agent running as service on OS X)- If you use
Gitlab-CI
, you can easily usedocker
mode which usually run and build anything inside the docker, just need to use mobiledevops/android-sdk-image or softartdev/android-fastlane Docker image to build and upload android project to GGPlay
After that, if your project requirement about ndk
package in android, you can perform this command to install and verify license for your SDK
build tools (NOTE: Need to give approving for license
of SDK, for except any problems)
So your environment for already, navigate to android
directory, and go fastlane init
to create and setup project
That will create your fastlane
directory inside the project, and you can change and make the build
lane for progress the build and export to .apk
file, currently you donβt need to take care Appfile
, just focus on Fastfile
This lane you create will include two step, clean
and assembleRelease
clean
: Help you on deletion thebuild
directory if it existsassembleRelease
: This task will perform build with integration multiple things, call extension and module, and bundle anything to.apk
Fact
You need to wait the long time on the first build to integration
gradle
,java
package andnode_module
if you usereact-native
If you have Fastfile
, all things already and you just need to use fastlane
command line to perform build
lane. Location on ./android
If you want setup to the pipeline, you can use Azure Pipelines
, for example
So on the last of progress, you can find the .apk
file on route ./android/app/build/outputs/apk/release/app-release.apk
Warning
Maybe, with
android
build not easily like i said, it will include multiple problems from not enough package, version is too old or not compatible with your applications, β¦ The best solutions are googling and install, and switch couple version to find target version for you project ππππ
Update - Install Platform Tools
If you want to test and run application with your real device through adb
, I think you can try to absorb that with the script down below
After you complete that one you can try reload profile .bashrc
or .zshrc
to receive new update
Conclusion
Abstract
That all for today, I hope this articles can help you and more others people which struggling on setup android project, introduce
fastlane
which such powerful for automation building mobile application. On the next part, I will deliver you a way to deployment and upload application to google play for beta test πππ
Info
There are more traps in build android progress, please careful and google to find best part for your choice, donβt afraid about that, anything are already putting on Internet, just find and find. So enjoy, stay safe and see yah on the next release πππ