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

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.

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.

Related

How to exclude a Kotlin file? [duplicate]

With Java, we are excluding like this:
java {
srcDir 'src'
exclude '**/myTests/**'
}
I want to make the same thing with Kotlin.
I am trying to find some documentation on this in official documentation configuring Kotlin, but without any success.
What I've expected and already tried (and of course without any success):
kotlin {
srcDir 'src'
exclude '**/myTests/*.kt'
}
java {
srcDir 'src'
exclude '**/myTests/*.kt'
}
There isn't any Kotlin related configuration.
Why I am saying this: I have all the Kotlin files into the kotlin directory and Java files into java directory. But while configuring, I have added:
sourceSets {
main.java.srcDirs += "src/main/kotlin"
}
This means that with src/main/java, add source files from src/main/kotlin also while compiling.
This should solve your issue.
Android (likely need improvement, seems flaky):
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile.class).configureEach {
it.exclude('**/TestExcludeKotlinClass.kt')
}
If you use a Kotlin Gradle project, try this:
tasks.withType<KotlinCompile> {
exclude("**/packageToExlude/**")}
In my case, a non-Android project.
I came across a way to make this work specifically for Android unit tests (but I'm assuming it's adaptable) using a combination of other solutions here:
def filesToExclude = [
'**/*TestOne*.kt',
'**/*TestTwo*.kt',
...
]
tasks.withType(org.gradle.api.tasks.SourceTask.class).configureEach {
it.exclude(filesToExclude)
}
android.sourceSets.test.kotlin.exclude(filesToExclude)
In my particular case, the extra wildcards around the test name were needed due to other generation occurring (specifically, Dagger with kapt).
This seems to be a bit hacky way to approach it, but it works by ensuring the test target is excluded from all tasks that it could actually be excluded from (including both build & kapt tasks). The sourceSets exclusion is still necessary for the file not to be picked up for compilation (I think this is the Kotlin Gradle Plugin doing it, but it might also be the Android Gradle Plugin).

How to exclude Kotlin files from compiling with Gradle

With Java, we are excluding like this:
java {
srcDir 'src'
exclude '**/myTests/**'
}
I want to make the same thing with Kotlin.
I am trying to find some documentation on this in official documentation configuring Kotlin, but without any success.
What I've expected and already tried (and of course without any success):
kotlin {
srcDir 'src'
exclude '**/myTests/*.kt'
}
java {
srcDir 'src'
exclude '**/myTests/*.kt'
}
There isn't any Kotlin related configuration.
Why I am saying this: I have all the Kotlin files into the kotlin directory and Java files into java directory. But while configuring, I have added:
sourceSets {
main.java.srcDirs += "src/main/kotlin"
}
This means that with src/main/java, add source files from src/main/kotlin also while compiling.
This should solve your issue.
Android (likely need improvement, seems flaky):
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile.class).configureEach {
it.exclude('**/TestExcludeKotlinClass.kt')
}
If you use a Kotlin Gradle project, try this:
tasks.withType<KotlinCompile> {
exclude("**/packageToExlude/**")}
In my case, a non-Android project.
I came across a way to make this work specifically for Android unit tests (but I'm assuming it's adaptable) using a combination of other solutions here:
def filesToExclude = [
'**/*TestOne*.kt',
'**/*TestTwo*.kt',
...
]
tasks.withType(org.gradle.api.tasks.SourceTask.class).configureEach {
it.exclude(filesToExclude)
}
android.sourceSets.test.kotlin.exclude(filesToExclude)
In my particular case, the extra wildcards around the test name were needed due to other generation occurring (specifically, Dagger with kapt).
This seems to be a bit hacky way to approach it, but it works by ensuring the test target is excluded from all tasks that it could actually be excluded from (including both build & kapt tasks). The sourceSets exclusion is still necessary for the file not to be picked up for compilation (I think this is the Kotlin Gradle Plugin doing it, but it might also be the Android Gradle Plugin).

gradle: modify build of library from project

I have a simple question. Is it possible to modify the gradle library build project specific and how?
Following example, I have a library that I use in multiple projects.
Now I want to exclude some files in the library when I build Project_A.
But I want them included when I build Project_B.
I wonder if its possible to add parameters to the dependencies in the build.gradle file of Projekt_A. Something like:
dependencies {
compile project(':explore_layout',
//specify the exclude inside the lib
sourceSets {
main {
java {
exclude '**/uncompilable/**'
}
}
}
)
}
Make two source sets in your library project and then depend on the the according configurations that are added for the source sets. This way you can get only one source set for A but both for B.

Exclude native library in Gradle Android build

Contrary to many other posts on this topic, I want to exclude a native library from an Android build with Gradle.
libfoo.so resides in a library project in the default directory thelib/src/main/jniLibs. In my main project's build.gradle I try to exlude the file as follows:
sourceSets {
all{
jniLibs {
exclude '**/libfoo.so'
}
}
}
This does not work though, the file is still in the final APK. I tried different path specifications already, but none of them work.
Is this even possible, or is there a workaround?
I know this is an old question, i solved my problem with the following
packagingOptions {
exclude 'lib/arm64-v8a/libfoo.so'
}
Hope it helps someones...
Note:
On further searching someone had already solved a similar issue;
Gradle exclude arm64 libs

Exclude aidl files from Javadoc task with Gradle

I'm currently discovering Android Studio and Gradle and migrating all the build chain of my project from bash scripts to Gradle configurations. It's probably going to be awesome in the end, but meanwhile I'm struggling.
What I want to do now is quite simple. I have a standard rule to generate Javadoc from my source (I took this snippet from http://snowdream.github.io/blog/android/2013/11/01/how-to-generate-javadocs-with-android-gradle-plugin/):
android.libraryVariants.all { variant ->
task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
source = variant.javaCompile.source
def androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
classpath = files(variant.javaCompile.classpath.files, androidJar)
options {
links "http://docs.oracle.com/javase/7/docs/api/"
linksOffline "http://d.android.com/reference","${android.sdkDirectory}/docs/reference"
}
exclude '**/BuildConfig.java'
exclude '**/R.java'
}
}
But my project also contains AIDL files and I don't want these aidl files (nor the .java files generated from them) to be included to the Javadoc.
I tried the rule:
exclude "**/$buildDir/**"
... and I tried a thousand others, but none works and my interfaces and Stub are processed into HTML files.
I beg for your help! Thanks a lot.
After hours of research about how Gradle and Groovy work, the best way I found is:
exclude {
it.file.path.contains('aidl')
}
However, I'm still unsatisfied. I do feel something less brutal could be done using variant.aidlCompile.sourceOutputDir that points to the Java files generated by AIDL. But I could not compare the iterated elements in the Closure to this File, or FileTree, or whatever shape I give it...
EDIT:
After further research, another method is to hide the interfaces defined in AIDL using a custom Doclet that supports #hide tag (like Doclava). I preferred this method because it doesn't cause errors during Javadoc generation.

Categories

Resources