Difference between google() and maven { url 'https://maven.google.com' } - java

Is there any difference between google() and maven { url 'https://maven.google.com' } in build.gradle file and if there is any, what is it?
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
// OR
google()
}
}

The google() repository is a shortcut to Google's maven repository. It was introduced in Gradle 4.x+. The actual repository URL used is `"https://dl.google.com/dl/android/maven2/" as specified here. https://maven.google.com actually points to the same repository.
However, if you are planning to use the google() shortcut, you need Gradle 4.x+, Android Studio 3.x+ and Gradle plugin for Android 3.x+.

Small correction to the answer above.
If you try to go to https://dl.google.com/dl/android/maven2/ it gives you a 404.
The correct url to google maven repository is:
https://dl.google.com/dl/android/maven2/index.html
or just
https://maven.google.com
Here you can check all the supported libraries and the latest versions.

When using gradle, you can mention multiple repositories which the build tool (gradle) uses to resolve dependencies mentioned in your project.
repositories {
jcenter()
maven { url 'https://maven.google.com' }
google()
}
In the above scenario, you're mentioning 3 repositories which gradle can use to resolve dependencies—all of which are Maven repositories.
1. jcenter()
Means the JCenter Maven repository.
This is a shortcut available in later versions of gradle
2. { url 'https://maven.google.com' }
This means you are referring a Maven repo hosted at the URL which can be used by gradle to resolve the dependencies.
If you want, you can actually enter the URL for JCenter and this would be the same as mentioning jcenter() in the gradle file.
3. google()
This means the Google Maven repository
Similar to the notation maven(), this can be used in later versions of gradle only

Related

Can I use both jcenter() and mavenCentral() to implement Firebase in my app?

So I am trying to implement Firebase in my app and I have seen in one step that in the guide it says the following ("Make sure that you have the following two repositories"):
repositories {
// Make sure that you have the following two repositories
google() // Google's Maven repository
mavenCentral() // Maven Central repository
}
In my project I am using jcenter() because I have 2 libraries that are from jcenter() and not from mavenCentral(). So my question is, if in the Firebase's guide it says "make sure to have mavenCentral(), can I use both jcenter() and mavenCentral() to continue having my libraries available at the same time as I am able to integrate Firebase? This is the code that I am thinking to put in build gradle:
buildscript {
repositories {
// Make sure that you have the following two repositories
google() // Google's Maven repository
mavenCentral() // Maven Central repository
jcenter() //code added by me to the guide's code
}
dependencies {
...
// Add the dependency for the Google services Gradle plugin
classpath 'com.google.gms:google-services:4.3.13'
}
}
allprojects {
...
repositories {
// Make sure that you have the following two repositories
google() // Google's Maven repository
mavenCentral() // Maven Central repository
jcenter() //code added by me to the guide's code
}
}
You can specify both. The addition of other repositories has no effect on the way Firebase works, as everything for Firebase is hosted out of google. Just make sure google is always listed first.

Android studio couldn't load JitPack library

I am using one library from github to create a tableview but the tableview is showing an error in my project. I have added their implementation in my gradle file.
Library Link : https://github.com/evrencoskun/TableView
JitPack seems to be published on jcenter whose use is now deprecated in Android. All non-Google artifacts now come from mavenCentral. The solution isn't documented in the official JitPack docs, but we can use the dependencyResolutionManagement block, like,
// settings.gradle
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
maven { url "https://jitpack.io" }
}
}
rootProject.name = "App"
include ':app'
The code will enforce settings repositories on the repositories mentioned in build.gradle. For repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) the official docs say,
If, for some reason, a project or a plugin declares a repository in a project, Gradle would warn you. You can however make it fail the build if you want to enforce that only settings repositories are used
Refer to this answer along with this one.

Gradle sync fails with Could not find com.android.tools.build:gradle:7.2

I am using android studio 4.2 and I live in Iran so I can't access google() and jcenter() repositories due to sanctions. I was looking for alternative ways for a week now and using vpn or any ip changer is not working because of Google's new location finding algorithm
installed Gradle locally via distributionUrl in gradle-wrapper
distributionBase=GRADLE_USER_HOME
distributionUrl=https://services.gradle.org/distributions/gradle-7.2-all.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
but ended up with this error
Could not find com.android.tools.build:gradle:7.2.
Searched in the following locations:
https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/7.2/gradle-7.2.pom
If the artifact you are trying to retrieve can be found in the
repository but without metadata in 'Maven POM' format, you need to
adjust the 'metadataSources { ... }' of the repository declaration.
my build.gradle
buildscript {
repositories {
google()
}
dependencies {
classpath "com.android.tools.build:gradle:7.2"
}
}
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}

Why does gradle try to download a Kotlin compiler during :app:lint?

I was trying to gradle build a plain Java Android project. It was an ADT project imported to Android Studio. To my astonishment, at one point Gradle tried to download the Kotlin compiler!
* What went wrong:
Execution failed for task ':app:lint'.
> Could not resolve all files for configuration ':app:lintClassPath'.
> Could not download kotlin-compiler.jar (com.android.tools.external.com-intellij:kotlin-compiler:26.2.0)
> Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/external/com-intellij/kotlin-compiler/26.2.0/kotlin-compiler-26.2.0.jar'.
> Read timed out
Why the heck did gradle attempt downloading a big package like the Kotlin compiler
How do I prevent that?
More generally, how do make gradle ask me before it tries to download anything or at least prevent it from downloading non-dependencies? Such things should be installed by my distro's package manager, after all!
1) Because Gradle supports linting Kotlin code, which requires parsing it, which is implemented using classes in the Kotlin compiler.
2) By not using the lint task I guess?
3) The Kotlin compiler is a dependency. Gradle requires a specific version of this dependency in a specific layout and in a specific location, and it can't use a version installed by your package manager.
change
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
to
buildscript {
repositories {
google()
maven {
url "https://maven2.google.com"
}
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}
allprojects {
repositories {
google()
maven {
url "https://maven2.google.com"
}
jcenter()
}
}

Gradle dependency resolution

I have an issue with how gradle resolves my dependencies.
I have four repositories that I need to investigate for different jars, five counting Maven central. Thus my repo statment in gradle.build looks like this:
repositories {
maven {
url 'urltoRepoA'
artifactUrls mavenLocal()
}
maven {
url 'urltoRepoB'
artifactUrls mavenLocal()
}
maven {
url 'urltoRepoC'
artifactUrls mavenLocal()
}
maven {
url 'urltoRepoD'
artifactUrls mavenLocal()
}
mavenCentral()
}
What I want to acheive:
Look for dependencies both in the remote repositories and the local maven repository.
But I get this error below, that is a jar that should be resolved from repoA (repoA is a mirror of maven central, and I have verified that this jar can be found there)
[16:43:10][Step 1/3] > Could not resolve all dependencies for configuration ':runtime'.
[16:43:10][Step 1/3] > Artifact 'junit:junit:4.11#jar' not found.
According to what I've read in gradles manual is that it tries to resolve all the dependencies from the same repo. Is that what I'm running in to here? Or have I failed to configure gradle properly?
I suspect there is something wrong elsewhere in your gradle configuration. I think you are misunderstanding how gradle resolves artifacts.
According to the gradle docs (see section 8.5)
A project can have multiple repositories. Gradle will look for a
dependency in each repository in the order they are specified,
stopping at the first repository that contains the requested module.
In fact, it's rather common to have multiple repositories in a gradle script.

Categories

Resources