22

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.

Gabriel
  • 763
  • 1
  • 10
  • 28
shoosh
  • 76,898
  • 55
  • 205
  • 325
  • With `gradle-experimental:0.3.0`, `exclude` syntax is available, see http://stackoverflow.com/questions/32636150/define-local-src-files-in-ndk-dsl/32640823#32640823 – Alex Cohn Dec 11 '15 at 13:16

5 Answers5

10

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.

Community
  • 1
  • 1
aga
  • 27,954
  • 13
  • 86
  • 121
  • 1
    @shoosh looks like it isn't implemented yet, see the updated answer. – aga Sep 07 '15 at 13:58
  • 2
    Looks you are out of luck. Unless you want to build your own gradle plugin, your best choice would be to use Android.mk via ndk-build. – Alex Cohn Sep 07 '15 at 21:29
2

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.

atul
  • 168
  • 1
  • 14
0

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.

Ethan
  • 6,883
  • 3
  • 33
  • 41
0

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

Toochka
  • 894
  • 1
  • 9
  • 25
-1

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.

Community
  • 1
  • 1
Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79