title: Jenkins read Android app version from build.gradle file to environment variable
body: <p>I want to push version tags to my git repository automatically when Jenkins creates a build. But in order to do this I need the <code>version name</code> and <code>version code</code> for the created build artifact.</p>
  
  <p>I'm currently using the following setup to achieve this (it works fine):</p>
  
  <ol>
  <li>Create a Gradle task in the build.</li>
  <li>Run a Gradle task that creates a properties file with the version name and version code in it;</li>
  <li>Using the <a href="https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin" rel="nofollow noreferrer">EnvInject</a> plugin read/inject the properties file, so that the environment variables are available to use in the current Jenkins job;</li>
  </ol>
  
  <p>The code to generate the <code>version.properties</code> file looks like this:</p>
  
  <pre><code>android.applicationVariants.all { variant -&gt;
      def taskName = "createVersionFile" + variant.flavorName.capitalize();
      if (tasks.findByPath(taskName) == null) {
          tasks.create(name: taskName) {
              doLast {
                  def prop = new Properties()
                  def propFile = new File("$buildDir/outputs/version.properties");
                  prop.setProperty('versionname', variant.versionName + '-' + variant.versionCode)
                  propFile.createNewFile();
                  prop.store(propFile.newWriter(), null);
              }
          }
      }
  }
  </code></pre>
  
  <p>This works (as mentioned before) but it's a quite unwieldy method, I'm forced to modify the <code>build.gradle</code> file in order for Jenkins to do it's work.</p>
  
  <p><strong>Is there an easier method, possibly without modifying the <code>build.gradle</code> file?</strong> Maybe by generating a second <code>build.gradle</code> file which
     includes the "version.properties" task?</p>