androidandroid-studiobuild-variant

Use a different class for each build variant


I have an Application class that I want to be different in each build variant like debug and release.
this is my map:

app/      
   |      
   |----debug/          
             |----java\            
                       |----com.example.App.class       
             |----res\
                      |----mipmap-hdpi       
   |----main/          
             |----java\      
                      |----com.example.App.class       
             |----res\
                      |----mipmap-hdpi            

but in android studio give me this error message "duplicate class found in ..."
but it does not show any error in my resources.
my question is why gradle cannot figure it out what class should use in different build variant but it can decide what resource must be used in different build variants.


Solution

  • Code under main is shared between all build types. Hence the class is there twice, once from debug and again from main.

    To have build type specific source files, put them under build type specific folders instead of main. For example if you have the usual debug and release build types, put the other variant under release.

    Why this is not needed for resources is because resource merging can override resources over build types. There is no code merging.