Conditional dependencies with Gradle, is it possible?

前端 未结 3 1234
醉话见心
醉话见心 2021-01-12 21:41

I have an Android project (already ported to Android Studio and using Gradle) that is made up of different modules.

The project is actually used to create two differ

3条回答
  •  长发绾君心
    2021-01-12 21:55

    Not the best way to do it, but if productFlavors is not enough to specify conditional dependencies you can rely on an inline if and evaluate it based on some value that can be injected via external properties.

    For example here is how I toggle LeakCanary (no-op is just the empty implementation of the other one):

    build.gradle

    dependencies {
        compile "com.squareup.leakcanary:leakcanary-android"+(project.ext.has("leakCanary")?"":"-no-op")+":1.3.1"
    }
    

    To build with com.squareup.leakcanary:leakcanary-android:1.3.1:

    $ ./gradlew :app:assembleDebug -PleakCanary
    

    By default it builds with the empty implementation com.squareup.leakcanary:leakcanary-android-no-op:1.3.1:

    $ ./gradlew :app:assembleDebug
    

    This provides a quick and more flexible way to toggle things using build command, but too much of it and things will get messy real quick.

提交回复
热议问题