Android Studio 3.0 submodule compile failed - android

App level build gradle dependencies
devCompile project(path: ':mymodule', configuration: 'devRelease')
proCompile project(path: ':mymodule', configuration: 'proRelease')
qaCompile project(path: ':mymodule', configuration: 'qaRelease')
offlineCompile project(path: ':mymodule', configuration: 'offlineRelease')
mentioned
publishNonDefault true
flavorDimensions "default"
I have tried This accepted answer but didn't work.
Update:
Look at the library gradle flavor that I want to compile. I have the same flavor mentioned in my app's Module.
dev {
manifestPlaceholders = [facebookId: "SOME_FACEBOOK_ID_1"]
}
pro {
manifestPlaceholders = [facebookId: "SOME_FACEBOOK_ID_2"]
}
qa {
manifestPlaceholders = [facebookId: "SOME_FACEBOOK_ID_3"]
}
offline {
manifestPlaceholders = [facebookId: "SOME_FACEBOOK_ID_4"]
}

You just need to reduce the details you provide:
compile project(path: ':mymodule')
The details what in which configuration is decided by gradle now by themselves. So it became way easier. Instead of 4 lines you just need the above now.
Also remove the publishNonDefault true from your modules gradle. It is not needed anymore.

Dependency management between modules has changed since Android Gradle Plugin 3.0.0. It automatically tries to matches flavours between your app and the libraries/modules it depends on.
See the documentation for more explanation!

Related

Android: Add library module in particular flavor only

In my android application, I have 2 library modules that need to be included conditionally based on flavor. Following is the application structure
:app:library1:library2
And app has 2 flavors, so it will generate 2 apk's, one free version and another paid. So accordingly configured app build.gradle as follows:
android {
flavorDimensions("billing_type")
productFlavors {
free {
dimension "billing_type"
}
paid {
dimension "billing_type"
}
}
}
dependencies {
implementation project(path: 'flavor1')
implementation project(path: 'flavor2')
}
As we can see, both library modules will be included in both flavors(free and paid). But i want to include library2 only in paid flavor. So how can i conditionally add library2 only in paid flavor? I followed some of the approaches mentioned in link
and made following changes:
android {
flavorDimensions("billing_type")
productFlavors {
free {
dimension "billing_type"
}
paid {
dimension "billing_type"
}
}
}
configurations {
freeImplementation
paidImplementation
}
dependencies {
freeImplementation implementation project(path: 'flavor1')
paidImplementation implementation project(path: 'flavor2')
}
All the references online are for older grdale versions which uses compile to add library modules,
But it has been deprecated since gradle plugin 3.0.0. So can anyone help me figure out how to conditionally add library module in particular flavor only in latest
For Gradle versions 3.0 and higher
When your main project uses modules or library modules(AAR) that have flavor dimensions, your app doesn't know which one to use. You should Use missingDimensionStrategy in the defaultConfig block of your app's build.gradle file to specify the default flavor. For example :
missingDimensionStrategy 'dimension', 'flavor1', 'flavor2'
Please check this link for more details.

gradle configurations/artifacts only uses "default" configuration

submodule (locallibs) build.gradle looks like the following:
configurations.create("default")
artifacts.add("default", file('defaultdistro.aar'))
configurations.create("distro2")
artifacts.add("distro2", file('distro2.aar'))
configurations.create("distro3")
artifacts.add("distro3", file('distro3.aar'))
the build.gradle for the app has a dependencies{} section that looks like the following:
dependencies {
debugCompile project(':locallibs')
flavor1ReleaseCompile project(path: ':locallibs', configuration: 'distro2')
flavor2ReleaseCompile project(path: ':locallibs', configuration: 'distro3')
}
The issue I'm having is that regardless of the flavor/build type the "default" configuration is always compiled so all of my flavors are including the "defaultdistro.aar" instead of the correct .aar distro.
I am expecting for flavor1 release build type to compile the distro2.aar but all flavors are compiling the defaultdistro.aar
EDIT: fixed the locallibs build.gradle representation
EDIT #2: the .aar distros are mutually exclusive

How to use publishNonDefault but deploy only one artifact to maven

I have a library that I am deploying to maven. The library is included in a bigger test project with multiple apps. If I set publishNonDefault to true, then in my tests apps I can use:
releaseCompile project(path: ':library', configuration: 'release')
debugCompile project(path: ':library', configuration: 'debug')
however, in this case mavenDeployer is automatically uploading both release and debug artifacts to maven. If I set publishNonDefault to false, then my gradle system fails, because debug flavors can not be found.
Is there any way I can use both? Use debug/release configurations locally and deploy only release as the main artifact to maven?
Thanks
Please try this, it should work:
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-Publication

build:gradle-experimental:0.4.0 include Wearable app

I have a working app in Android Studio using classpath 'com.android.tools.build:gradle-experimental:0.4.0' and gradle 2.8.
As mentioned in google docs, to include your wearable app with your APK, you need to declare the wear app into the phone app gradle file as dependency something like this:
dependencies {
//other dependencies
//wearApp project(':Wear')
wearApp project(path: ':Wear', configuration: 'google')
}
But I get an error on the line where I include my wear app with the error message:
Gradle sync failed: Gradle DSL method not found: 'wearApp()'
This is because the experimental gradle plugin is not recognizing this command ( this is for standard gradle)...
The question is, What is the command for including Wear app with gradle-experimental:0.4.0 plugin ???
** update
Added this to gradle also:
android.buildTypes {
debug {
debuggable = true
embedMicroApp = true // this should enable the WEAR command, idk...
}
release {
minifyEnabled = false
proguardFiles.add(file('proguard-rules.txt'))
}
}
I believe that when you have productFlavors defined in your build.gradle, you need to reference them in your dependencies section, like such:
dependencies {
// other dependencies
googleWearApp project(path: ':wear', configuration: 'google')
}
At least, this is what I'm doing in a similar situation. Give it a try and see if it works!

Multi flavor app based on multi flavor library in Android Gradle

My app has several flavors for several markets in-app-billing systems.
I have a single library which shares the base code for all of my projects. So I decided to add those payment systems to this library as product flavors.
The question is can android library have product flavors?
If so, how can I include different flavors in respective flavor of the app?
I searched a lot, and I couldn't find anything about this scenario. The only close thing I found was this in http://tools.android.com/tech-docs/new-build-system/user-guide:
dependencies {
flavor1Compile project(path: ':lib1', configuration: 'flavor1Release')
flavor2Compile project(path: ':lib1', configuration: 'flavor2Release')
}
I changed configuration to different things but it did not work!
I'm using android studio 0.8.2.
Finally I found out how to do this, I will explain it here for others facing same problem:
If App and Library have same Flavor name(s)
It's possible since Gradle Plugin 3.0.0 (and later) to do something like:
Library build.gradle:
apply plugin: 'com.android.library'
// Change below's relative-path
// (as the `../` part is based on my project structure,
// and may not work for your project).
apply from: '../my-flavors.gradle'
dependencies {
// ...
}
android {
// ...
}
Project build.gradle:
buildscript {
// ...
}
apply plugin: 'com.android.application'
// Note that below can be put after `dependencies`
// (I just like to have all apply beside each other).
apply from: './my-flavors.gradle'
dependencies {
api project(':lib')
}
android {
productFlavors {
// Optionally, configure each flavor.
market1 {
applicationIdSuffix '.my-market1-id'
}
market2 {
applicationIdSuffix '.my-market2-id'
}
}
}
My flavors .gradle:
android {
flavorDimensions 'my-dimension'
productFlavors {
market1 {
dimension 'my-dimension'
}
market2 {
dimension 'my-dimension'
}
}
}
If App or Library has different Flavor-name (old answer)
The key part is to set publishNonDefault to true in library build.gradle, Then you must define dependencies as suggested by user guide.
Update 2022; publishNonDefault is now by default true, and setting it to false is ignored, since said option is deprecated.
The whole project would be like this:
Library build.gradle:
apply plugin: 'com.android.library'
android {
....
publishNonDefault true
productFlavors {
market1 {}
market2 {}
market3 {}
}
}
project build.gradle:
apply plugin: 'com.android.application'
android {
....
productFlavors {
market1 {}
market2 {}
market3 {}
}
}
dependencies {
....
market1Compile project(path: ':lib', configuration: 'market1Release')
market2Compile project(path: ':lib', configuration: 'market2Release')
// Or with debug-build type support.
android.buildTypes.each { type ->
market3Compile project(path: ':lib', configuration: "market3${type.name}")
}
}
Now you can select the app flavor and Build Variants panel and the library will be selected accordingly and all build and run will be done based on the selected flavor.
If you have multiple app module based on the library Android Studio will complain about Variant selection conflict, It's ok, just ignore it.
There are one problem with Ali answer. We are losing one very important dimension in our build variants. If we want to have all options (in my example below 4 (2 x 2)) we just have to add custom configurations in main module build.gradle file to be able to use all multi-flavor multi-buildType in Build Variants. We also have to set publishNonDefault true in the library module build.gradle file.
Example solution:
Lib build.gradle
android {
publishNonDefault true
buildTypes {
release {
}
debug {
}
}
productFlavors {
free {
}
paid {
}
}
}
App build.gradle
android {
buildTypes {
debug {
}
release {
}
}
productFlavors {
free {
}
paid {
}
}
}
configurations {
freeDebugCompile
paidDebugCompile
freeReleaseCompile
paidReleaseCompile
}
dependencies {
freeDebugCompile project(path: ':lib', configuration: 'freeDebug')
paidDebugCompile project(path: ':lib', configuration: 'paidDebug')
freeReleaseCompile project(path: ':lib', configuration: 'freeRelease')
paidReleaseCompile project(path: ':lib', configuration: 'paidRelease')
}
Update for Android Plugin 3.0.0 and higher
According to the official Android Documentation - Migrate dependency configurations for local modules,
With variant-aware dependency resolution, you no longer need to use variant-specific configurations, such as freeDebugImplementation, for local module dependencies—the plugin takes care of this for you
You should instead configure your dependencies as follows:
dependencies {
// This is the old method and no longer works for local
// library modules:
// debugImplementation project(path: ':library', configuration: 'debug')
// releaseImplementation project(path: ':library', configuration: 'release')
// Instead, simply use the following to take advantage of
// variant-aware dependency resolution. You can learn more about
// the 'implementation' configuration in the section about
// new dependency configurations.
implementation project(':library')
// You can, however, keep using variant-specific configurations when
// targeting external dependencies. The following line adds 'app-magic'
// as a dependency to only the "debug" version of your module.
debugImplementation 'com.example.android:app-magic:12.3'
}
So in Ali's answer, change
dependencies {
....
market1Compile project(path: ':lib', configuration: 'market1Release')
market2Compile project(path: ':lib', configuration: 'market2Release')
}
to
implementation project(':lib')
And plugin will take care of variant specific configurations automatically. Hope it helps to others upgrading Android Studio Plugin to 3.0.0 and higher.
My Android Plugin is 3.4.0,and I find that it doesn't need configurations now.All you need is to make sure the flavorDimensions and productFlavors in application contains one productFlavor of the same flavorDimensions and productFlavors in libraries.For sample:
In mylibrary's build.gradle
apply plugin: 'com.android.library'
android {
....
flavorDimensions "mylibFlavor"
productFlavors {
market1
market2
}
}
application's build.gradle:
apply plugin: 'com.android.application'
android {
....
flavorDimensions "mylibFlavor", "appFlavor"
productFlavors {
market1 {
dimension "mylibFlavor"
}
market2 {
dimension "mylibFlavor"
}
common1 {
dimension "appFlavor"
}
common2 {
dimension "appFlavor"
}
}
}
dependencies {
....
implementation project(path: ':mylibrary')
}
After sync,you can switch all options in Build Variants Window:
To get the flavors working on an AAR library, you need to define defaultPublishConfig in the build.gradle file of your Android Library module.
For more information, see: Library Publication.
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:
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" }
I also ran into a problem compiling modules for various options.
What i've found:
It looks like we don't need add publishNonDefault true into lib's build.gradle file, since Gradle 3.0.1.
After decompiling a class BaseExtension found this:
public void setPublishNonDefault(boolean publishNonDefault) {
this.logger.warn("publishNonDefault is deprecated and has no effect anymore. All variants are now published.");
}
And instead of:
dependencies {
...
Compile project(path: ':lib', configuration: 'config1Debug')
}
We should use:
dependencies {
...
implementation project(':lib')
}
Only the important thing, is to add a configurations {...} part to the build.gradle.
So, the final variant of app's build.gradle file is:
buildTypes {
debug {
...
}
release {
...
}
}
flavorDimensions "productType", "serverType"
productFlavors {
Free {
dimension "productType"
...
}
Paid {
dimension "productType"
...
}
Test {
dimension "serverType"
...
}
Prod {
dimension "serverType"
...
}
}
configurations {
FreeTestDebug
FreeTestRelease
FreeProdDebug
FreeProdRelease
PaidTestDebug
PaidTestRelease
PaidProdDebug
PaidProdRelease
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':lib')
...
}
Also, you can use Filter variants to restrict build variants.
P.s. don't forget to include modules in the settings.gradle file, like:
include ':app'
include ':lib'
project(':lib').projectDir = new File('app/libs/lib')
At the moment it's not possible, although if I recall correctly its a feature they want to add. (Edit 2: link, link2 )
Edit:
For the moment I'm using the defaultPublishConfig option to declare which library variant get's published:
android {
defaultPublishConfig fullRelease
defaultPublishConfig demoRelease
}
I know this subject has been closed, but just an update with gradle 3.0, see this : https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#variant_aware and grep matchingFallbacks and missingDimensionStrategy.
Now it's way more simple to declare the dependencies between module flavors.
...and in this precise case with gradle3.0, as flavors share the same name, gradle would map them magically, there is no configuration required.
In this situation. How could I import the dependency for a specific build. For example: market1Common1Debug
market1Common1DebugImplementation 'androidx.appcompat:1.2.0'

Categories

Resources