3

I am developing an android application. I have an java file that I want to use it differently in debug and release build types. I know I can have different resource files for debug and release versions. But is it possible to do the same with Java files also? How to achieve this?

1
  • Use flavors. Don't ask me how - I have no clue in terms of the specifics. I only know it exists, and that it's possible to apply for stuff like this. Commented Apr 3, 2019 at 20:08

1 Answer 1

6

Step #1: Put the debug edition of your Java class in src/debug/java/...

Step #2: Put the release edition of your Java class in src/release/java/...

Step #3: Ensure that you do not have a copy of this Java class in src/main/java/...

Now, a debug build will pull the Java code from the debug source set, while a release build will pull the Java code from the release source set.

Sign up to request clarification or add additional context in comments.

7 Comments

I did the above steps, but while trying to build the debug version, I am getting below error org.gradle.api.GradleException: No matching client found for package name 'com.agmodels.bluetooth.agmodelsfeed.debug'
@adarsh: I do not recognize that error message, and I do not know what "client" it might be referring to. It is possible that some plugin that you are using is now getting confused. I recommend that you ask a separate Stack Overflow question, where your minimal reproducible example includes the entire Gradle output, as the rest of the Gradle output may give people clues as to what specifically is generating the error message.
I have found the issue, it is because of the google-services.json file. stackoverflow.com/questions/34990479/…
I've found I had to "Invalidate Caches / Restart" because A.S. was unable to find the new location of the file. Also, pro-tip if the /debug folder is being ignored by git: update .gitignore with the line !/src/debug
@CommonsWare Your answer works, I just needed to Build -> Clean first (in case someone is having the same issue). Thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.