Different source code for release / debug version

3

I saw that in android studio it is possible to have one AndroidManifest.xml for the debug version and another for release version. Well, I have an "AppMock.java" file with constants for testing.

I would like to not compile this source code in the release version or create an empty class to compile.

Is there any way to isolate or differentiate this code at generation release time?

    
asked by anonymous 27.09.2016 / 22:44

1 answer

4

Android Studio, when you create a module, automatically creates build types Debug and release . However, it only creates the source set   main/ .

In order to put code to be used as Build Variants (different versions of the app) of these build types , you must create their source set in>.

To create the source set , follow these steps:

  • In the Project panel, choose Project (1).
  • Right-click the /app/src folder and choose New-> Folder-> Java Folder (3). In the window that appears, choose debug for Target source set . Click Finish .

  • Repeat the previous step by shrinking release to Target source set .

Thefolder/app/srcnowhas,inadditiontothe/mainfolder,thedebug/andrelease/folders

Inthe/mainfolder,thecodecommontobothvariantsisplacedinthedebug/andrelease/folders,whichisspecifictoeach.

ButfirstyoushouldcreateaPackageineachofthe/debug/javaand/release/javafolders:

  • IntheBuildVariants(2)panelmakesurethatthedebugvariant(4)isselected.
  • Right-clickthe/debug/javafolderandchooseNew->Packageandenterthenameofthepackage.
  • IntheBuildVariants(2)panelselectthereleasevariant.
  • Right-clickthe/release/javafolderandchooseNew->Packageandenterthenameofthepackage.

Thefolderstructurewilllooklikethefollowingfigure:

Beforerunning,choosetheBuildVariantspanel,thevarianttobuild.

Formoreinformation,see Configure Build Variants in the documentation.

    
28.09.2016 / 19:55