How can I set gradle resolutionStrategy for only the release build? - android

I'm developing an android app where I want the release build to fail if there is a dependency version conflict. If I want to enable it for all builds, I can do this:
configurations.all {
resolutionStrategy {
failOnVersionConflict()
}
}
However I don't want to enable it for debug builds. I can't find any documentation on how to do that. I've tried:
configurations.each {
if (it.name.startsWith("release")) {
it.resolutionStrategy {
failOnVersionConflict()
}
}
}
But it doesn't apply. I've even tried what I thought would be the equivalent of the first example - but this has no effect:
configurations.each {
it.resolutionStrategy {
failOnVersionConflict()
}
}
I'm simply switching .all for .each here, but failOnVersionConflict doesn't apply even though the code is executed.
What am I doing wrong?

It should work in the following way:
apply plugin: 'java'
configurations.compile.resolutionStrategy {
failOnVersionConflict()
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.guava:guava:23.0'
compile 'com.google.guava:guava:22.0'
}
task deps(type: Copy) {
from configurations.compile
into('deps')
}
When you run gradle deps then it should fail with version conflict. If you comment out configuration block guava version 23.0 will be copied.

Related

Nested aar dependencies not working

To simplify things I'm going to call the libraries I'm using library_a.aar and library_b.aar.
Here is the scenario that I'm facing.
library_a is build and pushed to maven_repository and no problems here.
library_b depends on library_a and added to library_b as follows:
repositories {
maven {
credentials {
username USERNAME
password PASSWORD
}
url "https://api.bitbucket.org/1.0/repositories/COMPANY/maven_repository/raw/releases"
}
}
dependencies {
...
compile 'package:library_a:1.0'
...
}
library_b is built with no errors and uploaded to the maven_repository.
Now my application depends on library_b which I need to add by providing (as above) the repository along with the credentials.
The first issue that I'm facing is that in order to compile library_b in my project it needs to be compiled in the following way:
dependencies {
...
compile 'package:library_b:1.0#aar'
...
}
I have to add the #aar otherwise gradle won't recognize it, but I didn't have to do that with library_a.
The second issue is that when I build the app I get warnings that it can't find references to classes available in library_a. What am I missing over here? I did try to add transitive=true to library_a
dependencies {
...
compile ('package:library_a:1.0') {
transitive = true;
}
...
}
but absolutely nothing works. I did check the pom file and it includes the proper dependencies.
Could it be that what I'm doing is not supported by gradle and I have to compile both library_a and library_b in my app?
Sounds like your maven repo only contains the aar files but no or wrong pom files.
Are you using the following gradle plugin and the uploadArchives gradle task in order to upload the aars to your maven repository?
https://github.com/dcendents/android-maven-gradle-plugin
try this gradle snippet for your aars:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
}
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
android { ... }
dependencies { ... }
uploadArchives {
repositories {
mavenDeployer {
repository(url: "https://api.bitbucket.org/1.0/repositories/COMPANY/maven_repository/raw/releases") {
authentication(userName: USERNAME, password: PASSWORD)
}
}
}
}
Then use the uploadArchives gradle task

Gradle build - Could not determine the dependencies of task ':myLibrary:transformClassesAndResourcesWithProguardForRelease'

I have just migrated my project to gradle-experimental:0.4.0 to use JNI. I have followed the instructions here
The project consists of a library and an application. I cannot get round this error (tried the usual clean and invalidate cache/restart):
Could not determine the dependencies of task ':myLibrary:transformClassesAndResourcesWithProguardForRelease'
The library build fine but this error appears when I build the app module. Here are my modified build.gradle scripts:
Project:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle-experimental:0.4.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Library (build OK):
apply plugin: 'com.android.model.library'
model
{
android {
buildToolsVersion="23.0.1"
defaultConfig.with {
minSdkVersion.apiLevel=16
targetSdkVersion.apiLevel=16
testInstrumentationRunner="android.test.InstrumentationTestRunner"
}
}
android.buildTypes {
release {
minifyEnabled=true
proguardFiles.add(file('proguard.cfg'))
}
}
}
App (this build fails):
apply plugin: 'com.android.model.application'
dependencies {
compile files('libs/GoogleAdMobAdsSdk-4.1.1.jar')
compile project(':myLibrary')
}
model
{
android
{
compileSdkVersion='Google Inc.:Google APIs:16'
buildToolsVersion="23.0.1"
defaultConfig.with {
applicationId="my.app.ID"
minSdkVersion.apiLevel=16
targetSdkVersion.apiLevel=16
}
}
android.buildTypes {
release {
minifyEnabled=true
proguardFiles.add(file('proguard.cfg'))
}
}
}
Has anyone seen this error when moving to gradle-experimental?
Ok so removing the proguard line from the library build file fixed this:
--proguardFiles.add(file('proguard.cfg'))

Gradle script, how to execute a task (file copy) right after compilation and deploy

The following is my backend build.gradle for an android app.
After build and deploy, I would like to copy a few hibernate files to folders under build/exploded-app/. Instead of running gradlew cpyHibernate from terminal, how can I execute the cpyHibernate task from the script right after the deploy?
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.google.appengine:gradle-appengine-plugin:1.9.18'
}
}
repositories {
jcenter();
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'appengine'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
dependencies {
appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.18'
compile 'com.google.appengine:appengine-endpoints:1.9.18'
compile 'com.google.appengine:appengine-endpoints-deps:1.9.18'
compile 'com.googlecode.objectify:objectify:5.1.5'
compile 'javax.servlet:servlet-api:2.5'
compile 'hibernate:hibernate:3.1rc2'
compile 'mysql:mysql-connector-java:5.1.37'
}
appengine {
downloadSdk = true
appcfg {
oauth2 = true
}
endpoints {
getClientLibsOnBuild = true
getDiscoveryDocsOnBuild = true
}
}
task cpyHibernate(type: Copy) {
from 'src/main/java/hibernate.cfg.xml'
into 'build/exploded-app/WEB-INF/classes/com/example/myapplication/backend/hbm/'
}
Thanks
You can make some task finalizing another, in that case this task will run only if another one was called, right after it. You can read about it in the official user guide. This could be done as:
task cpyHibernate(type: Copy) {
doFirst {
//check anothe task status and skip this one if it didn't actually work
if (!tasks.getByName("deploy").getState().didWork) {
throw new StopExecutionException();
}
}
from 'src/main/java/hibernate.cfg.xml'
into 'build/exploded-app/WEB-INF/classes/com/example/myapplication/backend/hbm/'
}
deploy.finalizedBy cpyHibernate
You just have to know the deploy task name. In the example above, a supposed, that it's name is deploy. Note, your cpyHibernate is modified, to check, whether deploy task actally did some work and is not up-to-date. If you don't need it, just remove the doFirst section from it.
You can add build and deploy tasks as dependencies for cpyHibernate task.
After declaring task:
cpyHibernate { ... }
cpyHibernate.dependsOn build, deploy
Or in one place:
task cpyHibernate(type: Copy, dependsOn: [build, deploy]) {
from 'src/main/java/hibernate.cfg.xml'
into 'build/exploded-app/WEB-INF/classes/com/example/myapplication/backend/hbm/'
}

Android Scala and Gradle

I am new to Gradle. Is there some example how to configure properly gradle-android-plugin for scala classes.
this is what I have now.
buildscript {
repositories { mavenCentral() }
dependencies { classpath 'org.gradle.api.plugins:gradle-android-plugin:1.2.1' }
}
apply plugin: 'android'
apply plugin: 'eclipse'
apply plugin: 'scala'
sourceCompatibility = 1.6
version = "1.0.0"
repositories { mavenCentral() }
dependencies {
compile files('/home/pcu/workspace/workspace-android/emoo/libs/android-support-v4.jar')
compile 'org.scala-lang:scala-library:2.9.1'
scalaTools 'org.scala-lang:scala-compiler:2.9.1'
scalaTools 'org.scala-lang:scala-library:2.9.1'
}
task configureDebug << { jar.classifier = "debug" }
task configureRelease << { proguard.enabled = true }
but compilation fails. Scala class is not compiled.
It is actually much simpler with the right plugin:
https://github.com/saturday06/gradle-android-scala-plugin
This worked perfectly fine for me.
You need only about 3 more lines in your gradle configuration and potentially proguard to reduce the apk size.
The setup is well documented on the github page. Everything beyond steps 1-3 is optional.

Building android project with gradle

Im trying to build a simple android app using gradle build tools. but im getting an error like this
No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.compile() is applicable for argument types: (java.lang.String) values: [org.gradle.api.plugins:gradle-android-plugin:1.2.0-SNAPSHOT]
Possible solutions: module(java.lang.Object)
ang here's a simple configuration of build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
compile 'org.gradle.api.plugins:gradle-android-plugin:1.2.0-SNAPSHOT'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
processResource {
expand (project.properties)
}
task configureDebug << {
jar.classifier = "debug"
}
task configureRelease << {
proguard.enabled = true
}
When applying a plugin you want tell you build script to use it in its classpath. It is not required for compilation so just change the configuration compile to classpath. More more information see 51.5.1. Using your plugin in another project in the Gradle user guide.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.gradle.api.plugins:gradle-android-plugin:1.2.0-SNAPSHOT'
}
}
EDIT: At the moment the plugin does not support r20 of the Android SDK. For more information see this issue.
Make sure you are writing the dependency block on your application build.gradle "YourProjectName->yourprojectname->build.gradle" in android studio hierarchy .
Use android gradle tools
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}

Categories

Resources