thelawnmowerman
thelawnmowerman

Reputation: 12136

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 INSTALL_FAILED_MISSING_SHARED_LIBRARY error.

I think I could use two build variants:

I've tried including the SDK only in the release 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:

In main/java/package/PeripheralManager.java:

public class ScannerManager { 
   // fake 
}

In release/java/package/PeripheralManager.java:

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...

Upvotes: 9

Views: 2237

Answers (2)

thelawnmowerman
thelawnmowerman

Reputation: 12136

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!

Upvotes: 4

MohanRaj S
MohanRaj S

Reputation: 2008

Please Try this on your module build.gradle file, Here my Code, please let me know your feedback.Thanks in advance.

build.gradle

android {

    defaultConfig {
        ----some stuff---
        multiDexEnabled true


    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/maven/ch.acra/acra/pom.xml'
        exclude 'META-INF/maven/ch.acra/acra/pom.properties'

    }
    buildTypes {
        release {
            --some stuff----
        }
        debug {
        }
    }
    dexOptions {
        javaMaxHeapSize "2g"
    }
    } }
}

dependencies {
--some stuff----
}
}

Upvotes: 2

Related Questions