3

I have some code in my android application which I intend to keep only in debug-builds.

I am aware of this approach:

if (BuildConfig.DEBUG) {
    //code here
}

However, the problem is that my code relies on external dependencies which I would also like to keep in debug builds only :

debugCompile "dependency1"

//this wont compile in release mode
if (BuildConfig.DEBUG) {
    //code which references dependency1 
}

Given that there is no conditional compilation in java, are there any solutions besides commenting out blocks of code manually every time (which is obviously a huge hassle)?

EDIT: Egor pointed me in the right direction and after doing some more reasearch on "source sets" I found an answer which PRECISELY describes my situation and provides an excellent solution: https://stackoverflow.com/a/31483962/5790273

Community
  • 1
  • 1
Fire095
  • 3,715
  • 2
  • 11
  • 10

1 Answers1

2

Put the code you only want to compile in debug mode under "src/debug/java", as described here. Gradle allows you to use multiple source sets per build type and flavor, and will only compile those that are relevant for the build configuration.

Egor
  • 39,695
  • 10
  • 113
  • 130