In the build file for the library you can tailor the source sets to your needs. In general you write something like this:
apply plugin: 'java'
sourceSets {
main {
java {
exclude 'some/unwanted/package/**'
}
}
}
I'm assuming this is a plain Java library. If it's an Android library, the android-library plugin also supports exclude
syntax in source sets.
Here's a SO question for reference:
Android Studio Exclude Class from build?
You can also read the Gradle docs for source sets at http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.SourceSet.html#org.gradle.api.tasks.SourceSet:java(groovy.lang.Closure) and the Java plugin at http://www.gradle.org/docs/current/userguide/java_plugin.html