Add productFlavor to experimental Android gradle plugin library
Asked Answered
C

3

13

I've got a project that uses the experimental gradle plugin, i.e. 0.2.0. When I've got no productFlavor, I can perfectly integrate a library module in Android Studio and everything works perfectly. But when the library has a productFlavor, I main project doesn't find the library classes.

This fix does not appear to work with the experimental gradle plugin. Does anyone have any idea how make flavors work with the new plugin?

Library:

android.productFlavor {
    create ('flavor') {
        ...
    }
}

Project:

...

dependencies {
    compile project(':mylibrary')
}
Cara answered 5/10, 2015 at 20:23 Comment(0)
U
10

This might help?

Library Publication

By default a library only publishes its release variant. This variant will be used by all projects referencing the library, no matter which variant they build themselves. This is a temporary limitation due to Gradle limitations that we are working towards removing.

You can control which variant gets published with

android { defaultPublishConfig "debug" }

Note that this publishing configuration name references the full variant name. Release and debug are only applicable when there are no flavors. If you wanted to change the default published variant while using flavors, you would write:

android {defaultPublishConfig "flavor1Debug" }

It is also possible to publish all variants of a library. We are planning to allow this while using a normal project-to-project dependency (like shown above), but this is not possible right now due to limitations in Gradle (we are working toward fixing those as well). Publishing of all variants are not enabled by default. To enable them:

android {publishNonDefault true } 

It is important to realize that publishing multiple variants means publishing multiple aar files, instead of a single aar containing multiple variants. Each aar packaging contains a single variant. Publishing a variant means making this aar available as an output artifact of the Gradle project. This can then be used either when publishing to a maven repository, or when another project creates a dependency on the library project.

Gradle has a concept of default" artifact. This is the one that is used when writing:

compile project(':libraries:lib2')

To create a dependency on another published artifact, you need to specify which one to use:

dependencies {
    flavor1Compile project(path: ':lib1', configuration: 'flavor1Release')
    flavor2Compile project(path: ':lib1', configuration: 'flavor2Release') 
}

Important: Note that the published configuration is a full variant, including the build type, and needs to be referenced as such. Important: When enabling publishing of non-default, the Maven publishing plugin will publish these additional variants as extra packages (with classifier). This means that this is not really compatible with publishing to a maven repository. You should either publish a single variant to a repository OR enable all config publishing for inter-project dependencies.

From here.

Unlikelihood answered 18/10, 2015 at 8:44 Comment(1)
I already tried this.Cara
B
0

The syntax is wrong.

With the experimental plugin (0.2.1) flavors are define outside the android block and the right syntax is android.productFlavors

model {
    android {

    }

    android.productFlavors {
        create("flavor") {

        }
    }
}
Barker answered 5/10, 2015 at 20:48 Comment(5)
Does this example work for you on 0.2.1? With the proper syntax, of course. android.productFlavors in the library and compile project(':mylibrary') in the main project.Cara
The syntax is correct and it works fine with a application module. I don't know how it can work with a library. In my opinion you should move the flavor in the main module (I've never seen flavors inside a library)Barker
The problem is that I have a library that contains some native C code. This C code uses a static library at link time which I need to specify in the build.gradle file. I need to do this for each architecture and the only way I know works is this: github.com/googlesamples/android-ndk/blob/master/…Cara
In the Known Limitations of official page you can find: No support for creating and depending on static libraries. Not sure if the issue can be related to this.Barker
But it does work! They have their own official example. And I compiled mine and it works. What I need is just to be able to use this in a library.Cara
E
0

The things are a little bit more complex. I've tested on gradle experimental 0.8.0

on the library you need:

model{
    android {
    ...
    publishNonDefault true

     productFlavors {
                create("flavor1") {
                }
                create("flavor2") {
                }
            }
    }
}

on the app you need:

model {
    android {
    ...
    publishNonDefault true

     productFlavors {
                create("flavor1") {
                }
                create("flavor2") {
                }
            }
    }
}
//after the model is closed !!
configurations {
    flavor1DebugCompile
    flavor1ReleaseCompile

    flavor2DebugCompile
    flavor2ReleaseCompile
}

dependencies {
...
 flavor1DebugCompile project(path: ':mylibrary', configuration: 'flavor1Debug')

flavor1ReleaseCompile project(path: ':mylibrary', configuration: 'flavor1Release')

 flavor2DebugCompile project(path: ':mylibrary', configuration: 'flavor2Debug')

flavor2ReleaseCompile project(path: ':mylibrary', configuration: 'flavor2Release')

  }

hopefully, if you run full assemble you will get 2 apk's, one for each flavor . I've used "./gradlew build" from a cmd and got them. note that for your lib, you will get an AAR for each flavor too

Exemplar answered 3/7, 2017 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.