|

|  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 開発キット 2

無限のカスタマイズ

OMI 開発キット 2

$69.99

Omi AIネックレスで会話を音声化、文字起こし、要約。アクションリストやパーソナライズされたフィードバックを提供し、あなたの第二の脳となって考えや感情を語り合います。iOSとAndroidでご利用いただけます。

  • リアルタイムの会話の書き起こしと処理。
  • 行動項目、要約、思い出
  • Omi ペルソナと会話を活用できる何千ものコミュニティ アプリ

もっと詳しく知る

Omi Dev Kit 2: 新しいレベルのビルド

主な仕様

OMI 開発キット

OMI 開発キット 2

マイクロフォン

はい

はい

バッテリー

4日間(250mAH)

2日間(250mAH)

オンボードメモリ(携帯電話なしで動作)

いいえ

はい

スピーカー

いいえ

はい

プログラム可能なボタン

いいえ

はい

配送予定日

-

1週間

人々が言うこと

「記憶を助ける、

コミュニケーション

ビジネス/人生のパートナーと、

アイデアを捉え、解決する

聴覚チャレンジ」

ネイサン・サッズ

「このデバイスがあればいいのに

去年の夏

記録する

「会話」

クリスY.

「ADHDを治して

私を助けてくれた

整頓された。"

デビッド・ナイ

OMIネックレス:開発キット
脳を次のレベルへ

最新ニュース
フォローして最新情報をいち早く入手しましょう

最新ニュース
フォローして最新情報をいち早く入手しましょう

thought to action.

Based Hardware Inc.
81 Lafayette St, San Francisco, CA 94103
team@basedhardware.com / help@omi.me

Company

Careers

Invest

Privacy

Events

Manifesto

Compliance

Products

Omi

Wrist Band

Omi Apps

omi Dev Kit

omiGPT

Personas

Omi Glass

Resources

Apps

Bounties

Affiliate

Docs

GitHub

Help Center

Feedback

Enterprise

Ambassadors

Resellers

© 2025 Based Hardware. All rights reserved.