添加第三方Jar包后重建App遇Program type already present异常求助
Hey there, this error is a classic sign of duplicate class definitions in your project—and since your app worked perfectly before adding those four JARs, the issue is definitely that two (or more) of these JARs contain the same jpos.JposException class. Let's fix this step by step:
Step 1: Identify which JARs are causing the duplicate
First, we need to pinpoint the conflicting JARs:
- Unzip each of your JAR files (
bixolon_printer_v127.jar,icu4j-58_1.jar,jpos117-controls.jar,xerces.jar) and check if they contain ajpos/JposException.classfile. - Chances are, both
bixolon_printer_v127.jar(since it's a printer SDK built on JPOS) andjpos117-controls.jarinclude this class.
Step 2: Resolve the duplicate
You have two safe, actionable ways to fix this:
Option 1: Remove the redundant JAR
If the Bixolon printer SDK already includes all the JPOS functionality you need, try removing the jpos117-controls.jar entirely:
- Either delete the JAR from your
libsfolder, or comment out its line in yourbuild.gradle. - Update your dependencies block to look like this:
dependencies { implementation files('libs/bixolon_printer_v127.jar') implementation files('libs/icu4j-58_1.jar') // implementation files('libs/jpos117-controls.jar') // Commented out to resolve duplicate implementation files('libs/xerces.jar') // Keep your other existing dependencies here } - Clean your project (
Build > Clean Project) then rebuild it.
Option 2: Exclude the duplicate class via Gradle
If you absolutely need both JARs, you can tell Gradle to exclude the conflicting class during packaging. Add this packagingOptions block inside your android section in build.gradle:
android { compileSdkVersion 27 defaultConfig { applicationId "com.andalus.mobilesales" minSdkVersion 17 targetSdkVersion 27 versionCode 1 versionName "1.0" } // Add this block to exclude duplicate class packagingOptions { exclude 'jpos/JposException.class' // If you find more duplicate jpos classes, you can exclude the entire package: // exclude 'jpos/**' } }
⚠️ Note: Only use this option if you're certain the excluded class is redundant—excluding necessary classes can lead to runtime crashes.
Step 3: Verify the fix
After making either change, run a full rebuild of your project. If the error disappears, test your printer functionality to ensure nothing broke from removing/excluding the duplicate class.
内容的提问来源于stack exchange,提问作者Mohammad amarneh

