0

I am developing an Android application with Android Studio which will use industrial devices with barcode scanners. Some of them (different models from different companies) use a special SDK (I have to compile with a special SDK, not just API 23 for example).

My problem is that when I switch to an other device and I have to compile with an other SDK (or just API 23 for example), I have some code that doesn't work anymore : the imports don't exist so the objects and methods I use for a specific device neither.

My code looks like this :

if(Build.MANUFACTURER.equals(COMPANY_NAME)){
    // do some stuff that won't compile with an other sdk
}else if (Build.MANUFACTURER.equals(COMPANY_NAME)){
    // do some other stuff that compile with a specific sdk too...
}else if (...){
    ...
}

I don't want to remove the code or put it in commentaries since it will be executed only on specific devices (and I don't want to come back uncomment some code each time I want to compile for a specific SDK).

How can I do to keep my code (and not necessarly put it in commentaries) without showing errors for others SDK?

What I have found so far :

  • compileSdkVersion seems to not be able to be set in product flavors
  • we can put the compileSdkVersion depending on a condition like this :

    if (condition) {

     compileSdkVersion 23
    

    } else {

    compileSdkVersion 'Datalogic:Datalogic SDK v1:23'
    

    }

But the condition can't be from product flavors since it's executed after that. A "solution" is to put the condition in gradle.properties as mentionned here but it has to be changed every time I compile manually and doesn't solve my problem hanving some code that don't compile on a specific SDK version.

Community
  • 1
  • 1
leb1755
  • 1,386
  • 2
  • 14
  • 29

1 Answers1

0

You should use different flavors if you have code which is depending on a special device.

productFlavors {
    normal {
        resValue "string", "someApiKey", "1111"
    }

    samsung {
        applicationId 'com.example.special' // change the package name

        resValue "string", "someApiKey", "2222"
    }
    instantTest {
        minSdkVersion 21
    }
}

Then you can store your special code in src/samsung/java instead of src/main/java. The someApiKey above can be accessed by using BuildConfig.someApiKey.

Your dependencies will get a special prefix so instead of

compile 'something:general:0.0'

use

samsungCompile 'something:forSamsung:0.0'

Regarding API-levels you should use always the latest version as target and check with a runtime check if the method is available and add some fallback in that case:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // ...
}

Regarding the issue with different sets of build dependencies you could try this:

buildscript {
  dependencies {
    if(!this.properties['target'].equals('datalogic')) {
      compile '...'
    } else {
      // ...
    }
  }
}

You can specify that "target" via -Ptarget=datalogic, e.g. try this command: ./gradlew -Ptarget=datalogic assambleRelease.

rekire
  • 47,260
  • 30
  • 167
  • 264
  • It seems interesting but not what I want, or maybe I misunderstood. Can I specifiy the compileSdkVersion with this? – leb1755 Dec 21 '16 at 10:03
  • Theoretically you could, but you should not. It does not make sense to compile against a legacy SDK. You can make sure in your code that you don't use deprecated code. – rekire Dec 21 '16 at 10:04
  • I added a small example I'm using while development to speedup the instant app builds. There I increased the `minSdkVersion`. – rekire Dec 21 '16 at 10:07
  • I have an SDK not provided by Google but by an other OEM which include specific code that doesn't exist in the standard SDK (not something about lollipop or not for example). It looks like something like that : `compileSdkVersion 'Company:Company SDK v1:23'` – leb1755 Dec 21 '16 at 10:09
  • That is a very interesting case I never faced that before. Can I ask which SDK you are using? However I think that should work fine. Since the references to non existing code will be in a different flavor you don't see or get unsolvable references. – rekire Dec 21 '16 at 10:19
  • Apparently, it happens with Amazon SDK too. The one I use is from [Datalogic](http://datalogic.github.io/quick-start/android-studio.html) – leb1755 Dec 21 '16 at 10:32
  • I have found an other way by using intents to make it work, so I will use this instead. I never used command lines to build my project and I am not very confident with it but it seems to be the solution I should use if I still had the problem. One other thing that bother me is the fact that I should dispatch the code of my activity in 2 separate classes with 2 packages names differents... That seems a bit overkill for the little code I have inside. Marking your solution as correct since it answers the original problem. Thank you for your time. – leb1755 Dec 21 '16 at 13:08