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.
Related
I'm writing a Flutter app that uses the "flutter_reactive_ble" plugin. That plugin, in turn, uses "RxAndroidBle" java library. I have made a small change to RxAndroidBle, and it compiles into .aar files. But my Gradle-fu is insufficient to figure out how to tell flutter_reactive_ble to use my version instead of retrieving the latest version from the web. I'd be happiest if I could just point to my local .aar files, but if I have to serve them from my own site, that would be OK as well.
After building my app, the "android/app/build.gradle" file ends with:
dependencies {
. . .
implementation "com.polidea.rxandroidble2:rxandroidble:1.11.1"
}
I assume that's what I'll need to change, but nothing I can find in the Gradle docs look like what I need.
I think, you need fork repo first
And .yaml set like this
(read this)
I think you can do something like this in gradle:
configurations.each {
c -> c.resolutionStrategy.dependencySubstitution {
all { DependencySubstitution dependency ->
if (dependency.requested.group == 'com.polidea') {
dependency.useTarget '<your grade plugin>'
}
}
}
}
It can be replaced alike this:
configurations.all {
exclude group: 'com.polidea.rxandroidble2', module: 'rxandroidble'
}
dependencies {
implementation files('libs/rxandroidble.aar')
// or depend on the module.
}
If you want your app to use your aar, then just declare it as dependency. Or you want the flutter_reactive_ble to use it? I don't think it's possible. You'd have to make pr to RxAndroidBle and then pr to flutter_reactive_ble to take that version in.
I think overriding android dependency may solve your problem.
in build.gradle file of android project
configurations.all {
resolutionStrategy {
force ‘<android library with specific version>’
}
}
in your case:
force com.polidea.rxandroidble2:rxandroidble:1.11.1
check this link for more detail.
--------------------
for adding dependency of local project
in your settings.gradle file add mapping of your local project and include it
project(':local_project_name').projectDir = new File(settingsDir, '<relative path of project>')
include ':local_project_name'
And then add a resolutionStrategy in your project's build.gradle file
configurations.all {
resolutionStrategy {
force implementation project(':local_project_name')
}
}
You need to fork flutter_reactive_ble, go to build.gradle and remove this line then open android studio and open File > Project Structure > Dependencies:
Then Add your module library(.aar files you built):
in the end, android studio will add this line to build.gradle:
implementation(project(path: ":example-library"))
Make sure to use flutter_reactive_ble from your git repository inside your app, in your .yaml file add:
flutter_reactive_ble:
git:
url: your/git/flutter_reactive_ble/repository/url
ref: tag or commit id
I have 2 project modules: A, B and a library module. Both projects using a library. I also have some connected tests in both projects (UI Automator). That tests have common utility code that I'd like to move to the library. How can I do it?
What I've tried. First way:
Copy common code to library project to src/androidTest/java/xxx
folder
Add to library build.gradle UI automator dependencies under
androidTestCompile
Add to project build.gradle following:
dependecies {
...
androidTestCompile project(path: ':library', configuration: "debug")
...
}
That cause build issue in project because as I understand src/androidTest folder is not used during building debug configuration
Second way:
First 2 steps are same
Add to library build.gradle
task connectedTestsJars(type: Jar, dependsOn: "assembleXXXAndroidTest") {
classifier = 'connectedTests'
includes = ['com/**']
from "$buildDir/intermediates/classes/androidTest/XXX"
}
configurations {
connectedTestArtifact
}
artifacts {
connectedTestArtifact connectedTestsJars
}
Add to project build.gradle
dependencies {
...
androidTestCompile project(path: ':library', configuration: "connectedTestArtifact")
...
}
That way all compiles fine. It crashes during connected test runtime because of missing resources from library (it has a lot of common code and resources from both projects)
As I understand that happens because I am using only sources from the library, but I need to use aar file instead (aar with adding src/androidTest contents?).
What will be task type in that case? Or what actions I should make in my custom task to get aar file as result? Other way?
I understand that I can move connected tests common code to another (second) library module and use, but I'd like to avoid it to save compilation time.
Is there any way to keep current module structure and move connected tests common code to library module?
I did find a solution. Add to project's build.gradle:
android {
sourceSets {
androidTest {
java.srcDirs = ['../<LIBRARY_MODULE_NAME>/src/androidTest/java', 'src/androidTest/java']
}
}
...
}
I am sure there is a better solution since the common code will be compilated in both projects. Who knows it?
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.
Basically, I'm using an open-source library in my main project. The library is included by compile project('<path-to-lib>'). The trouble is, there're a lot of files/classes/resources which I don't really need. I only need a small subset of those. Instead of deleting redundant parts, is there any way for me to write Groovy/Gradle script to pick only essential parts for building? This way, ideally, I can make minimal changes to the library.
In the build file for the library you can tailor the source sets to your needs. In general you write something like this:
apply plugin: 'java'
sourceSets {
main {
java {
exclude 'some/unwanted/package/**'
}
}
}
I'm assuming this is a plain Java library. If it's an Android library, the android-library plugin also supports exclude syntax in source sets.
Here's a SO question for reference:
Android Studio Exclude Class from build?
You can also read the Gradle docs for source sets at http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.SourceSet.html#org.gradle.api.tasks.SourceSet:java(groovy.lang.Closure) and the Java plugin at http://www.gradle.org/docs/current/userguide/java_plugin.html
I am trying to build an Android project with Gradle.
It has following structure:
ProjectA----- MainProject,
LibA ---- Library project,
LibB ---- Library project,
LibC ---- Library project,
LibD ---- Library project,
etc...
Based on situtation, I need to include the libraries, sometimes need to include all libraries, 1 library, 2 or 3 etc. based on flavors.
In settings file I have included all projects.
Does anybody know how to include/exclude libraries based on flavors?
I have tried on dependency block, there I am getting error.
Following is the sample code
dependencies {
if (task.name.matches('compileFlovor1'){
exclude module: 'LibD'
}
}
Error is: Could not find method exclude() for arguments [{module=LibD}].
Please guide me resolving this issue
do add flavour specific dependencies you must configure the according configuration.
let's say you have to flavours "free" and "payed"
android {
productFlavors {
free
paid
}
}
then you can set flavour specific dependencies in the dependency block in the following way:
dependencies {
compile project(':library1') //library1 used in free and paid flavour
freeCompile project(':library2')
paidCompile project(':library3')
paidCompile project(':library4')
paidCompile project(':library5')
}
hope that helped,
cheers,
René