質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

2回答

1392閲覧

androidstudioで、クラスが2つのモジュールに重複しているというエラーが出ました

tera877

総合スコア9

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2023/05/12 16:26

編集2023/05/12 17:05

androidstudioで発生した、クラスが2つのモジュールに重複しているというエラーを直したい

前提

androidが提供しているコードラボのナビゲーションコンポーネントの範囲を勉強していたら上記に書いたようなエラーが出ました。(エラーコードは下記)

実際に行ったことは、メインアクティビティに2つのフラグメントを設定するといったことです。その際にgradleファイルに少し書き足しましたが、チュートリアルの指示通りです。

実は、このチュートリアルを行うのは2回目で、1回目も同じところでエラーになり、解決できなかったためやり直しました。ですが、また同じところでエラーになりました。

エラーコードにはモジュール名が出ていますが、このモジュールがどこに入っているかわかりません。そのため、何のファイルを提示したらよいかわかりません。とりあえず、一番可能性のありそうなgradleファイルを張っておきます

発生している問題・エラーメッセージ

Duplicate class androidx.lifecycle.ViewModelLazy found in modules jetified-lifecycle-viewmodel-ktx-2.2.0-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0) and lifecycle-viewmodel-2.5.1-runtime (androidx.lifecycle:lifecycle-viewmodel:2.5.1) Go to the documentation to learn how to Fix dependency resolution errors.

該当のソースコード

kotlin

1/* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16plugins { 17 id 'com.android.application' 18 id 'kotlin-android' 19 id 'kotlin-kapt' 20 id 'androidx.navigation.safeargs.kotlin' 21} 22 23android { 24 compileSdkVersion 33 25 26 defaultConfig { 27 applicationId "com.example.wordsapp" 28 minSdkVersion 19 29 targetSdkVersion 33 30 versionCode 1 31 versionName "1.0" 32 33 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 34 } 35 36 buildTypes { 37 release { 38 minifyEnabled false 39 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 40 } 41 } 42 compileOptions { 43 sourceCompatibility JavaVersion.VERSION_1_8 44 targetCompatibility JavaVersion.VERSION_1_8 45 } 46 47 kotlinOptions { 48 jvmTarget = '1.8' 49 } 50 51 buildFeatures { 52 viewBinding = true 53 } 54} 55 56dependencies { 57 implementation "androidx.core:core-ktx:$core_ktx_version" 58 implementation "androidx.appcompat:appcompat:$appcompat_version" 59 implementation "com.google.android.material:material:$material_version" 60 implementation "androidx.constraintlayout:constraintlayout:$constraintlayout_version" 61 implementation "androidx.navigation:navigation-fragment-ktx:$nav_version" 62 implementation "androidx.navigation:navigation-ui-ktx:$nav_version" 63 64}

試したこと

チュートリアルの完成形のコードを実行するとしっかり動きます。ですが、自分で作ってみるとエラーになってしまいます。チュートリアルをそのまままねしているだけだと思うのですが。

また、gradleにdependencies {

implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'

}

を記述しましたが、下のエラーになります

”Duplicate class androidx.lifecycle.ViewModelLazy found in modules jetified-lifecycle-viewmodel-ktx-2.2.0-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0) and lifecycle-viewmodel-2.5.1-runtime (androidx.lifecycle:lifecycle-viewmodel:2.5.1)

Go to the documentation to learn how to Fix dependency resolution errors.”

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hoshi-takanori

2023/05/12 19:51

ライブラリのバージョンが合ってないんでしょうね。 $core_ktx_version, $appcompat_version, $material_version, $constraintlayout_version, $nav_version はそれぞれどんな値ですか? (というか、プロジェクトの build.gradle はどうなってますか?)
tera877

2023/05/13 02:34

dependencies { implementation "androidx.core:core-ktx:$core_ktx_version" implementation "androidx.appcompat:appcompat:$appcompat_version" implementation "com.google.android.material:material:$material_version" implementation "androidx.constraintlayout:constraintlayout:$constraintlayout_version" implementation "androidx.navigation:navigation-fragment-ktx:$nav_version" implementation "androidx.navigation:navigation-ui-ktx:$nav_version"  implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0' //対処法として追加したもの } このようになっています
hoshi-takanori

2023/05/13 03:11

それは app モジュールの build.gradle の一部ですよね。その $core_ktx_version とかの具体的な値を知りたいんですけど…。たぶんプロジェクト直下の build.gradle に書いてませんか?
tera877

2023/05/13 03:38

buildscript { ext { appcompat_version = "1.5.1" constraintlayout_version = "2.1.4" core_ktx_version = "1.9.0" kotlin_version = "1.7.10" material_version = "1.7.0-alpha02" nav_version = "2.3.1" } これですかね?間違っていたらごめんなさい
hoshi-takanori

2023/05/13 08:21

nav_version が古いようなので、新しいの (最新は 2.5.3) にすれば良いかも…。
tera877

2023/05/13 08:34

最新バージョンに設定しました。既述したエラーは消えました。ありがとうございます。なのですが、次に ”FATAL EXCEPTION: main Process: com.example.wordsapp, PID: 16689 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wordsapp/com.example.wordsapp.MainActivity}: java.lang.NullPointerException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment” というエラーが出ました。これは全く別のエラーですよね。
guest

回答2

0

イメージ説明
Rebuild module on dependency change のチェックボックスにチェックは入っているでしょうか?

投稿2023/05/12 17:30

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tera877

2023/05/12 17:39

回答ありがとうございます。すみませんが、その画面への生き方を教えていただけませんか。調べてはいるのですが、その画面に到達できません
tera877

2023/05/12 17:42

すみません、その画面に到達しましたが、そもそもそのチェックボックス自体がありません。2つしかありませんでした。
guest

0

以下のリンク先に回答がありました。
https://qiita.com/antk/items/aa90603f44e2b8ee259b

bundle.gradle に以下を追記します。

dependencies { ... implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0' }

投稿2023/05/12 16:46

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tera877

2023/05/12 17:03

既述し忘れましたが、これは一度やってみました。しかしまた違ったエラーが出ます。質問に捕捉を付けます。
退会済みユーザー

退会済みユーザー

2023/05/12 17:30

Bardに聞いてみました。Android Studioの設定を確認してください。もう1回回答を書いておきます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問