Gradle build flavors for specific devices

391 Views Asked by At

My app is supposed to scan barcodes with the camera, if the user has a TC55 Motorola device, the in-built scanner is used. In order for it to work, I have to specify in the build.gradle file :

dependencies {
provided files('libs/com.symbol.emdk.jar')

However if i run an apk with this line on a non-TC55 device the app crashes. How can I create a single flavor build that works on both TC55 and non-TC55 devices?

Edit 1 :

AndroidRuntime: FATAL EXCEPTION: main
Process: company.mobile.application.app, PID: 28701
java.lang.RuntimeException: Unable to instantiate activity     ComponentInfo{company.mobile.application.app/com.company.mobile.application.ui.Pr    oductionActivity}: java.lang.ClassNotFoundException: Didn't find class     "com.company.mobile.application.ui.ProductionActivity" on path: DexPathList[[zip     file "/data/app/company.mobile.application.app-3.apk"],nativeLibraryDirectories=    [/data/app-lib/company.mobile.application.app-3, /vendor/lib, /system/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class     "com.company.mobile.application.ui.ProductionActivity" on path: DexPathList[[zip     file "/data/app/company.mobile.application.app-3.apk"],nativeLibraryDirectories=    [/data/app-lib/company.mobile.application.app-3, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2222)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363) 
at android.app.ActivityThread.access$900(ActivityThread.java:161) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:157) 
at android.app.ActivityThread.main(ActivityThread.java:5356) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
at dalvik.system.NativeStart.main(Native Method) 
1

There are 1 best solutions below

0
On

To use the Zebra's EMDK on a TC55 you need to use the EMDK library that is only available on Zebra devices like the TC55.
So, if you try to open an Activity that implements one of the EMDK Interfaces (EMDKListener, DataListener or StatusListener) your application will crash if the EMDK is not available.

So, starting from the AndroidManifest.xml you need to think that the EMDK library may not be available on the device:

<uses-library android:name="com.symbol.emdk" android:required="false" />

The key point is: don't implement one of the EMDK's Listener in your main Activity, otherwise your app will crash when running on a non Zebra device (missing the required EMDK library).

You can take a look at this sample application that check if the EMDK is available before making a second activity (based on the EMDK listeners) available.