Skip to content

Instantly share code, notes, and snippets.

@davidjoneshedgehog
Last active December 13, 2021 23:40
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidjoneshedgehog/6e44ff0e909d1512a004c0fb5e211694 to your computer and use it in GitHub Desktop.
Save davidjoneshedgehog/6e44ff0e909d1512a004c0fb5e211694 to your computer and use it in GitHub Desktop.

Incrementing Android Version Documentation

Setup

Step One

Append the following version variable definitions to your app's build.gradle file, this can be anywhere but preferably at the top of the file above all "apply" declarations. You may set the values to your current build/version codes if this is not a new project.

def VERSION_BUILD=0
def VERSION_MAJOR=0
def VERSION_MINOR=0
def VERSION_PATCH=0

Step Two

In your gradle's default config, update the versionCode and versionName to use the variables declared in the gradle file. This is also where you can make changes to how your version name is displayed (the example below uses the major.minor.patch format), and you can also reuse the variables for example providing a versionName in a release configuration that omits the patch value.

versionCode VERSION_BUILD
versionName VERSION_MAJOR + "." + VERSION_MINOR + "." + VERSION_PATCH

Step Three

Include the increment_android_version_code and increment_android_version_code actions in your fastlane file, providing them both with the path to your app's build.gradle file, and providing the version name value to update to the increment_android_version_name action.

Step Four (Optional)

To commit the version bump to git, simply add your build.gradle path to the default FastLane "git_commit" command.

Available Actions

increment_android_version_code

increment_android_version_code(path: "app/build.gradle")

Increments the "VERSION_BUILD" value in the build.gradle file by one and sets the SharedValues::ANDROID_VERSION_CODE lane context variable.

increment_android_version_name

increment_android_version_name(path: "app/build.gradle", type: "patch")

Increments the version name value in the build.gradle file, the specific value to be updated (major, minor, or patch) is defined by the "type" parameter passed to the method. When updating the major version, the minor and patch values are reset to zero, similarly when updating the minor version, the patch value is reset to zero. This also sets the SharedValues::ANDROID_VERSION_NAME lane context variable.


module Fastlane
module Actions
module SharedValues
ANDROID_VERSION_CODE = :ANDROID_VERSION_CODE
end
class IncrementAndroidVersionCodeAction < Action
def self.run(params)
path = params[:path]
buildNumber = 0
foundBuild = false
data = File.read(path)
updated_data = data
data.each_line do |line|
if (line.start_with?('def VERSION_BUILD'))
foundBuild = true
buildNumber = line.delete("def VERSION_BUILD=").to_i + 1
updated_data = updated_data.gsub(line, "def VERSION_BUILD=#{buildNumber}\r\n")
end
end
if (!foundBuild)
UI.error "VERSION_BUILD not found in build.gradle file, please ensure file contains 'def VERSION_BUILD=0' declaration"
raise "Illegal Argument Exception : No VERSION_BUILD variable in build.gradle file"
end
File.open(path, "w") do |f|
f.write(updated_data)
end
UI.message "Android version code incremented to #{buildNumber}"
return Actions.lane_context[SharedValues::ANDROID_VERSION_CODE] = buildNumber
end
def self.description
'Increment the version code value in your projects version.properties file '
end
def self.is_supported?(platform)
platform == :android
end
def self.author
"david.jones@hedgehoglab.com"
end
def self.available_options
[
FastlaneCore::ConfigItem.new(key: :path,
description: "Path to your version.properties file",
optional: false)
]
end
def self.output
[
['ANDROID_VERSION_CODE', 'The new version code']
]
end
def self.example_code
[
'increment_android_version_code(
path: "/path/to/version.properties"
)'
]
end
def self.category
:project
end
end
end
end
module Fastlane
module Actions
module SharedValues
ANDROID_VERSION_NAME = :ANDROID_VERSION_NAME
end
class IncrementAndroidVersionNameAction < Action
def self.run(params)
path = params[:path]
type = params[:type]
major = 0
foundMajor = false
minor = 0
foundMinor = false
patch = 0
foundPatch = false
data = File.read(path)
data.each_line do |line|
if (line.start_with?("def VERSION_MAJOR"))
foundMajor = true
major = line.delete("def VERSION_MAJOR=").to_i
elsif (line.start_with?("def VERSION_MINOR"))
foundMinor = true
minor = line.delete("def VERSION_MINOR=").to_i
elsif (line.start_with?("def VERSION_PATCH"))
foundPatch = true
patch = line.delete("def VERSION_PATCH=").to_i
end
end
if (!foundMajor)
UI.error "VERSION_MAJOR not found in build.gradle file, please ensure file contains 'def VERSION_MAJOR=0' declaration"
raise "Illegal Argument Exception : No VERSION_MAJOR variable in build.gradle file"
end
if (!foundMinor)
UI.error "VERSION_MINOR not found in build.gradle file, please ensure file contains 'def VERSION_MINOR=0' declaration"
raise "Illegal Argument Exception : No VERSION_MINOR variable in build.gradle file"
end
if (!foundPatch)
UI.error "VERSION_PATCH not found in build.gradle file, please ensure file contains 'def VERSION_PATCH=0' declaration"
raise "Illegal Argument Exception : No VERSION_PATCH variable in build.gradle file"
end
if (type.casecmp("major").zero?)
major = major + 1
minor = 0
patch = 0
elsif (type.casecmp("minor").zero?)
minor = minor + 1
patch = 0
elsif (type.casecmp("patch").zero?)
patch = patch + 1
elsif(!type.casecmp("none").zero?)
UI.error "Please specify the version name value to increase (major|minor|patch|none)"
raise "IllegalArgumentException : No valid version name value provided"
end
updated_data = data
data.each_line do |line|
if (line.start_with?("def VERSION_MAJOR"))
updated_data = updated_data.gsub(line, "def VERSION_MAJOR=#{major}\r\n")
elsif (line.start_with?("def VERSION_MINOR"))
updated_data = updated_data.gsub(line, "def VERSION_MINOR=#{minor}\r\n")
elsif (line.start_with?("def VERSION_PATCH"))
updated_data = updated_data.gsub(line, "def VERSION_PATCH=#{patch}\r\n")
end
end
File.open(path, "w") do |f|
f.write(updated_data)
end
versionName = "#{major}.#{minor}.#{patch}"
UI.message "Android version name updated to #{versionName}"
return Actions.lane_context[SharedValues::ANDROID_VERSION_NAME] = versionName
end
def self.description
'This action updates the Android version name'
end
def self.is_supported?(platform)
platform == :android
end
def self.author
"david.jones@hedgehoglab.com"
end
def self.available_options
[
FastlaneCore::ConfigItem.new(key: :path,
description: "Path to your version.properties file",
optional: false),
FastlaneCore::ConfigItem.new(key: :type,
description: "Version name value to update [major, minor, patch, or none]",
optional: false)
]
end
def self.output
[
['ANDROID_VERSION_NAME', 'The new version name']
]
end
def self.example_code
[
'increment_android_version_name(
path: "/path/to/version.properties"
type: "patch"
)'
]
end
def self.category
:project
end
end
end
end
@joncursi
Copy link

Bravo! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment