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?

adarsh
  • 115
  • 1
  • 8
  • 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. – Zoe Apr 03 '19 at 20:08

1 Answers1

5

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 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 Apr 06 '19 at 15:04
  • @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 [mcve] 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. – CommonsWare Apr 06 '19 at 15:08
  • 1
    I have found the issue, it is because of the google-services.json file. https://stackoverflow.com/questions/34990479/no-matching-client-found-for-package-name-google-analytics-multiple-productf – adarsh Apr 06 '19 at 15:17
  • 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` – Someone Somewhere Jun 05 '19 at 23:58
  • I tried the above but getting `Unresolved reference` for the common file. And the `release` path does not appear at all in Android Studio (`debug` does appear). – netcyrax Apr 05 '20 at 13:25
  • @Mike: You might want to ask a separate Stack Overflow question, where your [mcve] shows your directory structure and a screenshot of what you are seeing in Studio. – CommonsWare Apr 05 '20 at 13:28
  • 1
    @CommonsWare Your answer works, I just needed to Build -> Clean first (in case someone is having the same issue). Thanks! – netcyrax Apr 05 '20 at 15:24