thelawnmowerman thelawnmowerman - 8 months ago 46
Android Question

Different library/imports depending on build type

In an Android app, I need to use a specific SDK for a certain peripheral.

Everything works fine with a single build variant:

provided fileTree(include: ['peripheral.jar'], dir: 'libs')
compile fileTree(exclude: ['peripheral.jar'], dir: 'libs')

The problem is that the SDK relies on a shared library installed in the actual peripheral. If I try to install the app in any other Android device (for debugging purposes of the rest of the functionalities), I get a

I think I could use two build variants:

  • release
    , that uses the SDK

  • debug
    , that doesn't use the SDK

I've tried including the SDK only in the
build type:

releaseProvided fileTree(include: ['peripheral.jar'], dir: 'libs')
releaseCompile fileTree(exclude: ['peripheral.jar'], dir: 'libs')

But then, I can't compile the project because of the missing imports.

So I duplicated the only class that uses those imports:


public class ScannerManager {
// fake


import peripheral; <- IT FAILS WHEN COMPILING

public class ScannerManager {
// real code

No matters the build type I chose, I can't compile the project if the import is not resolved in one of the variants...

Answer Source

I finally solved it with flavours. The problem was trying to duplicate the same class in main and release folders. We can't do that. Instead, I had to duplicate it in, for example, debug and release, or flavour1 and flavour2, (one with the imports and the other without them) and removing it from main. Then, I can choose what to compile and everything works.

Thank you anyway for your help, +MohanRaj!