1

I have a set a path in my .bashrc which I wanna have access to from my build.gradle file. I'm using the commandLine method in gradle, but I can't seems to get it working.

My .bashrc:

APK_PATH="echo /Users/CPE/APK"
export APK_PATH

Which give me this result in a terminal:

$APK_PATH
/Users/CPE/APK

In my gradle.build file I have the following code:

def getOutputPath = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'echo', '$APK_PATH'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

applicationVariants.all { variant ->

        def newName;
        def versionNumber = variant.versionName;
        def appName = variant.name.replace("Release","");
        def date = getDate();

        if (variant.buildType.name == "release") {
            newName = appName + "_" + date + ".apk";
            releaseDir = getOutputPath() + "/" + appName;
        } else {
            newName = variant.name;
        }

        variant.outputFile = new File(releaseDir, newName);
    }

When i'm trying to make a release build I get the following error:

Unable to open '$APK_PATH/ostran/ostran_20141209.apk' as zip archive

Eixx
  • 313
  • 2
  • 14
  • From [this](http://stackoverflow.com/questions/9854176/in-gradle-is-there-a-better-way-to-get-environment-variables) it looks like you should be getting `APK_PATH` like `System.getenv('APK_PATH')` – arco444 Dec 09 '14 at 10:52

1 Answers1

0

Instead of using the .bashrc file you can use your local gradle.properties which I found WAAAY easier!

You can place a gradle.properties file in the Gradle user home directory. The properties set in a gradle.properties file can be accessed via the project object. The properties file in the user's home directory has precedence over property files in the project directories.

If this property does not exist, an exception will be thrown and the build will fail. If your build script relies on optional properties the user might set, perhaps in a gradle.properties file, you need to check for existence before you access them. You can do this by using the method hasProperty('propertyName') which returns true or false.

my flavors.gradle

applicationVariants.all { variant ->
        if (project.hasProperty('APK_PATH')) {
            def appName = variant.name.replace("Release", "");
            def releaseDir = variant.outputFile.parent
            def newName = variant.name;

            if (variant.buildType.name == "release") {
                newName = appName + "_" + getDate() + ".apk";
                releaseDir = APK_PATH + appName;
            }

            variant.outputFile = new File(releaseDir, newName);
        }
    }
Community
  • 1
  • 1
Eixx
  • 313
  • 2
  • 14