LoginSignup
1
0

More than 3 years have passed since last update.

【Android】build.gradleからfastlaneでversionNameを取得する

Posted at

概要

fastlaneでversionNameを使用したい時、build.gradleで管理しつつ、FastfileでもversionNameを使えるようにしたので議事録として記載

build.gradle側の記載

app/build.gradle
apply plugin: 'com.android.application'
・・・
def versionMajor = 1
def versionMinor = 2
def versionPatch = 3

android {
    ・・・
    defaultConfig {
         ・・・
        versionName "${versionMajor}.${versionMinor}.${versionPatch}"
    }

それぞれバージョンを定義して、defaultConfigのversionNameで組み合わせる

Fastfile側の取得処理

fastlane/Fastfile
platform :android do

  before_all do |lane, options|
  end

  ・・・

  lane :version_name do
    app_version_name = get_version_name
  end

  after_all do |lane, options|
    # laneの実行が成功した際に呼ばれる
    ・・・
  end

  error do |lane, exception, options|
    # laneの実行がエラーになった際に呼ばれる
    ・・・
  end
end

def get_version_name
  version_major = 0
  version_minor = 0
  version_patch = 0
  File.open("../app/build.gradle") do |file|
    file.each_line do |line|
      if line.strip.start_with?("def versionMajor")
        version_major = line.split("=")[1].strip.to_i
      elsif line.strip.start_with?("def versionMinor")
        version_minor = line.split("=")[1].strip.to_i
      elsif line.strip.start_with?("def versionPatch")
        version_patch = line.split("=")[1].strip.to_i
      end
    end
  end
end

文字列にする場合は、それぞれの変数を組み合わせる
"#{version_major}.#{version_minor}.#{version_patch}"
versionNameを使いたいlaneで呼んであげる
アプリバージョンを上げる時はbuild.gradle側を更新するだけでfastlaneでも最新のversionNameを取得できる

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0