47

I need to use Amazon Maps and Amazon Messaging in my apps.

With gradle, I did not succeed in adding the Amazon dependencies with a "provided" scope as they need to be :

The JAR file contains stub implementations of the Amazon Maps API. It does not contain actual implementations of the Maps API classes, so you should not compile the JAR into your app.

None of the solutions provided by Amazon support worked for me.

If someone succeeded to use amazon maps or amazon messaging with Gradle, please share your build.gradle file here.

Michael Oryl
  • 20,856
  • 14
  • 77
  • 117
Benjamin
  • 2,777
  • 2
  • 16
  • 14

5 Answers5

61

In the 2.12 release of Gradle, compileOnly was added to give similar functionality to provided scope. There is a difference in what happens in the test classpath. Here is relevant quote and snippet from the release notes:

You can now declare dependencies to be used only at compile time in conjunction with the Java plugin. Compile only dependencies are used only during source compilation and are not included in the runtime classpath or exposed to dependent projects. This behavior is similar to that of the 'provided' scope available in Maven based builds. However, unlike Maven provided dependencies, compile only dependencies in Gradle are not included on the test classpath.

Compile only dependencies should be assigned to the relevant source set's 'compileOnly' configuration.

dependencies {
    compileOnly 'javax.servlet:servlet-api:2.5'
}
mkobit
  • 43,979
  • 12
  • 156
  • 150
  • Great answer. Did what I need without all those additional actions and thirdparty plugins. –  Sep 03 '16 at 16:18
  • If you're using version 2.12+ of gradle, yes, this should likely be the accepted answer. If you're stuck with supporting something older (say, 1.12, @MichaelOryl's answer is the one to use. – Meower68 Jul 11 '17 at 17:03
  • 2
    An additional dependency with `testCompile` required for actual `provided` capability. – Jin Kwon Nov 06 '17 at 01:01
  • This compileOnly total crap and won't inherited even by tests, you need to duplicate recored with testCompileOnly. And even more, this testCompileOnly won't be inherited by another module when you will try to use project with transitive dependencies enabled. – m1ld Oct 10 '19 at 09:28
41

The solution that I've been using is pretty simple. You must add the following code to your build.gradle file:

apply plugin: 'eclipse'  // Eclipse users only

configurations {
    provided
}

sourceSets {
    main.compileClasspath += configurations.provided
    test.compileClasspath += configurations.provided
    test.runtimeClasspath += configurations.provided
}

eclipse.classpath.plusConfigurations += configurations.provided  // Eclipse users only

If you are not an Eclipse user (I'm not), you don't actually need the first and last lines, as you might have guessed.

Once the above configuration additions are in, you can then simply add a provided dependency in your dependencies section alongside of any regular compile dependencies:

dependencies {
    compile group: 'org.springframework', name: 'spring-core', version: '3.2.6.RELEASE'

    provided group: 'javax.servlet', name: 'servlet-api', version:'2.5'
    provided group: 'javax.servlet.jsp', name: 'jsp-api', version:'2.1'
}

Hope that helps. It's been working quite well for me for some time.

mkobit
  • 43,979
  • 12
  • 156
  • 150
Michael Oryl
  • 20,856
  • 14
  • 77
  • 117
  • 2
    If you use intellij-idea and `apply plugin: 'idea'` add: `idea { module { scopes.PROVIDED.plus += [configurations.provided] } }` This will force idea to behave right with provided classes. – Danny Dan Jul 16 '15 at 14:41
  • 2
    Build of project depends on in which IDE you work? I thought we should use tools and not they us. Something strange here... – Mrusful May 14 '16 at 17:30
8

There's now a great plugin from Netflix, gradle-extra-configurations-plugin. It provides a provided and optional scope. No more manual plumbing necessary and also generates the required metadata when pubishing e.g to a maven repository.

thokuest
  • 5,800
  • 24
  • 36
4

A little late to the show, using gradle you copy the .jar to the libs folder and in your gradle file you have:

dependencies {
 ....
 provided files('libs/someLibrary')
 ....
}
Calin
  • 6,661
  • 7
  • 49
  • 80
2

There is a prodeps plugin which adds additional optional and provided dependency configurations for Gradle

...
apply plugin: 'propdeps'
...
buildscript {
    repositories {
        maven { url 'http://repo.spring.io/plugins-release' }
    }
    dependencies {
        classpath 'org.springframework.build.gradle:propdeps-plugin:0.0.7'
    }
}
...
dependencies {
    ...
    provided('android:android:2.2_r3')
    ...
}
...
Yaroslav
  • 4,543
  • 5
  • 26
  • 36