|

|  CompilationError: Program type already present in Flutter: Causes and How to Fix

CompilationError: Program type already present in Flutter: Causes and How to Fix

February 10, 2025

Discover the causes of 'CompilationError: Program type already present' in Flutter and learn effective fixes in our comprehensive guide.

What is CompilationError: Program type already present Error in Flutter

 

Understanding CompilationError: Program type already present Error

 

  • This error occurs when there are conflicting classes present in your project's dependencies. It typically arises when two or more libraries (or modules) include the same Java class.
  •  

  • The error message usually pinpoints the specific class that is duplicated, mentioned as "Program type already present". This is crucial for diagnosing the issue in the project structure.
  •  

  • In Flutter, this problem might not be immediately obvious as it often stems from the native Android part of the application where dependencies are managed through Gradle.

 

Impact on Development

 

  • This error prevents your project from building successfully, halting the development process until resolved.
  •  

  • It can lead to significant delays in development, especially if the conflicting classes are deep within the dependency hierarchy, requiring thorough inspection to identify the root cause.
  •  

  • The error message can sometimes point to classes that are indirectly included by your dependencies, making it a challenging task to trace back to the source.

 

Example Scenario in Flutter

 

  • Consider a Flutter project that uses different third-party plugins, each with its own set of dependencies. If two plugins rely on different versions of the same library, a class within that library could be duplicated.
  •  

  • For example, suppose you encounter this error: `Program type already present: com.example.library.SomeClass`. It indicates that `SomeClass` is included in your project more than once.
  •  

  • Here’s a mock Dart code snippet to illustrate:
    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Demo App',
          home: Scaffold(
            appBar: AppBar(
              title: Text('Demo App'),
            ),
            body: Center(
              child: Text('Welcome to Flutter'),
            ),
          ),
        );
      }
    }
    

    However, despite having the above Dart code, the build might fail due to this error if a plugin you used brings conflicting native classes.

 

Understanding Error Logs

 

  • The build output will typically list the error similar to this:
    Execution failed for task ':app:transformClassesWithDexBuilderForDebug'.
    > com.android.build.api.transform.TransformException: 
      java.util.concurrent.ExecutionException: 
      com.android.builder.dexing.DexArchiveMergerException: 
      Error while merging dex archives: 
      Duplicated class com.example.library.SomeClass found in modules 
      jetified-lib1.jar (lib1) and jetified-lib2.jar (lib2)
    

    Reviewing this log helps identify which modules are responsible for the duplication.

  •  

  • Such error logs are vital for resolving these compilation issues, as they highlight where to look during troubleshooting.

 

What Causes CompilationError: Program type already present in Flutter

 

Root Cause of CompilationError: Program type already present

 

  • **Duplicate Classes**: One of the most common causes of the error "Program type already present" in Flutter is the presence of duplicate classes. This usually happens when the built APK contains the same class from multiple dependencies. If two libraries (dependencies) include the same Java class, the build system gets confused because it doesn't know which class to use.
  •  

  • **Multiple Libraries Including the Same Dependency**: When two or more libraries within your project depend on different versions of the same library, it may lead to the situation where the same class file is included multiple times. This generally happens when using package dependencies that internally depend on common libraries, like Firebase, Google services, etc.
  •  

  • **Mixed Version Handling**: Flutter's dependency management might pull in different versions of a library due to version conflicts or version constraints declared in dependencies. This can result in the duplicated class issues where different versions of a library introduce the same class but in slightly different ways.
  •  

  • **Local and Remote Dependencies**: Sometimes, developers might reference a library both as a local project dependency and a remote dependency. This results in the class files existing twice in the build process. This can happen, for instance, if you have a local module that's also added through a package manager like Maven.
  •  

  • **Dependency of Dependencies**: Dependencies themselves can have dependencies, which in technical terms are known as transitive dependencies. This means that one or more of your packages may have dependencies that you haven't directly included but which indirectly introduce conflicts of having the same class file.
  •  

  • **Improperly Managed ProGuard Configuration**: In some advanced use-cases, if you are using ProGuard for optimization and obfuscation, improper configurations might lead to duplication errors. Suppressing necessary rules or incorrectly removing essential parts can cause this issue indirectly as they can sometimes add classes or keep them from being minimized.

 

dependencies {
    implementation 'com.example:lib1:1.0.0'
    implementation 'com.other:lib2:2.0.0'
    // Both lib1 and lib2 might internally use the same classes
}

 

Omi Necklace

The #1 Open Source AI necklace: Experiment with how you capture and manage conversations.

Build and test with your own Omi Dev Kit 2.

How to Fix CompilationError: Program type already present in Flutter

 

Remove Duplicate Dependencies

 

  • Search in your `pubspec.yaml` for dependencies that might be included more than once, either directly or through transitive dependencies.
  •  

  • Ensure your dependencies are not conflicting with each other. Check for multiple versions of the same dependency and consolidate them if possible.

 

dependencies:
  flutter:
    sdk: flutter
  dependency_name: ^1.0.0  # Ensure there's no duplicate

 

Clean and Rebuild the Project

 

  • Automatically generated build files might cause conflicts. It's best to remove them and start fresh.

 

flutter clean
flutter pub get
flutter run

 

Check for Conflicting Plugins

 

  • Open the `android/app/build.gradle` and `android/app/src/main/AndroidManifest.xml`.
  •  

  • Examine these files for any redundant or conflicting plugin entries.

 

// Example entries in build.gradle
dependencies {
    implementation 'com.some.library:1.0.0'
    // Ensure there's no conflicting or redundant entry
}

 

Inspect android/app/proguard-rules.pro

 

  • If you are using ProGuard, conflicts might arise from the rules defined.
  •  

  • Check the rules for overlaps or redundant entries that might cause issues.

 

# Example of a proguard rule
-keep class some.package.** { *; }

 

Using pub for a Thorough Cleanup

 

  • Ensure that all cache and temporary files are cleared by running the following command:

 

flutter pub cache repair

 

Gradle Dependencies

 

  • In `android/build.gradle`, ensure the gradle version is compatible with your Flutter setup and dependencies.

 

buildscript {
    ext.kotlin_version = '1.5.21' // example version
    repositories {
        //...
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.2' // use compatible version
    }
}

 

Adjust Multidex Settings

 

  • If you have many methods causing the limit to be exceeded, enable multidex in your `app/build.gradle`:

 

defaultConfig {
    multiDexEnabled true
}

 

Integrate Gradle Tooling

 

  • Optimize your Gradle setup by synchronizing with the latest Android Gradle Plugin.
  •  

  • This assists in managing dependencies effectively and resolves potential conflicts.

 

Delete Derived Data

 

  • Remove the build files from Android Studio or Xcode to force regeneration of these files.

 

Note: Following these steps helps address dependency duplication and related conflicts, potentially fixing the CompilationError: Program type already present in your Flutter projects.

Omi App

Fully Open-Source AI wearable app: build and use reminders, meeting summaries, task suggestions and more. All in one simple app.

Github →

Order Friend Dev Kit

Open-source AI wearable
Build using the power of recall

Order Now

Join the #1 open-source AI wearable community

Build faster and better with 3900+ community members on Omi Discord

Participate in hackathons to expand the Omi platform and win prizes

Participate in hackathons to expand the Omi platform and win prizes

Get cash bounties, free Omi devices and priority access by taking part in community activities

Join our Discord → 

OMI NECKLACE + OMI APP
First & only open-source AI wearable platform

a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded

OMI NECKLACE: DEV KIT
Order your Omi Dev Kit 2 now and create your use cases

Omi Dev Kit 2

Endless customization

OMI DEV KIT 2

$69.99

Make your life more fun with your AI wearable clone. It gives you thoughts, personalized feedback and becomes your second brain to discuss your thoughts and feelings. Available on iOS and Android.

Your Omi will seamlessly sync with your existing omi persona, giving you a full clone of yourself – with limitless potential for use cases:

  • Real-time conversation transcription and processing;
  • Develop your own use cases for fun and productivity;
  • Hundreds of community apps to make use of your Omi Persona and conversations.

Learn more

Omi Dev Kit 2: build at a new level

Key Specs

OMI DEV KIT

OMI DEV KIT 2

Microphone

Yes

Yes

Battery

4 days (250mAH)

2 days (250mAH)

On-board memory (works without phone)

No

Yes

Speaker

No

Yes

Programmable button

No

Yes

Estimated Delivery 

-

1 week

What people say

“Helping with MEMORY,

COMMUNICATION

with business/life partner,

capturing IDEAS, and solving for

a hearing CHALLENGE."

Nathan Sudds

“I wish I had this device

last summer

to RECORD

A CONVERSATION."

Chris Y.

“Fixed my ADHD and

helped me stay

organized."

David Nigh

OMI NECKLACE: DEV KIT
Take your brain to the next level

LATEST NEWS
Follow and be first in the know

Latest news
FOLLOW AND BE FIRST IN THE KNOW

thought to action

team@basedhardware.com

company

careers

invest

privacy

events

products

omi

omi dev kit

omiGPT

personas

omi glass

resources

apps

bounties

affiliate

docs

github

help