Jon
Jon

Reputation: 9833

How Can I Include Only Certain Source Files In Gradle

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 exclude source files and added this to build.gradle:

sourceSets {
    main {
        java {
            //needed until we integrate these classes
            exclude 'org/odk/collect/android/**'
        }
    }
}

Now I'm ready to start using some of these classes. What I'd like to do is specify individual files which should be included. Unfortunately the include statements don't seem to have any effect as gradle will still report that a certain package doesn't exist.

In fact it doesn't seem as though include does anything. I would have liked it to have the opposite effect of exclude. So that, if I change my config to only specify which files to include, only those are included. Sadly, this is not the case.

I've also tried being more explicite with my exclude'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:

EDIT: this has a bug in it, I didn't intend for exclude 'org/odk/collect/android/logic/**' to be in there. I also didn't need the includes while excluding explicitly. I shall post what finally worked in an solution.

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/**'
        }
    }
}

Now, I know you might wonder, 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? And the reason is that I want to keep the git history of these files intact so that features like blame and annotate properly attribute the code lines to their original author.

Some other configurations which didn't work:

includes first, then exclude

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/**'

example errors:

package org.odk.collect.android.logic does not exist
package org.odk.collect.android.exception does not exist

exclude first then includes

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/**'

example errors:

package org.odk.collect.android.logic does not exist
package org.odk.collect.android.exception does not exist

Upvotes: 1

Views: 3293

Answers (2)

addevp
addevp

Reputation: 41

Just leaving this here for someone who might come along later. This answer shows how you can use HashSet to include just the files you need. So for instance, if you just wanted to include FileA.java and FileB.java and exclude everything else, you can use the following configuration:

sourceSets {
    main {
        java {
            setIncludes(new HashSet(['com/somepackage/FileA.java',
                                     'com/somepackage/FileB.java']))          
        }
    }
}

Upvotes: 4

Jon
Jon

Reputation: 9833

Having an exclude for every class not included works. I haven't had any luck with includes. I would have preferred specifying only the files to include so if anyone knows how to do that, post another solution. I left my include statements there, but commented so it is more obvious which files I'm not excluding.

This is a quite verbose way of doing things. And I don't doubt there is a better way out there.

    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/F*'
//                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/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/Ar*'
//                include 'org/odk/collect/android/views/AudioButton'
                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/A*'
                exclude 'org/odk/collect/android/widgets/B*'
                exclude 'org/odk/collect/android/widgets/D*'
                exclude 'org/odk/collect/android/widgets/E*'
                exclude 'org/odk/collect/android/widgets/G*'
                exclude 'org/odk/collect/android/widgets/I*'
                exclude 'org/odk/collect/android/widgets/L*'
                exclude 'org/odk/collect/android/widgets/O*'
//                include 'org/odk/collect/android/widgets/QuestionWidget'
                exclude 'org/odk/collect/android/widgets/SelectMulti*'
                exclude 'org/odk/collect/android/widgets/SelectOneAuto*'
//                include 'org/odk/collect/android/widgets/SelectOneWidget'
                exclude 'org/odk/collect/android/widgets/Si*'
                exclude 'org/odk/collect/android/widgets/Sp*'
                exclude 'org/odk/collect/android/widgets/St*'
                exclude 'org/odk/collect/android/widgets/T*'
                exclude 'org/odk/collect/android/widgets/U*'
                exclude 'org/odk/collect/android/widgets/V*'
//                include 'org/odk/collect/android/widgets/WidgetFactory'
            }
        }
    }

Upvotes: 0

Related Questions