android – How to exclude a cpp file in experimental gradle?-ThrowExceptions

Exception or error:

I’m trying to use Android Studio 1.3.1 to compile an NDK project using the experimental gradle syntax.

My build.gradle looks very much like the one from the Teapot example

With the exception that my source tree has some files which I don’t want to include in the build. I can’t remove these files so I need gradle to ignore them.

I tried adding an exclude definition:

android.sources {
    main {
        jni {
            source {
                srcDirs 'src/main/jni'

                excludes += "src/main/jni/FileToExclude.cpp"
            }
        }

    }
}

but that did not affect the outcome. gradle still tries to compile this file.

I tried excludes, exclude with =, += and with nothing at all but no permutation works.

How to solve:

From what I’ve found, the correct directive to exclude the file from the build is exclude, not excludes. Check your build.gradle to make sure you didn’t make a mistake here (you’ve used excludes in the provided sample).

Upd: ok, after some research I found this thread on the AOSP issue tracker. The topic starter says the following:

The java/resources components of the sourcesets allow for include/exclude patterns.

We should do this for aidl/rs/jni/assets.

The issue is still open so I suppose this functionality has to be implemented in the Android Gradle plugin or Android Studio or in both of them (and isn’t implemented yet). It’ll be implemented in Android Studio 1.5, at least this is what the tags are saying.

###

I think you are giving path of the file in wrong manner.
It should be:

srcDir ‘src/main/jni’

exclude ‘FileToExclude.cpp’

You can follow this link.
https://discuss.gradle.org/t/how-can-i-exclude-certain-java-files-from-being-compiled/5287/2

Also note that you should use exclude instead of excludes and srcDir instead of srcDirs.

###

I looked into the android source, it looks like you could update the filter to exclude your file. I don’t know what version of the gradle plugin you are using, so I can’t be sure what the underlying api is. You could try setting the PatternFilterable manually to exclude the file.

android.sources {
    main {
        jni {
            source {
                srcDirs 'src/main/jni'
                getFilter().exclude('**/FileToExclude.cpp')
            }
        }
    }
}

I looked at Gradle Code Review, and saw that LanguageSourceSet was being used. Looking at the Gradle documentation for LanguageSourceSet, which you can access a SourceDirectorySet, which has a PatternFilterable that you can set the exclude on.

###

As it’s still experimental there’s not a much of documentation on Gradle Experimental. But with some experimentation I was able to dig a way on how to exclude some files, i.e.:

model {
    //...

    android.sources {
        //...

        jni {
            //...

            source {
                excludes.add("<FILE PATH>") // You can have multiple excludes.add(...)
            }
        }
    }
}

Note: the solution works on gradle-experimental 0.7.0

###

The easiest way is to set the properties in build.gradle:

sourceSets.main.jni.srcDirs = [] // now, AS will not try to compile your source files
sourceSets.main.jniLibs.srcDirs = ['libs'] // now, AS will pick up the compiled SO files from libs (where ndk-build will normally put them)

You can also define a “buildNative” task, to run ndk-build as part of compileTask, as defined in this answer.

Leave a Reply

Your email address will not be published. Required fields are marked *