Re: Gradle: How to include libs properly?

5,749 views
Skip to first unread message

Szabolcs Berecz

unread,
Nov 27, 2012, 10:18:28 AM11/27/12
to adt...@googlegroups.com
Hi,

You put the "compile fileTree(dir: 'libs', include: '*.jar')" in the wrong section. You need to put it in a toplevel "dependencies" section, because that's what describes the dependencies of your project.
The section "buildscript" describes the configuration of your build script, and that's why the "classpath 'com.android.tools.build:gradle:0.2'" is in this section: you need the gradle android plugin to build your android project.

So, remove the "compile..." from "buildscript.dependencies" and put this after the "android" section:
dependencies {
  compile fileTree(dir: 'libs', include: '*.jar')
}

Regards,
Szabolcs

On Tue, Nov 27, 2012 at 11:09 AM, Stefan Hoth <stefan...@gmail.com> wrote:
Hi there,

after I've seen the introduction at Devoxx I wanted to migrate a current project from Ant to Gradle.

For the easiest choice I picked the "migrated"-example from the samples and adapted a bit.

This is what it looks like now:

build.gradle:

buildscript {
   repositories {
       mavenCentral()
   }
   dependencies {
       classpath 'com.android.tools.build:gradle:0.2'
       compile fileTree(dir: 'libs', include: '*.jar')
   }
   
}

apply plugin: 'java'

tasks.withType(Compile) {
   options.encoding = 'UTF-8'
}

apply plugin: 'android'

android {
target = 'android-16'
   defaultConfig {
       versionCode = 3
       versionName = '1.0.1'
   
   signingStoreLocation = "deployment/debug.keystore"
   signingStorePassword = "android"
   signingKeyAlias = "androiddebugkey"
   signingKeyPassword = "android"
   }
   sourceSets {
       main {
           manifest {
               srcFile 'AndroidManifest.xml'
           }
           java {
               srcDir 'src'
               //exclude 'some/unwanted/package/**'
           }
           res {
               srcDir 'res'
           }
           assets {
               srcDir 'assets'
           }
           resources {
               srcDir 'src'
           }
       }
       test {
           java {
               srcDir 'tests/src'
           }
       }

       // Could also be done with:
       //main.manifest.srcFile 'AndroidManifest.xml'
       //main.java.srcDir 'src'
       //main.res.srcDir 'res'
       //main.assets.srcDir 'assets'
       //main.resources.srcDir 'src'
       //test.java.srcDir 'tests/src'
   }

buildTypes {
       debug {
           packageNameSuffix = ".debug"
       }
       staging {
           packageNameSuffix = ".staging"
           debuggable = true
           debugSigned = true
           zipAlign = false
       }
   }    
}

My current problem is that I never got it to work properly because I can't find the examples on how to include 

a) either the jars in ./libs/ to be used 
or
b) the maven dependencies for the libs.

List of used libs:

GoogleAnalytics-1.5.1.jar
android-support-v4.jar
android-support-v7-gridlayout.jar
androlog-1.0.5-sources.jar
androlog-1.0.5.jar
gson-2.2.2-sources.jar
gson-2.2.2.jar
zxing-core-2.0-javadoc.jar
zxing-core-2.0.jar

With the current setup of the file above I get the following error:

Compiling build file './build.gradle' using BuildScriptClasspathScriptTransformer.

FAILURE: Build failed with an exception.

* Where:
Build file './build.gradle' line: 8

* What went wrong:
A problem occurred evaluating root project 'gradletest'.
> No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.compile() is applicable for argument types: (org.gradle.api.internal.file.collections.DefaultConfigurableFileTree) values: [directory 'libs']
Possible solutions: module(java.lang.Object) 


I'm just not familiar at all with gradle so I'm pretty much helpless. I'd apriciate if anyone could point me to suggestion on how to get a successful build from where I can explore more options.

Thanks,
Stefan


Xavier Ducrohet

unread,
Nov 27, 2012, 4:50:18 PM11/27/12
to adt...@googlegroups.com
On top of this you should not apply the Java plugin. It would create additional tasks that could create problem (particularly if you start putting tests under src/test/java and run "gradle check")

You can still put 
    tasks.withType(Compile) {
        options.encoding = 'UTF-8'
  }

after you apply the android plugin.
--
Xavier Ducrohet
Android SDK Tech Lead
Google Inc.
http://developer.android.com | http://tools.android.com

Please do not send me questions directly. Thanks!

Stefan Hoth

unread,
Dec 2, 2012, 3:56:20 PM12/2/12
to adt...@googlegroups.com
Thank you both for the pointers, it really helped understand the syntax better.

I now face a different problem: The compiler doesn't know any class from the Android SDK (neither base nor Google APIs) so the build fails yet again. 

Do I need to use the Maven SDK installer to provide those libs and include them as dependencies?
Or did I miss anything else?

Thanks
- Stefan

PS: Working on a Windows machine if that is of importance.

Xavier Ducrohet

unread,
Dec 3, 2012, 12:11:49 PM12/3/12
to adt...@googlegroups.com
You don't need the Maven SDK. The Gradle plugin will find android.jar from your Android SDK directly.

Do you only have a compilation error? Nothing else? that's strange since it means it found the Android SDK, found the API 16 but then didn't use it? I'm not sure what would trigger this.

Stefan Hoth

unread,
Dec 3, 2012, 1:32:56 PM12/3/12
to adt...@googlegroups.com
This is the output I'm getting:

C:\coding\gradletest>gradle clean build
:clean
:prepareDebugDependencies
:compileDebugAidl
Ignoring platform 'android-7': build.prop is missing.
:generateDebugBuildConfig
:crunchDebugRes
ERROR: 9-patch image C:\coding\gradletest\res\drawable-hdpi\fade_black_to_transparent.9.png malformed.
       No marked region found along edge.
       Found along left edge.
:processDebugManifest
:processDebugRes
:compileDebug
C:\coding\gradletest\src\de\wall\bluespot\activities\SpotMapActivity.java:22: package com.google.android.maps does not exist
import com.google.android.maps.*;
^
C:\coding\gradletest\src\de\wall\bluespot\activities\SpotMapActivity.java:37: cannot find symbol
symbol: class MapActivity
public class SpotMapActivity extends MapActivity {

[...]

100 errors
:compileDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileDebug'.
> Compilation failed; see the compiler error output for details.


I can't spot the error. Here's my current build.gradle again to make sure I did everything right: https://gist.github.com/4196894

Thanks for helping out.

- Stefan

Xavier Ducrohet

unread,
Dec 3, 2012, 1:50:56 PM12/3/12
to adt...@googlegroups.com
You are using the maps API, meaning you should be compiling against the proper target, but your gradle file says:

android {
  target = 'android-16'
}

This value is the same as what was in project.properties. Based on the rest of your build.gradle it almost looked like you copied it from a sample without really understanding what was in there (do you really need a 'staging' build type?)

Anyway, just put what you would have had in your project.properties in the android.target property.
Probably something like "Google Inc.:Google APIs:16"

Stefan Hoth

unread,
Dec 5, 2012, 11:54:36 AM12/5/12
to adt...@googlegroups.com
Xavier,

thank you for your kind help. I finally got around to build my project with gradle.

And your impression was right: As I said in my first mail I am indeed a newbie in gradle so I took a sample and tried to adjust it to my needs. Sorry if I confused or missed things an experienced developer would have catched right away. Certainly trying to learn quickly.

Thanks again,
Stefan
Reply all
Reply to author
Forward
0 new messages