Different source code for release/debug version

Asked

Viewed 291 times

3

I saw that in android studio it is possible to have an Androidmanifest.xml for the debug version and another for the 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.

Do you have any way to isolate or differentiate this code at the time of release version generation?

  • 2

    This is possible. but when using debug and release variants if, for example, Mainactivity calls methods of this class it has to exist in both variants, however the methods may have different implementations. If that’s a deterrent, you’ll have to use Flavors.

1 answer

4


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

So you can put code to be used as Build Variants(different versions of the app) from these build types, has to create their own source set.

To create the source set, follow the following steps:

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

  • Repeat the previous step, shrinking release for Target source set.

inserir a descrição da imagem aqui

The briefcase /app/src will now, in addition to the folder /main, the folders debug/ and release/

In the folder /main the code common to both variants is placed in the folders debug/ and release/, which is specific to each.

But first, you must create a Package in each of the folders /debug/java and /release/java:

  • On the panel Build Variants(2) ensure that the variant is selected debug(4).
  • Right click on the folder /debug/java and choose the option New->Package and enter the name of package.
  • On the panel Build Variants(2) select the variant release.
  • Right click on the folder /release/java and choose the option New->Package and enter the name of package.

The folder structure shall be identical to the following figure:

inserir a descrição da imagem aqui

Before running, choose from the panel Build Variants, the variant to be built.

For more information see Configure Build Variants in the documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.