Set up custom library repo in gradle - android

I uploaded my lib to bintray to the custom private repo (myRepoName), then tried to use it in another project.
But I need to upload it somehow to dsl methods, otherwise it says Gradle method not found myRepoName
How to do it?
buildscript {
repositories {
myRepoName { url "https://bintray.com/myRepo/sdk/repo" }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
}
}

At the moment you are using a custom name for the repository you are declaring. Instead you will have to call an existing method on RepositoryHandler, the underlying domain object for the repositories method. To fix the error message, you will have to use the method maven for a Maven-based repository.
buildscript {
repositories {
maven {
name 'myRepoName'
url 'https://bintray.com/myRepo/sdk/repo'
}
}
}
Please also keep in mind that there's a difference between the repositories you define within the buildscript block and for repositories defined on the top-level of the build script. Please refer to the Gradle user guide for more information.

Related

Gradle 7.2: How to apply a custom Gradle SETTINGS plugin?

I am in the process of migrating buildSrc convention plugins into standalone plugins. There are a lot of examples for creating Gradle plugins for Project objects, but a real dearth for Settings and Gradle. I want to centralize the list of repositories that we use in our gradle.settings.kts files, so I've created a Gradle Settings plugin: RepositoriesPlugin.
It is implemented in the same manner at a Project plugin, however, I am unsure about how to interpret the following quote from Gradle's docs: "A plugin can instead receive a parameter of type Settings, in which case the plugin can be applied in a settings script.", as stated in the documentation at this link: Gradle 7.2 Doc
The following example shows how I've applied my settings plugin in a settings.gradle.kts file. Is this how a settings plugin is applied, as per the documentation?
I've included the basic settings plugin code, below, too.
I'd appreciate your help to clarify that I am doing this properly. I am sure that this posting will help others who stray away from just Project plugins.
Thanks for your time and interest..
// settings.gradle.kts
pluginManagement {
plugins {
id("com.abitofhelp.gradle.plugins.repositoriesplugin") version "1.0.0-1"
}
settings.extensions
getByType(RepositoriesPluginExtension::class).apply {
localRepoName = "local-repo"
localRepositoryPath = "../../local-repo"
}
// Set the plugin repositories for all projects.
//repositories {
// maven { name = "localRepo"; url = uri(file("./local-repo")) }
// gradlePluginPortal()
// mavenCentral()
//}
}
// repositoriesplugin.kt
open class RepositoriesPlugin: Plugin<Settings> {
override fun apply(settings: Settings) {
val extension: RepositoriesPluginExtension =
settings.extensions.create("repositoriesPlugin", RepositoriesPluginExtension::class.java)
val localRepositoryName = extension.localRepositoryName?.let { it }?: "../../localRepo"
val localRepositoryPath = extension.localRepositoryPath?.let { it }?: "../../local-repo"
settings.pluginManagement.repositories.apply {
// Set the PLUGIN REPOSITORIES for all subprojects.
maven { repository ->
repository.name = localRepositoryName
repository.url = URI.create(localRepositoryPath)
}
gradlePluginPortal()
mavenCentral()
}
settings.dependencyResolutionManagement.repositories.apply {
// Set the DEPENDENCY REPOSITORIES for all subprojects.
maven {
it.name = localRepositoryName
it.url = URI.create(localRepositoryPath)
}
mavenCentral()
gradlePluginPortal()
}
}
}
I just had to figure this out myself. You weren't far off with your initial attempt but the plugins block actually has to go after the pluginManagement block. The basic layout of my working implementation looks like this:
// settings.gradle.kts
rootProject.name = "myProject"
pluginManagement {
repositories {
maven {
url = uri("/tmp/maven-local")
gradlePluginPortal()
}
}
}
plugins {
id("com.example.my-plugin") version "1.0-SNAPSHOT"
}
I believe you need to add the plugin to settings.gradle classpath via buildscript closure. pluginManagement.plugins makes the plugin availabile to the root (& sub) project build.gradle classpath, but I think that is a different classpath from the settings.gradle script.
To add to settings gradle, give this a try:
// settings.gradle
buildscript {
repositories {
maven { url "http://repo.where.plugin.is.published" }
}
classpath("complete.maven.coordinates:your-settings-plugin:version")
}
pluginManager.apply(your.settings.plugin.id)
As Justin Warkentin already noted, you first need to add your repo in the pluginManagement section and then you can apply your plugin like any other.
If you want to bypass the repository, you can also add the plugin project as composite build via includeBuild in the pluginManagement section.
In my example I have combined both methods. If the plugin project exists locally, this one is used, otherwise the version from the repo is used. This way you can (temporarily) add your plugin project to develop on it and test it directly in including projects.
pluginManagement {
repositories {
// your plugin repo; maybe...
mavenLocal()
}
// use local copy of your plugin if available,
// otherwise load plugin from repo
def name = 'your_plugin_project'
if (file(name).isDirectory()) {
includeBuild(name)
}
}
plugins {
// apply your plugin
id 'your.plugin.id'
}

Automatically add repositories from dependencies

I have a library with a dependency that requires a custom repo
dependencies {
implementation 'com.chartboost:chartboost-sdk:8.1.0'
}
repositories {
mavenCentral()
maven { url "https://chartboostmobile.bintray.com/Chartboost" }
}
This works fine, but it forces me to add maven { url "https://chartboostmobile.bintray.com/Chartboost" } to every project that uses that library. I would like to know, is there a way to modify my own library so I wouldn't need to add the custom maven repo for that dependency? Something that would add all the repos in the library to the project. I tried switching implementation with api to no avail
You can put your repository into [USER_HOME]/gradle/init.gradle file:
allprojects {
repositories {
mavenCentral()
maven { url "https://chartboostmobile.bintray.com/Chartboost" }
}
}
then they are will be applied to all projects

YoutubeExtratctor Android dependency Error

I have an error in this dependency
implementation 'com.github.HaarigerHarald:android-youtubeExtractor:v1.7.0'
and the error is
ERROR: Failed to resolve: com.github.HaarigerHarald:android-youtubeExtractor:v1.7.0
I'm trying to use it to fetch an url of youtube video so that I can use it in Exoplayer
Do you have already the jitpack.io added to your repositories config in the build.gradle file?
Something like
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" } // this is the line that you probably need
}
}
The idea is that gradle needs to know where to find artifacts build from github, this is usually from jitpack.io. By default, this line is not generated in the build.gradle file so you must add it manually.
Hope it helps

Android Studio project structure VS build.gradle

I'm trying to add a maven repository to my project by adding it to build.gradle
repositories {
maven {
url "https://XXX/"
}
}
It works fine when I build in the console. When I try to build it with Android Studio, the dependency that I use in that repository is not found.
When I add the repository to File > project structure > project > Default library Repository bam! it works.
What am I doing wrong? why do I have to duplicate the information? What is exactly the purpose of that configuration since it's already in build.gradle file?
Thank you
Aghilas, i am not sure if it is issue with https or http.
I am putting down what works for me. you can add 'maven()' in the block depending on that repo is required for buildscript functionality or for your app.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}

Why does the order of imported repositories in build.gradle make or break the project?

Please explain why the first code gives me syncing errors while the second doesn't.
allprojects {
repositories {
jcenter()
google()
}
}
Failed to resolve: play-services-base
Open File
Failed to resolve: play-services-tasks
Open File
Changing the repository order syncs just fine:
allprojects {
repositories {
google()
jcenter()
}
}
Can someone give me a reason or educated guess why this occurs?
This doc might be of use for you:
https://docs.gradle.org/current/userguide/declaring_repositories.html
Right down the bottom it mentions:
Note: The order of declaration determines how Gradle will check for dependencies at runtime. If Gradle finds a module descriptor in a
particular repository, it will attempt to download all of the
artifacts for that module from the same repository. You can learn more
about the inner workings of Gradle’s resolution mechanism.

Categories

Resources