Common tips about Gradle - part 2

In my previous post I wrote some common gradle tips.

In this post I will continue to collect other common (and basic) cases.

I would like to use the Eclipse folders structure in Android Studio. How to achieve that?
In your build.gradle you can set this script:
android {
      sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
}

Where should I create the libs folder for my jars file?
You can use this structure:
- root
  - project
      libs
         myjar.jar
      src
         main
           java
           res
      build.gradle
In build.gradle:
    dependencies {
       compile files('libs/myjar.jar')
    }

Where should I create the aidl folder?
This is the defualt structure:
- root
  - project
      src
         main
           aidl
           java
           res
      build.gradle
You can use your custom structure. In this case you have to configure your build.gradle:
android {
      sourceSets {
        main {
            aidl.srcDirs = [myFolder]
        }
    }
}

How to exclude a dependency/module when I am importing a library
The real case is the Crouton library (great lib!) by Benjamin Weiss.
   compile ('de.keyboardsurfer.android.widget:crouton:1.8.1') {
        exclude module: 'support-v4'
    }

I've just modified my build.gradle file. But it doesn't work
First of all, click "Syncing Android Studio project with Gradle files" button in your toolbar.

What is gradle.properties?
You can set your variables into a gradle.properties file.
You can provides your gradle.properties in these folder:

- located in project build dir.
- located in gradle user home.

Also there is a third way to declare variables:
from system properties, e.g. when -Dsome.property is used in the command line.

Pay attention: in case an option is configured in multiple locations the last one wins.

Can I set same values in BuildConfig.java with Gradle?
Yes, you can.
Just add this to your build.gradle:
 android {
    defaultConfig {
        buildConfig "private final static boolean DEFAULT = true;", \
                    "private final static String FOO = \"foo\";"
    }
 }

Are you looking for a more Useful BuildConfig, or a better way to Decompose Version Name and Code?
Then you are looking for this great tips by Jake Wharton


Comments

Popular posts from this blog

AntiPattern: freezing a UI with Broadcast Receiver

NotificationListenerService and kitkat

How to centralize the support libraries dependencies in gradle