LoginSignup
3
0

More than 1 year has passed since last update.

gradle "Error: Program type already present: "

Last updated at Posted at 2020-10-15

ライブラリのバージョン更新中に以下のようなエラーが出ました。

Error: Program type already present: android.arch.lifecycle.ViewModel

エラーについて

このエラーについて、Android Studio ユーザーガイド : 重複クラスエラーを修正する に説明があります。

Program type already present com.example.MyClass

このエラーは通常、以下のいずれかの状況で発生します。
・バイナリ依存関係に含まれているライブラリが、直接的な依存関係としてアプリにも含まれている場合。たとえば、アプリでライブラリ A とライブラリ B に対する直接的な依存関係が宣言されているものの、すでにライブラリ A のバイナリにライブラリ B が含まれている場合などです。この問題を解決するには、ライブラリ B を直接的な依存関係から削除します。
* アプリにローカル バイナリ依存関係があり、同じライブラリに対するリモート バイナリ依存関係も存在する場合。この問題を解決するには、バイナリ依存関係の一方を削除します。

build.gradle ファイル

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {

    // 移行中の古い環境です
    //ext.kotlin_version = "1.4.0"
    ext.kotlin_version = "1.2.51"
    repositories {
        google()
        jcenter()
    }
    dependencies {

        // 移行中の古い環境です
        //classpath "com.android.tools.build:gradle:4.0.1"
        classpath "com.android.tools.build:gradle:3.2.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app/build.gradle ファイル

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.example.gradleerrorsample"
        minSdkVersion 24
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:2.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation 'android.arch.lifecycle:extensions:1.0.0'
}

Make Project 等、ビルドを行うと以下のようなエラーになります。

image.png

Error: Program type already present: android.arch.lifecycle.ViewModel

調査 1

該当するクラスを import してみました。

image.png

android.arch.lifecycle.ViewModel
android.arch.lifecycle:extensions:1.0.0 に定義されているようです。

調査 2

gradle app:dependencies を実行し、依存関係を出力してみました。

image.png

151行目に android.arch.lifecycle:viewmodel:1.1.1 があります。
依存先をたどると com.android.support:appcompat-v7:28.0.0 が依存しています。
これにより、クラス名が衝突しているようです。

また、Lifecycle リリース # バージョン1.1.0 を参照すると、lifecycle:extensions はviewmodel を含めることをやめているようです。

対応

android.arch.lifecycle:extension:1.1.1 に更新して、ビルド自体は解決です。

image.png

その他

Android Gradle Plugin ( com.android.tools.build:gradle: ) を更新すると、このエラーは以下のように出力されます。
わかりやすくなってます。
(ライブラリの更新はまめにしましょう)

image.png

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0