프로그래밍 언어/Java

[Android] Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by build file 'app\build.gradle' 오류 해결

happy_life 2021. 11. 4. 12:44
문제상황

라이브러리를 연결하기 위해 gradle에 코드를 입력하였으나, 이러한 오류가 발생

Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by build file 'app\build.gradle'

 

 

(문제해결 전) Build gradle 코드

plugins {
    id 'com.android.application'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.forcode"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

repositories {
    maven { url 'https://jitpack.io' }
}



dependencies {
    implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}


dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

 

settings.gradle 코드

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}
rootProject.name = "ForCode"
include ':app'

 

해결방법

 

settings.gradle에 있는

repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)

코드를

repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)

로 수정

 

Build Success 가 나옵니다.