3

I try to specify two different sourcesets of a gradle library and add a sourceset specific dependency in a project.

enter image description here

The idea is to do something like this:

The library build.gradle file:

apply plugin: 'com.android.library'

android {
   ...
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'

            java {
                srcDir 'src'
            }
            res {
                srcDir 'res'
            }
        }

        exclude_fr_it{
            manifest.srcFile 'AndroidManifest.xml'

            java {
                srcDir 'src'
            }
            res {
                srcDir 'res'
                exclude 'res/values/values-fr'
                exclude 'res/values/values-it'
            }
        }
    }
...
}
...

In the Project A, I want to include the main sourceset:

dependencies {
    compile project(':explore_layout')
}

In the Project B, I want to include the build that use sourceset exclude_fr_it.

dependencies {
    compile project(':explore_layout') library 'exclude_fr_it'
}

I tried to understand the documentation. But I don't see how this can be done...

Question 1

Doc

Question 2

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
lukas
  • 495
  • 4
  • 13

2 Answers2

1

This is not a direct answer to the sourceSets question. But you may not need to use the sourceSets feature at all.

If you simply want your app to include a subset of the resources in your custom android library, you can accomplish that with a configuration block in your app's build.gradle. (In fact, the library does not need those special configuration blocks for the 'main' and 'exclude_fr_it' sourceSets at all.)

This is possible because of the 'resConfigs' property.

In particular, suppose your library has resource configurations for three languages: 'en', 'fr', and 'it'.

Then, in project A, which includes all of the languages from the library, you simply use this in build.gradle:

dependencies {
    compile project(':explore_layout')
}

But, in project B, which you want to exclude the custom library's 'fr' and 'it' resources, you use the 'resConfigs' property within a configuration closure to include only a subset of resources like this:

dependencies {
    compile project(':explore_layout') {
        android {
            defaultConfig {
                resConfigs 'en'
            }
        }
    }
}
albert c braun
  • 2,650
  • 1
  • 22
  • 32
  • Thank you very much for your answer! I tried your solution, but this gives me an other error. Error:(99, 0) Android tasks have already been created. This happens when calling android.applicationVariants, android.libraryVariants or android.testVariants. Once these methods are called, it is not possible to continue configuring the model. https://stackoverflow.com/questions/29910720/android-studio-error-building-android-tasks-have-already-been-created – lukas Aug 22 '17 at 05:38
  • https://stackoverflow.com/a/26801256/1797191 worked for me. you showed me the direction to google the right thing! Thanks you a lot – lukas Aug 22 '17 at 05:49
0

I have achieved this using distribution to mavenLocal of a specific sources specification. I have a library project that has some java.awt code that I want to omit so I can include the project as dependency to an android project (which doesnt support java.awt)

In your library you will have something like this:

apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'maven-publish'

sourceSets {
    main {
        java {
            srcDirs = [ 'src' ]
        }
    }

    // android source set excludes portion of source that 
    // references java.awt
    android {
        java {
            srcDirs = [ 'src' ]
            excludes = ['**/skunkworks/**', '**/awt/**']
        }
    }
}

// this task compiles sources defined by custom sourceSet 'android'
task androidJar(type: Jar) {
    from sourceSets.find {'android'}.output
}

publishing {
    publications {
        myLibrary(MavenPublication) {
            groupId = 'foo.bar'
            artifactId = 'mylib-android'
            version = '1.0'

            artifact androidJar
        }
    }

}

Then use the tasks->publishing->publishToMavenLocal target to compile the custom jar to your ~/.m2/repository.

Now in the android project I reference like this:

repositories {
    mavenLocal()
}

dependencies {
    implementation 'foo.bar:mylib-android:1.0'
}
Fracdroid
  • 1,135
  • 10
  • 15