'Get version number for Android using Fastlane

I'm using Fastlane to automate my iOS and Android releases for my React Native app. It works great I'm just struggling to get the current Android version number to pass into a Slack message once the app has been deployed. Below is my current Android lane:

lane :deploy_staging do
    gradle(task: "clean")

    puts "CONFIG: #{ENV['CONFIG']}"

    gradle(
        task: 'bundle',
        build_type: 'Release',
        print_command: false,
    )

    upload_to_play_store(track: 'internal')

   slack(
       message: "Test successfully deployed to Play Store",
       success: true,
       slack_url: "https://hooks.slack.com/services/test",
       attachment_properties: {
           fields: [
               {
                   title: "Environment",
                   value: "Staging",
               }
           ]
       }
   )
end

With iOS I run the following to get the version number:

  {
           title: "Version number",
           value: get_version_number(target:"testapp"),
  }

But there doesn't seem to be this method call for Android, is there an easy way for me to pull in the version number?



Solution 1:[1]

You can set the version code and version name as a local variable and pass them manually to your gradle method. Then use them in your slack method, too. Try something like this:

versionCode = 100
versionName = "1.0.0"

#...

gradle(
        task: 'bundle',
        build_type: 'Release',
        print_command: false,
        properties: {
              "versionCode" => versionCode,
              "versionName" => versionName,             
            }
      )
#...

slack(
       message: "Test version code: #{versionCode} and version name: #{versionName} successfully deployed to Play Store",
       success: true,
       slack_url: "https://hooks.slack.com/services/test",
       attachment_properties: {
           fields: [
               {
                   title: "Environment",
                   value: "Staging",
               }
           ]
       }
)

Solution 2:[2]

This fastlane plugin can do set & get of version name and version code https://github.com/beplus/fastlane-plugin-versioning_android

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Akif
Solution 2 mazen amr