2

Android 4.2 ZBarCodescan java application needs to compiled in Windows 7 x64 computer.

Its source code is located here.

Android Studio is installed, and code is imported using Github import command. In gradle file API was changed to 23

compileSdkVersion 23 

according to

How to use eclipse project from github for Android development

Android studio Build APK command produces exception

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:579)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:535)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:517)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:164)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
    at com.android.dx.command.dexer.Main.run(Main.java:277)
    at com.android.dx.command.dexer.Main.main(Main.java:245)
    at com.android.dx.command.Main.main(Main.java:106)

Trying to fix this by adding lines

dexOptions {
    preDexLibraries = false
}

to both project gradle lines according to

Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat

causes errors

Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoIcsImpl;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoJellyBeanMr2;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoStubImpl;

How to fix this ?

Answer here recommends to examine dependency tree. How to get this tree in Android Studio and how to fix it ?

After removing android-support-v4.jar exception

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:579)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:535)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:517)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:164)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
    at com.android.dx.command.dexer.Main.run(Main.java:277)
    at com.android.dx.command.dexer.Main.main(Main.java:245)
    at com.android.dx.command.Main.main(Main.java:106)

Still occurs.

Update. I tried to importfixes branch using https://github.com/cricket007/DeviceSDK/tree/fixes url but Test button and import produce error

error

How to import this branch ?

Community
  • 1
  • 1
Andrus
  • 26,339
  • 60
  • 204
  • 378
  • On the command line type `gradle dependencies` to get the dependency tree. Could you post it? – antonio Mar 19 '16 at 16:23
  • 1
    Why don't you add your Gradle file so we can try to reproduce the problem? – OneCricketeer Mar 19 '16 at 16:26
  • Is that Github link your repo? Why is android-support-v4 in the libs folder and not being built by Gradle? – OneCricketeer Mar 19 '16 at 16:30
  • You still have not followed my earlier comment to show the build.gradle file – OneCricketeer Mar 19 '16 at 20:53
  • Also, overall, this feels like an [XY Problem](http://meta.stackexchange.com/a/66378). You just want a Barcode Scanner? Use [this library](https://github.com/dm77/barcodescanner). I'm having a hard time follow the non-english commit messages and that repository you linked to is riddled with problems. – OneCricketeer Mar 19 '16 at 21:04
  • @cricket_007 My device is special scanner. It has 2 green and 2 white LEDs in addition to camera. Library you referenced seems to support only camera. One should position camera to correct square in android screen. This is not reasonable for 8 hrs scanning. Repo in question controls device scanner via serial port. Using it scans quickly by using green led indicators, no need to look into android screen. – Andrus Mar 19 '16 at 21:12
  • And the build.gradle for the `SmartDeviceSDK` module? – OneCricketeer Mar 19 '16 at 21:32
  • And there are no copied JAR files between the `SmartDeciveSDK/libs` and `TestDEMO/libs` directories? The only other thing that would cause a conflict is if the **contents** of the JAR files overlap. You have have to manually open them to see that though. – OneCricketeer Mar 19 '16 at 21:41
  • You could also follow the instructions it gives you to [get more output](http://stackoverflow.com/questions/21674091/how-to-add-stacktrace-or-debug-option-when-building-android-studio-project) – OneCricketeer Mar 19 '16 at 21:42
  • @cricket_007 File names in both project are different. I added `--stacktrace --debug --info` command line options. Same error as previously appears. I updated question. – Andrus Mar 19 '16 at 21:56
  • @antonio I tried in terminal window `C:\DeviceSDK>gradle dependencies` but but error `'gradle' is not recognized as an internal or external command, operable program or batch file.` – Andrus Mar 20 '16 at 07:00
  • Are you using Eclipse or Android Studio? I have forked the Github repo and made it work with Android Studio. – OneCricketeer Mar 20 '16 at 07:24

3 Answers3

5

UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;

It happens becuase you are adding twice the same class.

In your case you are using:

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.android.support:appcompat-v7:23.1.1'
}

The appcompat-v7 has a dependency with the support-v4.
You have the android-support-v4.jar in your libs folders.

Remove the android-support-v4.jar (you don't need it because appcompat has already it).

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • I dont understand how exactly to fix: should I remove the line ` compile 'com.android.support:appcompat-v7:23.1.1'` from file or delete some file ? I havent used java and gradle before, I have used only Visual Studio – Andrus Mar 19 '16 at 18:36
  • 1
    @Andrus You can remove the android-support-v4.jar because the appcompat dependency just has it – Gabriele Mariotti Mar 19 '16 at 18:43
  • I removed android-support-v4.jar from all places. `Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;` exception still appears. how to fix ? – Andrus Mar 20 '16 at 06:38
1

I made a pull request to the library after getting it to work in Android Studio.

If you get this error when trying to build

SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.

Or you don't see that, but any error saying local.properties is missing when doing an import, then add a local.properties file that points to your Android SDK location, then trying to import/rebuild.

For example on a Mac, that file would contain this line, but obviously change the file path to point at your respective SDK's location

sdk.dir=/usr/local/opt/android-sdk

Once done with that, it should load into Android Studio without a problem.

The code was also tested on a 4.2 emulator.


The easy way via a Git Terminal (could do the same with Git Desktop).

git clone https://github.com/cricket007/DeviceSDK /path/to/download/
cd /path/to/download/
git branch fixes

Then import /path/to/download/DeviceSDK into Android Studio as Import Project Gradle


The long way (via Android Studio)

Use https://github.com/cricket007/DeviceSDK.git as the Git repo

Import

Open Project, you can ignore most errors that popup

Go the the Menu Bar, select VCS > Enable Version Control Integration

Enable VCS

Choose Git

Go back to VCS > Git > Branches

Checkout fixes as a new local branch

Pick Branches

Checkout

Name the branch.

Choose Force Checkout if prompted

I don't know what clicking the Run button does with a Gradle project, but you should open Gradle View on the right side and open TestDemo, select installDebug

Gradle View

From here on you need the solution above for the local.properties file and enable ADB debugging on a physical device or test on an emulator.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Where to get working project ? Should your repository imported into Android Studio again to get it ? In Android Studio in Windows, prompt appears on load and SDK location is fixed automatically. – Andrus Mar 20 '16 at 09:22
  • Not sure what you mean. Do `git checkout ` to get the repo, and then `git checkout fixes` to switch to the implemented branch. – OneCricketeer Mar 20 '16 at 09:37
  • How to import fixes branch to Android Studio? I tried to import url `https://github.com/cricket007/DeviceSDK/tree/fixes` but got error repository test failed. I updated question with error image. – Andrus Mar 20 '16 at 10:50
  • So you want to checkout and modify code from Github repo, but don't understand Git? You need to use the command line or the git GUI to switch branches using `git checkout `... See more here https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell#Switching-Branches – OneCricketeer Mar 20 '16 at 14:19
  • Or just download a zip of the branch and extract it https://github.com/cricket007/DeviceSDK/archive/fixes.zip – OneCricketeer Mar 20 '16 at 14:33
  • I asked about doing this from Android Studio GUI. git clone command should used to retrieve project. git checkout send data to github. How to run your project in Android emulator or in debug mode in device? After pressing Run button and selecting Android emulator only empty Nextus image appears. I connected scanner with USB cable and debugging is enabled in its settings but Android Studion complains that there is no device in USB . – Andrus Mar 20 '16 at 22:02
  • No, `checkout` is used to "checkout" branches & files. `push` is the only command that sends to a remote repository. – OneCricketeer Mar 21 '16 at 00:53
  • Or you can use the initial Github project. And forget about the branch. My pull request was merged. – OneCricketeer Mar 21 '16 at 03:54
0

When I had the same error I had to comment out the following line in my build.gradle file:

compile fileTree(dir: 'libs', include: '*.jar')

Endre Börcsök
  • 477
  • 1
  • 8
  • 19
  • Why comment that line? Just remove all the other lines that you manually added that compile from the libs folder – OneCricketeer Mar 19 '16 at 16:25
  • I commented out this line in both projects. Now Build APK returns error `C:\DeviceSDK\SmartDeviceSDK\src\com\smartdevicesdk\barcodescanner\zxing\FormatSelectorDialogFragment.java:9: error: package com.google.zxing does not exist import com.google.zxing.BarcodeFormat;` how to fix? – Andrus Mar 20 '16 at 06:53