6

I am using fastlane to automate my deployments to the play store. Here is a alpha lane as an example.

lane :alpha do    
    gradle(task: 'clean')
    gradle(
      task: 'assemble',
      build_type: 'Release',
      properties: {
        "android.injected.signing.store.file" => ENV["ANDROID_KEYSTORE"],
        "android.injected.signing.store.password" => ENV["ANDROID_KEYSTORE_PASSWORD"],
        "android.injected.signing.key.alias" => ENV["ANDROID_KEY_ALIAS"],
        "android.injected.signing.key.password" => ENV["ANDROID_KEY_PASSWORD"],
      }
    )
    upload_to_play_store(track: 'alpha')
end

I want to automate this deployment with Github actions. Whenever a commit to staging occurs, run fastlane alpha. The issue I am running into, however, is versioning. I need to bump the versionCode in my build.gradle file. I have been doing this manually before running fastlane alpha.

I want to remove the need to bump this value manually before I commit, how can I achieve this?

I have seen this fastlane plugin to increment the version code. If I, for example, commit once while the versionCode is at 1, then my workflow will automatically increment the versionCode to 2. When I commit again, the versionCode will still be 1, and Github will bump it to 2. However, the play store needs versionCode 3. The ideal solution is to have Github query from the Google Play Store the current versionCode of my app and supply that + 1 to fastlane. I have searched for a way to do this but cannot find a solution.

1

2 Answers 2

10

Have you tried this: https://docs.fastlane.tools/actions/google_play_track_version_codes/ ?

    g = google_play_track_version_codes
    gb = google_play_track_version_codes(track: 'beta')
    ga = google_play_track_version_codes(track: 'alpha')
    max_value = [g[0].to_i, gb[0].to_i, ga[0].to_i].max
    version_updated = max_value + 1
    increment_version_code(app_project_dir: "./app", version_code: version_updated.to_i)

Basically I'm taking all version codes of Google Play and increasing +1 the max one, and then using the versioning plugin to update it on the build.gradle

4
  • 1
    Just to be explicit, google_play_track_version_codes fetches the version code from the Google Play store, correct? Commented Mar 20, 2020 at 18:10
  • 2
    Yes, and you have to check all tracks to have the biggest one.
    – rcarba
    Commented Mar 21, 2020 at 9:38
  • 1
    I get this error Google Api Error: apkNotificationMessageKeyUpgradeVersionConflict: APK specifies a version code that has already been used. - APK specifies a version code that has already been used. even though the plugin increment_version_code says that Version code has been changed to 4 (which is one more than what is in the Google play store) Commented Mar 24, 2020 at 3:39
  • Marking answer as correct and moving follow up question here Commented Mar 24, 2020 at 4:00
1

You can add a lane to do this as described in this Medium post by Atul Anand:

lane:IncrementBuildNumber do

    path = '../app/build.gradle'
    re = /versionCode\s+(\d+)/
    s = File.read(path)
    versionCode = s[re, 1].to_i
    s[re, 1] = (versionCode + 1).to_s
    f = File.new(path, 'w')
    f.write(s)
    f.close

end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.