Skip to content

Instantly share code, notes, and snippets.

@boshajones
Last active June 29, 2017 02:59
Show Gist options
  • Save boshajones/b7088262cfdd33470381d0c24ff2097c to your computer and use it in GitHub Desktop.
Save boshajones/b7088262cfdd33470381d0c24ff2097c to your computer and use it in GitHub Desktop.

Incrementing Android Version Documentation

Setup

Step One

Create a property file named "version.properties" in the project root directory which contains the following properties. You may set the values to your current build/version codes if this is not a new project.

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

Step Two

Next in the project root directory, create a new gradle file which will contain the methods used to read the version.properties file. These can be found in the common-methods.gradle file which will expose methods "readBuildCode()" and "readVersionName()". The default version name format is major.minor.patch but this can be changed by altering the returned value in the "readVersionName()" method.

Step Three

In your apps build.gradle file, where your versionCode and versionName configuration is defined, add the following apply from line to the top of this file (under any apply plugin lines) and update your versionCode and versionName to call the methods provided by the common-methods.gradle file.

apply from: "$rootDir/common-methods.gradle"
versionCode readBuildCode()
versionName readVersionName()

Step Four (Optional)

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

Actions

increment_android_version_code

increment_android_version_code(path: "version.properties")

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

increment_android_version_name

increment_android_version_name(path: "version.properties", type: "patch")

Increments the version name value in the version properties 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.


// Export read methods as closures
ext {
readVersionName = this.&readVersionName
readBuildCode = this.&readBuildCode
}
// Read the version and build property values from the version.properties file
def readVersion() {
def versionFile = new File(project.rootDir, 'version.properties')
def version = new Properties()
def stream
try {
stream = new FileInputStream(versionFile)
version.load(stream)
} catch (FileNotFoundException ignore) {
} finally {
if (stream != null) stream.close()
}
// Defaults in case file is missing
if (!version['VERSION_MAJOR']) version['VERSION_MAJOR'] = "1"
if (!version['VERSION_MINOR']) version['VERSION_MINOR'] = "0"
if (!version['VERSION_PATCH']) version['VERSION_PATCH'] = "0"
if (!version['VERSION_BUILD']) version['VERSION_BUILD'] = "0"
return version
}
// Returns the version name in the major.minor.patch format
def readVersionName() {
def version = readVersion()
return "${version['VERSION_MAJOR']}.${version['VERSION_MINOR']}.${version['VERSION_PATCH']}"
}
// Returns the version code integer value
def readBuildCode() {
def version = readVersion()
def build = version['VERSION_BUILD'] as int
return build
}
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
data = File.read(path)
updated_data = data
data.each_line do |line|
if (line.start_with?('VERSION_BUILD'))
buildNumber = line.delete("VERSION_BUILD=").to_i + 1
updated_data = updated_data.gsub(line, "VERSION_BUILD=#{buildNumber}\r\n")
end
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
minor = 0
patch = 0
data = File.read(path)
data.each_line do |line|
if (line.start_with?("VERSION_MAJOR"))
major = line.delete("VERSION_MAJOR=").to_i
elsif (line.start_with?("VERSION_MINOR"))
minor = line.delete("VERSION_MINOR=").to_i
elsif (line.start_with?("VERSION_PATCH"))
patch = line.delete("VERSION_PATCH=").to_i
end
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
end
updated_data = data
data.each_line do |line|
if (line.start_with?("VERSION_MAJOR"))
updated_data = updated_data.gsub(line, "VERSION_MAJOR=#{major}\r\n")
elsif (line.start_with?("VERSION_MINOR"))
updated_data = updated_data.gsub(line, "VERSION_MINOR=#{minor}\r\n")
elsif (line.start_with?("VERSION_PATCH"))
updated_data = updated_data.gsub(line, "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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment