allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
I want to add a new dependency in gradle.build file but don't know where to add this section
There are usually two build.gradle files - one in the root folder of your project, and one in the app module (also any other modules you might have). You should add the above snippet within the root build.gradle file, for example:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
}
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Related
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.3.0' apply false
id 'com.android.library' version '7.3.0' apply false
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}
this is only what i see in my build.gradle project. Im trying add this code (to get some custom toast cause the toast.view is deprecated):
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
but i cant see allprojects{} and the rest of things that should be there
There are different ways of defining repositories in Gradle. Recently, projects created in the Android Studio wizard no longer include an allProjects block in build.gradle. They configure the dependency repositories in a dependencyResolutionManagement block in the settings.gradle file. For example:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
i'm studying how to use tensorflow lite on android studio with kotlin, and i got a problem now. i was referring a project to study, and this author's gradle file is very different with what i have been knowing.
plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
--> this is what i have been knowing from the first time.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.4"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
--> and this is the gradle file for project that i'm reffering.
what i'm wondering aboout is why the author is not using plugins{} and having buildscript{} and allprojects{}. plus even though i copied and pasted it on the new project that i made, it does not work and throws the error.
error message img
what should i do for this?
I'm trying to add Firebase SDK, follow the step guide of Firebase:
enter image description here
but the new version of Android Studio has a different build.gradle file, just:
plugins {
id 'com.android.application' version '7.1.1' apply false
id 'com.android.library' version '7.1.1' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
And I don't know how right to add code to my file. I've tried this:
buildscript {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
}
dependencies {
// Add this line
classpath 'com.google.gms:google-services:4.3.10'
}
}
plugins {
id 'com.android.application' version '7.1.1' apply false
id 'com.android.library' version '7.1.1' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
allprojects {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
}
}
But the log here is:
org.gradle.api.GradleScriptException: A problem occurred evaluating root project 'Uber clone'
Caused by: org.gradle.api.InvalidUserCodeException: Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle
at build_8l5l0a77l47futp20icywdlc2$_run_closure2$_closure3.doCall(D:\Android\AndroidProjects\Uberclone\build.gradle:25)
at build_8l5l0a77l47futp20icywdlc2$_run_closure2.doCall(D:\Android\AndroidProjects\Uberclone\build.gradle:23)
at build_8l5l0a77l47futp20icywdlc2.run(D:\Android\AndroidProjects\Uberclone\build.gradle:22)
There is no need to add the repositories and the allprojects inside the Gradle file. This is because both are already present inside the setting.gradle file. To solve this simply remove them as you can see below:
buildscript {
dependencies {
// Add this line
classpath 'com.google.gms:google-services:4.3.10'
}
}
plugins {
id 'com.android.application' version '7.1.1' apply false
id 'com.android.library' version '7.1.1' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I resolved this issue by commenting out 'dependencyResolutionManagement' section which is present in the settings.gradle.
Go to Gradle Scripts -> settings.gradle -> comment out 'dependencyResolutionManagement'
//dependencyResolutionManagement {
// repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
// repositories {
// google()
// mavenCentral()
// }
//}
Then paste the all project repository in Gradle Scripts -> build.gradle -> paste it below 'plugins' section
like this-
plugins {
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
}
allprojects {
repositories {
// Make sure that you have the following two repositories
google() // Google's Maven repository
mavenCentral() // Maven Central repository
}
}
I want to add lottie dependency in my project.
this is the way that lottie document recommends
implementation "com.airbnb.android:lottie-compose:$lottieVersion"
allprojects {
repositories {
...
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}}
here is my gradle
buildscript {
ext {
compose_version = '1.1.1'
lottie_version = '0.5.4-SNAPSHOT'
}}
plugins {
id 'com.android.application' version '7.1.1' apply false
id 'com.android.library' version '7.1.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false}
task clean(type: Delete) {
delete rootProject.buildDir}
as you can see there is no allProjects in my gradle.
how can I add Maven repository?
Go to your build.gradle module and inside dependencies section add
implementation "com.airbnb.android:lottie-compose:$lottieVersion"
Now go in settings.gradle and inside dependencyResolutionManagement -> repositories add this
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
When creating a new project in Android Studio Arctic Fox Canary 8, this is the app level build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.0-alpha08"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
When creating the same new project in the Android Studio 4.1.2, the app-level build.gradle file is this:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.2"
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
}
One of the libraries I am using requires the allprojects
I manually tried to add the allprojects section in Canary 8, received this error:
problem occurred evaluating root project 'My Application'.
> Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'
Why was the allprojects in Android Studio Canary 8 was removed and how can I add it back so that I can use the libraries?
In settings.gradle you can add the repositories you want to add to the project
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
jcenter()
maven { url "https://maven.xyz.com" }
}
}
In settings.gradle just comment out the whole block
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
Go to setting.gradle and replace the line:
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
with:
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
That's a gradle setting which allows this to work.
In new release of Android, provide a new way to define your repositories. Before this release we used build.gradle(Project level) file to define repositories. Now you should write then into settings.gradle file. That's why It collides with Gradle plugin and generate this error message:
Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'
We can solve it in two possible ways.
Solution 1:
Use previous way: Not to use dependencyResolutionManagement for repositories in settings.gradle file
Just keep settings.gradle file like below:
/*dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}*/
rootProject.name = "YourAppName"
include ':app'
Keep build.gradle(Project level) like below;
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
classpath 'com.google.gms:google-services:4.3.10' // If you use
}
}
allprojects {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Change or add the line at top of the build.gradle(Module level)
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.gms.google-services' // If you use
}
Solution 2:
Use new way: Use dependencyResolutionManagement for repositories in settings.gradle file
Just keep settings.gradle file like below:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "YourAppName"
include ':app'
Keep build.gradle(Project level) like below:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
classpath 'com.google.gms:google-services:4.3.10' // If you use
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Keep build.gradle(Module level) file as previous:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.gms.google-services'// If you use
}
Hopefully you overcome your problem.
allProjects{} block is removed from the updated version of gradle. So, in settings.gradle you can add the repositories you want to download and add to the project
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
maven {
url 'https://your/url/to/download/repository/'
credentials {
username = "your username"
password = "your password"
}
}
}
}
rootProject.name = "My Application"
include ':app'
just keep this line in settings.gradle
include ':app' (Put in First line)
comment rest of code if any.
Google has made some changes in the project label Gradle.
you can fix this by removing all project label configuration to setting.gradle file.
allprojects {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
mavenCentral()
}
}
Remove this code in the Gradle file and all your plugins code in setting.gradle file. Like below.
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
plugins {
id 'com.android.application' version '7.1.0-alpha03'
id 'com.android.library' version '7.1.0-alpha03'
id 'org.jetbrains.kotlin.android' version '1.5.10'
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "MVVMDEMO"
include ':app'