Gillis Haasnoot
Gillis Haasnoot

Reputation: 2278

gradle if else clause for "api" or "compileOnly" dependency statement

I have an installable app and an instant app version. In order to reduce the APK size for the instant app version i reduced some of the features. This also means that at runtime some libraries are not used anymore. They are still needed during compilation. In gradle there is a nice dependancy statement called "compileOnly". Works perfectly fine. My app size get reduced exactly as I wanted it to be. But whenever i want to compile the instantapp or installable i have to manually change all the gradle files and replace "Api" with "CompileOnly" or vice versa.

Is there a way to set a condition inside the gradle file. So I have something like:

//normal dependencies here.

if isInstantApp{
    compileOnly "org.twitter4j:twitter4j-core:$rootProject.ASNETwitter"
}
else if (isInstallable){
    compile "org.twitter4j:twitter4j-core:$rootProject.ASNETwitter"
}

That would save so much of my time.

PS. I am not looking for build flavors, because that is not the usecase here.

Upvotes: 0

Views: 646

Answers (1)

Lukas Körfer
Lukas Körfer

Reputation: 14493

You can simply create a new configuration and let either api or compileOnly extend from it:

configurations {
    shared
    (isInstallable ? api : compileOnly).extendsFrom shared
}

dependencies {
    shared 'my:dependency:1'
    // ....
}

Upvotes: 1

Related Questions