Search code examples
androidandroid-studiogradlerelease

How to set up signingConfigs only for release variants?


I've set up continuous deployment with fastlane that handles my code signing for release apps, annoying bit is that when I run them in debug in on my machine android studio fails because it can't find path to them (they are declared as env variables demonstrated below). I can fix this by creating dummy data for the env variables, but would rather android studio ignore this bit of code unless it is running a release buid.

So in my app/build.gradle right after defaultConfig I have

signingConfigs {
    release {
        storeFile file(System.getenv("MYAPP_STOREFILE"))
        storePassword System.getenv("MYAPP_STORE_PASSWORD")
        keyAlias System.getenv("MYAPP_KEY_ALIAS")
        keyPassword System.getenv("MYAPP_KEY_PASSWORD")
    }
}

I thought that putting it under release would do the trick, but no luck :/


Solution

  • Not a super elegant solution, but just wrap it with try-catch?

    signingConfigs {
        release {
            try {
                storeFile file(System.getenv("MYAPP_STOREFILE"))
                storePassword System.getenv("MYAPP_STORE_PASSWORD")
                keyAlias System.getenv("MYAPP_KEY_ALIAS")
                keyPassword System.getenv("MYAPP_KEY_PASSWORD")
           } catch (Exception ignored) {
                // Do stuff or just ignore
           }
        }
    }
    

    If you don't want try-catch you can try the following:

    signingConfigs {
        release {
            storeFile System.getenv("MYAPP_STOREFILE") ? file(System.getenv("MYAPP_STOREFILE")) : null
            storePassword System.getenv("MYAPP_STORE_PASSWORD")
            keyAlias System.getenv("MYAPP_KEY_ALIAS")
            keyPassword System.getenv("MYAPP_KEY_PASSWORD")
        }
    }