title: How Can I Include Only Certain Source Files In Gradle
body: <p>I have a bunch of code in an Android Studio Project that was added from another project. When I originally added it, I wasn't ready to start using it so I researched how to <em>exclude</em> source files and added this to <code>build.gradle</code>:</p>
  
  <pre class="lang-groovy prettyprint-override"><code>sourceSets {
      main {
          java {
              //needed until we integrate these classes
              exclude 'org/odk/collect/android/**'
          }
      }
  }
  </code></pre>
  
  <p>Now I'm ready to start using some of these classes. What I'd like to do is specify individual files which should be <em>included</em>. Unfortunately the <a href="https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/util/PatternFilterable.html" rel="nofollow">include</a> statements don't seem to have any effect as gradle will still report that a certain package doesn't exist.</p>
  
  <p>In fact it doesn't seem as though <em>include</em> does anything. I would have liked it to have the opposite effect of <em>exclude</em>. So that, if I change my config to only specify which files to <em>include</em>, only those are included. Sadly, this is not the case.</p>
  
  <p>I've also tried being more explicite with my <code>exclude</code>'s and specifying 20+ packages which should be excluded and the individual classes I want included but it didn't work for me. Here's and example of that:</p>
  
  <p><strong>EDIT: this has a bug in it, I didn't intend for <code>exclude 'org/odk/collect/android/logic/**'</code> to be in there.</strong> I also didn't need the includes while excluding explicitly. I shall post what finally worked in an solution.</p>
  
  <pre class="lang-groovy prettyprint-override"><code>sourceSets {
      main {
          java {
              //needed until we integrate these classes
              exclude 'org/odk/collect/android/activities/**'
              exclude 'org/odk/collect/android/adapters/**'
              exclude 'org/odk/collect/android/application/**'
              exclude 'org/odk/collect/android/database/**'
              include 'org/odk/collect/android/exception/**'
              exclude 'org/odk/collect/android/external/**'
              exclude 'org/odk/collect/android/listeners/**'
              exclude 'org/odk/collect/android/logic/Drive*'
              exclude 'org/odk/collect/android/logic/File*'
              include 'org/odk/collect/android/logic/FormControlle'
              exclude 'org/odk/collect/android/logic/FormDetails*'
              exclude 'org/odk/collect/android/logic/H*'
              exclude 'org/odk/collect/android/logic/P*'
              exclude 'org/odk/collect/android/logic/**'
              exclude 'org/odk/collect/android/picasa/**'
              exclude 'org/odk/collect/android/preferences/**'
              exclude 'org/odk/collect/android/provider/**'
              exclude 'org/odk/collect/android/receivers/**'
              exclude 'org/odk/collect/android/tasks/**'
              exclude 'org/odk/collect/android/utilities/**'
              exclude 'org/odk/collect/android/views/A*'
              exclude 'org/odk/collect/android/views/D*'
              exclude 'org/odk/collect/android/views/E*'
              exclude 'org/odk/collect/android/views/H*'
              include 'org/odk/collect/android/views/MediaLayout'
              include 'org/odk/collect/android/views/ODKView'
              exclude 'org/odk/collect/android/views/T*'
              exclude 'org/odk/collect/android/widgets/**'
          }
      }
  }
  </code></pre>
  
  <p>Now, I know you might wonder, <em>why even have all these files in the project in the first place? Why don't you just delete the unused ones and add them again when you're ready to implement them?</em> And the reason is that I want to keep the git history of these files intact so that features like <code>blame</code> and <code>annotate</code> properly attribute the code lines to their original author.</p>
  
  <h2>Some other configurations which didn't work:</h2>
  
  <h3>includes first, then exclude</h3>
  
  <pre><code>include 'org/odk/collect/android/exception/**'
  include 'org/odk/collect/android/logic/FormController.java'
  include 'org/odk/collect/android/views/A*'
  include 'org/odk/collect/android/views/MediaLayout*'
  include 'org/odk/collect/android/views/ODKView*'
  include 'org/odk/collect/android/widgets/**'
  exclude 'org/odk/collect/android/**'
  </code></pre>
  
  <p>example errors:</p>
  
  <pre><code>package org.odk.collect.android.logic does not exist
  package org.odk.collect.android.exception does not exist
  </code></pre>
  
  <h3>exclude first then includes</h3>
  
  <pre><code>exclude 'org/odk/collect/android/**'
  include 'org/odk/collect/android/exception/**'
  include 'org/odk/collect/android/logic/FormController.java'
  include 'org/odk/collect/android/views/A*'
  include 'org/odk/collect/android/views/MediaLayout*'
  include 'org/odk/collect/android/views/ODKView*'
  include 'org/odk/collect/android/widgets/**'
  </code></pre>
  
  <p>example errors:</p>
  
  <pre><code>package org.odk.collect.android.logic does not exist
  package org.odk.collect.android.exception does not exist
  </code></pre>