In my gradle project i have two subprojects, one called api and the other core:
api.gradle
plugins {
id 'java-library'
}
repositories {
maven { url 'https://repo.spongepowered.org/repository/maven-public/' }
}
dependencies {
api('org.spongepowered:spongeapi:8.0.0')
}
core.gradle
dependencies {
implementation(project(':api'))
}
The problem is that when I try to compile the core subproject I get the following error:
> Could not find org.spongepowered:spongeapi:8.0.0-SNAPSHOT.
Adding the spongepowered repository in core.gradle works, but I wanted to know if there is a way to also add the repository transitive to reduce boilerplate.
Repositories used by every subproject (in your case - api & core) can be declared in the settings.gradle file at the root of the project:
settings.gradle
dependencyResolutionManagement {
repositories {
mavenCentral()
maven { url 'https://repo.spongepowered.org/repository/maven-public/' }
}
}
Related
I have written my own library for reuse in my projects which I push to github packages using
gradle publish
When I try to use it in my project, it works fine, and I can see the .pom, .jar and .module file nicely in my local gradle ~/.gradle/caches/modules-2/files-2.1/
However, every time I run my app, it tries to download the library again, even though it has it locally. If I comment out the repository block, the build fails stating it can't find my library, listing the places it tried to look, which are the other sources listed in my repositories {} block (MavenLocal, google etc etc)
Why can my library not be found? It's the same if I leave the repo in and use --offline, it can't find it.
This is how I get the library (uk.co.wheelergames.boardgame)
buildscript {
project.ext.roboVMVersion = "2.3.18-SNAPSHOT"
project.ext.roboVMGradleVersion = "2.3.18-SNAPSHOT"
project.ext.objectboxVersion = "3.1.2"
repositories {
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
classpath 'com.mobidevelop.robovm:robovm-gradle-plugin:2.3.18-SNAPSHOT'
classpath group: 'com.mobidevelop.robovm', name: 'robovm-gradle-plugin', version: project.roboVMGradleVersion
classpath("io.objectbox:objectbox-gradle-plugin:$objectboxVersion")
}
}
allprojects {
apply plugin: "eclipse"
version = '1.0'
ext {
appName = "The Networks"
// gdxVersion = '1.10.1-SNAPSHOT'
gdxVersion = '1.10.1-wheelergames-SNAPSHOT'
altpodsVersion = "1.11.0-SNAPSHOT"
boardGameVersion = '0.2.187'
}
repositories {
mavenLocal()
['boardgame', 'libgdx-wheelergames'].each {
maven {
credentials {
username "wheelergames"
password System.env.WHEELER_GAMES_PACKAGES_TOKEN
}
url "https://maven.pkg.github.com/wheelergames/${it}"
}
}
mavenCentral()
google()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
}
project(":core") {
apply plugin: "java-library"
dependencies {
api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
implementation "uk.co.wheelergames:boardgame:$boardGameVersion"
implementation 'com.google.code.gson:gson:2.8.6'
}
apply plugin: "io.objectbox" // Apply last.
}
and this is how I publish it to github packages
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
id 'maven-publish'
}
repositories {
mavenLocal()
['libgdx-wheelergames'].each {
maven {
credentials {
username "wheelergames"
password System.env.WHEELER_GAMES_PACKAGES_TOKEN
}
url "https://maven.pkg.github.com/wheelergames/${it}"
}
}
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
google()
}
ext {
gdxVersion = '1.10.1-wheelergames-SNAPSHOT'
}
dependencies {
api "com.badlogicgames.gdx:gdx:$gdxVersion"
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:29.0-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.13'
}
publishing {
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/wheelergames/boardgame")
credentials {
username = "wheelergames"
password = System.getenv("BOARDGAME_PUBLISH_PASSWORD") // stored in /etc/environment
}
}
}
publications {
gpr(MavenPublication) {
groupId 'uk.co.wheelergames'
artifactId 'boardgame'
version '0.2.187'
from(components.java)
}
}
}
This is the list of files in my .gradle dir locally
~/.gradle/caches/modules-2/files-2.1$ find . | grep "wheelergames"
./uk.co.wheelergames
./uk.co.wheelergames/boardgame
./uk.co.wheelergames/boardgame/0.2.187
./uk.co.wheelergames/boardgame/0.2.187/23d5f8797381cc3141371fe5dd754d8ea409e9d7
./uk.co.wheelergames/boardgame/0.2.187/23d5f8797381cc3141371fe5dd754d8ea409e9d7/boardgame-0.2.187.pom
./uk.co.wheelergames/boardgame/0.2.187/9e228e30227d23d0a5f715316cc12d2a395e651f
./uk.co.wheelergames/boardgame/0.2.187/9e228e30227d23d0a5f715316cc12d2a395e651f/boardgame-0.2.187.jar
./uk.co.wheelergames/boardgame/0.2.187/84d81ce69a68fd850fcd6713bacd2a7906441856
./uk.co.wheelergames/boardgame/0.2.187/84d81ce69a68fd850fcd6713bacd2a7906441856/boardgame-0.2.187.module
The error when I remove the repo, even when it's already stored locally
* What went wrong:
Execution failed for task ':core:compileJava'.
> Could not resolve all files for configuration ':core:compileClasspath'.
> Could not find uk.co.wheelergames:boardgame:0.2.187.
Searched in the following locations:
- file:/home/wheeler/.m2/repository/uk/co/wheelergames/boardgame/0.2.187/boardgame-0.2.187.pom
- https://repo.maven.apache.org/maven2/uk/co/wheelergames/boardgame/0.2.187/boardgame-0.2.187.pom
- https://dl.google.com/dl/android/maven2/uk/co/wheelergames/boardgame/0.2.187/boardgame-0.2.187.pom
- https://oss.sonatype.org/content/repositories/snapshots/uk/co/wheelergames/boardgame/0.2.187/boardgame-0.2.187.pom
- https://oss.sonatype.org/content/repositories/releases/uk/co/wheelergames/boardgame/0.2.187/boardgame-0.2.187.pom
Required by:
project :core
I am trying to write a React Native version of this module https://github.com/paypal/PayPal-Cordova-Plugin
It is deprecated, but until we can transition to Braintree, we need the functionality for an existing app.
It has a dependency:
repositories{
mavenCentral()
}
dependencies {
compile('com.paypal.sdk:paypal-android-sdk:2.16.0') {
exclude group: 'io.card'
}
}
I would think that I should add this to the build.gradle file for the React Native app, but when I go there, I see this:
dependencies {
classpath("com.android.tools.build:gradle:3.4.1")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
So it seems like I should have a build.gradle file in my module directory, which I do - the same as the one above (com.paypal.sdk...)
However, it doesn't seem to be registering.
Is there a way to indicate to download this dependency? I know in iOS there is pod install - is there an equivalent method in Android?
How do I install this dependency?
EDIT: My newest plugin build.gradle is this, and it still throws an error due to the library being missing:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.paypal.sdk:paypal-android-sdk:2.16.0'
compile 'com.facebook.react:react-native:+'
}
In order to not manually manage, both, Maven and Gradle build configuration files, I wanted to let Gradle generate the Maven POMs for a multi-module build.
This is working so far. Below you find the settings.gradle
rootProject.name = 'parent'
include 'module-a'
include 'module-b'
and here follows build.gradle
allprojects {
apply plugin: 'maven'
group = 'com.example.project'
version = '1.0-SNAPSHOT'
}
subprojects {
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
task createPom << {
pom {
project {
parent {
groupId project.group
artifactId rootProject.name
version project.version
}
}
}.writeTo("pom.xml")
}
}
task createPom << {
pom {
project {
packaging 'pom'
modules {
module 'module-a'
module 'modula-b'
}
}
}.writeTo("pom.xml")
}
The problem is that I have to manually declare the modules in the createPom task of the root project. Also, I need two dedicated tasks; one for the root project and and for the subprojects.
How can I let Gradle figure out what the modules are? Or is there a way to programmatically determine and add the subprojects as modules? Furthermore, is it even necessary to have two distinct tasks?
I am working on android (Java) and trying to consume websockets so I thought I would use this tutorial and they are using dependency org.java-websocket:Java-WebSocket:1.3.0 from this repo which has now become 1.3.1.
So I have in my modular build.gradle
dependencies {
...
compile 'org.java-websocket:Java-WebSocket:1.3.1'
...
}
and in my project / top level build.gradle I have
repositories {
jcenter()
maven { url 'http://clojars.org/repo' }
}
and I am getting error
Error:(49, 13) Failed to resolve: org.java-websocket:Java-WebSocket:1.3.1
Try it with lowercase:
compile 'org.java-websocket:java-websocket:1.3.1'
It worked for me.
Edit:
the project level gradle file:
buildscript {
repositories {
jcenter()
maven { url 'http://clojars.org/repo' }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
}
}
allprojects {
repositories {
jcenter()
maven { url 'http://clojars.org/repo' }
}
}
I was also facing same issue ,I restarted Android Studio and sync again,it build successfully.
I have investigated a while and probably saw most popular answers here related to aar and transitive dependencies but somehow it is still not clear for me how to make this working.
So:
I have android library with given gradle config:
apply plugin: 'android-library'
apply plugin: 'android-maven'
version = "1.0.0"
group = "com.somepackage"
buildscript {
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
classpath 'com.github.dcendents:android-maven-plugin:1.0'
}
}
android {
compileSdkVersion 19
buildToolsVersion '19.0.3'
defaultConfig {
minSdkVersion 10
}
}
repositories {
maven { url 'http://www.bugsense.com/gradle/' }
}
dependencies {
provided 'com.google.android.gms:play-services:+'
provided 'com.android.support:appcompat-v7:+'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.bugsense.trace:bugsense:3.6'
compile 'commons-net:commons-net:3.3'
}
Then I am deploying it to local maven repo with gradle install. POM file of the deployed library looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sprezzat</groupId>
<artifactId>app</artifactId>
<version>1.0.0</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
<groupId>com.bugsense.trace</groupId>
<artifactId>bugsense</artifactId>
<version>3.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
And finally gradle config of my android application using above library as a dependency:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
mavenLocal()
}
android {
compileSdkVersion 15
buildToolsVersion "19.0.2"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
}
dependencies {
compile 'com.google.android.gms:play-services:+'
compile 'com.android.support:appcompat-v7:+'
compile 'com.somepackage:LIBRARY_NAME:1.0.0#aar'
}
And after deploying application on phone I am getting NoClassDefFoundError for classes belonging to compile dependencies of my android library.
Inspecting my android application dependencies using gradle dependencies:
apk - Classpath packaged with the compiled main classes.
+--- com.google.android.gms:play-services:+ -> 4.3.23
| \--- com.android.support:support-v4:19.0.1 -> 19.1.0
+--- com.android.support:appcompat-v7:+ -> 19.1.0
| \--- com.android.support:support-v4:19.1.0
\--- com.somepackage:LIBRARY_NAME:1.0.0
According to above tree, all transitive dependencies are not detected. Where is the problem and how should it be done correctly?
I have solved my problem by setting transitive attribute for my aar dependency:
compile ('com.somepackage:LIBRARY_NAME:1.0.0#aar'){
transitive=true
}
you should not use "#aar", if use "#" is become "Artifact only notation", if you want to use "#" and want have dependence transitive, you should add "transitive=true"
Try this if you are using aar locally:
compile(project(:your-library-name)) {
transitive=true
}
I was having a similar problem and felt I could share the steps of solving the problem.
The basic idea of not being able to use the transitive dependencies while you are publishing your own aar is actually not having the .pom file generated with the expected transitive dependencies.
I was using 'maven-publish' plugin for my android aar dependency to publish it in my own private maven repository. The transitive dependencies were not resolved when my other projects were adding my aar dependency in their build.gradle. Hence here what I did to modify the .pom file while publishing my aar.
An important thing to note here that, the dependencies which you want to have the transitive behavior should be imported using the api in your library project's build.gradle file like the following.
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
api 'com.android.volley:volley:1.0.0'
api "com.google.code.gson:gson:$globalGsonVersion"
}
Now as I said earlier, I was using maven-publish plugin to publish the aar dependency and hence my publishing task in the gradle looks like the following.
publishing {
publications {
mavenAar(MavenPublication) {
from components.android
}
mavenJava(MavenPublication) {
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
// Iterate over the api dependencies (we don't want the test ones), adding a <dependency> node for each
configurations.api.allDependencies.each {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
repositories {
maven {
// Your repository information goes here
}
}
}
Hence, I used another mavenJava task to publish the .pom file in my private maven repo so that when the aar file is added as a dependency to some other module, it gets the .pom information and download the transitive dependency.
To complete the answer, this is how you should add the dependency in the build.gradle file for your own published aar to me imported.
api('com.example.masudias:my_lib:1.0.0#aar') {
transitive = true
}
Transitive dependency
transitive means that the consumer(e.g. app) includes a producer and all producer's dependencies(e.g. libraries). It increase build time and can create some issues with dependency versions
By default, Gradle dependency has transitive = true
api ('com.package:library:0.0.1')
//the same
api ('com.package:library:0.0.1') {
transitive = true
}
When you use #artifact notation it has transitive = false
api ('com.package:library:0.0.1#aar')
//the same
api ('com.package:library:0.0.1#aar') {
transitive = false
}
For me complete publishing solution looks like this:
apply plugin: 'com.github.dcendents.android-maven'
group = GROUP
version = VERSION
// you could move it to env variable or property
def publishFlavorless = true
def firstTask = null
android.libraryVariants.all { variant ->
if (variant.name.toLowerCase().contains("debug")) {
// Workaround for https://github.com/gradle/gradle/issues/1487
if (publishFlavorless && firstTask == null) {
def bundleTask = tasks["bundle${variant.name.capitalize()}Aar"]
firstTask = bundleTask
artifacts {
archives(firstTask.archivePath) {
builtBy firstTask
name = project.name
}
}
}
return
}
def bundleTask = tasks["bundle${variant.name.capitalize()}Aar"]
artifacts {
archives(bundleTask.archivePath) {
classifier variant.flavorName
builtBy bundleTask
name = project.name
}
}
}
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom.project {
name POM_NAME
artifactId POM_ARTIFACT_ID
// For aar it is equal to 'aar' with jar transitive dependencies won't work
packaging POM_PACKAGING
description POM_DESCRIPTION
}
}
}
The transitive = true block is required as well ...
AAR file doesn't contain transitive dependencies. So even if use api instead of implementation it wont work.
In our team we had developed a library to use in our applications and we wanted it to be for internal use only. Earlier we used to include whole module that seems to work. Later we decided to move to aar file but we were also facing same issue of classpath not found. After some research we came to know that we can also use local maven repo. We decided to use that.
Here is step by step process
Publishing repo
1.In your Library's root build.gradle file you need to include
id 'com.github.dcendents.android-maven' version '2.0' apply false
2.In you library's module level build.gradle file you need to add
a) In plugins
id 'maven-publish'
b) At bottom of gradle file add
publishing {
publications {
release(MavenPublication) {
groupId = 'com.demo.android'
artifactId = 'qrcodescanner'
version = '1.0.0'
afterEvaluate {
from components.release
}
}
}
repositories {
maven {
name = "qrcodescanner"
url = "${project.buildDir}/repo"
}
}
}
3.Depends on the name you have given a gradle tasks will be generated you can check using gradlew tasks or by using gradle window at top right corner in android studio, Our was
publishReleasePublicationToQrcodescannerRepository
4.Run it and repo will be generated in given path
gradlew publishReleasePublicationToQrcodescannerRepository
Final step you need to publish it to maven local using
gradlew publishToMavenLocal
if it doesn't work you can try gradlew clean and gradlew build before executing it.
Using local repo
1.You need to add mavenLocal() in root level build.gradle file of your project you wanted to use it in like this before other central repos
allprojects {
repositories {
mavenLocal()
google()
jcenter()
maven { url "https://jitpack.io" }
mavenCentral()
}
}
Now you need to include your dependency in the project like we do with other dependencies as well
implementation 'com.demo.android:qrcodescanner:1.0.0'
keep in mind format should groupdId:artifactId:version
That's it.
References :
Publish Library
Local Maven
Simply adding #aar at the end of the dependency is what worked for me.
dependencies {
implementation 'org.videolan.vlc:libvlc:3.0.13#aar'
}