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!
Related
My Crashlytics data is not appearing in the Firebase Crashlytics view. It does show "Crash-free statistics" as being 75%, so it appears to be recording some data. I also see the crashes appearing instantly both in Fabric and in the old Firebase Crash Reporting interface, as well as in the DebugView as general events but nothing in the Issues section of the Crashlytics page. I thought that perhaps my Fabric project had not been linked to my Firebase project, but when I tried doing it manually using this link https://www.fabric.io/firebase_migration/apps it tells me the projects are already linked, specifically it says Project already contains a linked app with that bundle ID and platform. Every time I open my app in Android studio I immediately run the command adb shell setprop debug.firebase.analytics.app ie.moses.keepitlocal so that my events will appear in the DebugView, perhaps this could be affecting it, but I doubt it.
Here is my project build.gradle file:
buildscript {
repositories {
google()
jcenter()
maven {
url 'https://maven.fabric.io/public'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.google.gms:google-services:3.3.0'
classpath 'io.fabric.tools:gradle:1.25.4'
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url 'https://maven.google.com/'
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
and this is the build.gradle for my specific app module
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
def supportLibraryVersion = '27.1.1'
android {
compileSdkVersion 27
defaultConfig {
applicationId "ie.moses.keepitlocal"
minSdkVersion 16
targetSdkVersion 27
versionCode 5
versionName "0.3.1-alpha"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
buildConfigField 'boolean', 'CRASHLYTICS', 'true'
}
release {
buildConfigField 'boolean', 'CRASHLYTICS', 'true'
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
if (details.requested.group == 'com.android.support') {
// Used to prevent conflicting versions of Android support library
// contained in other dependencies (e.g. all the firebase dependencies)
details.useVersion supportLibraryVersion
}
}
}
dependencies {
implementation files('libs/YouTubeAndroidPlayerApi.jar')
implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
implementation "com.android.support:recyclerview-v7:$supportLibraryVersion"
implementation "com.android.support:cardview-v7:$supportLibraryVersion"
implementation 'com.google.firebase:firebase-core:16.0.3'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.3'
implementation 'com.google.firebase:firebase-crash:16.2.0'
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.5'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
implementation 'com.google.guava:guava:26.0-android'
}
apply plugin: 'com.google.gms.google-services'
I am using a Button to force a crash with the following code (as I mentioned, these crashes appear almost immediately in Fabric and in the old Crash Reporting interface which has been deprecated and will be removed in 2 days!). Here is the code for the crash button:
Button crashButton = new Button(this);
crashButton.setText("Crash!");
crashButton.setOnClickListener(view -> {
throw new IllegalStateException("you hit the crash button!");
});
addContentView(crashButton, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
EDIT
I have just noticed that on the firebase transition status page it says "0/1 APP LINKED" and "NO PROJECTS YET".
I also need to reinstall the Crashlytics library from the Fabric plugin every time I open Android studio. The dependencies already exist in my gradle file but the plugin does not seem to recognise them.
EDIT
I am now trying to unlink fabric and firebase so I can try linking them again from fresh. I came across this question Unlink an existing firebase app?, but fabric is not even listed as an integration in my project.
Forget Fabric, it's not required. Just follow instructions in your Firebase console and Firebase documentation: https://firebase.google.com/docs/crashlytics/get-started#android
Don't mess with Fabric console, you should completly remove any calls to Fabric console and configure your project from scratch as Firebase doc says. As soon as you run app correctly configured, you will see Crashlytics panel in your Firebase console
The only solution I could find was as follows (I am including all steps exactly as I carried them out even if they may not be relevant):
I exported my realtime database to JSON and made a copy of my database rules.
I deleted my app from the Firebase project.
I deleted the Firebase project.
I deleted my project from Fabric.
I removed all references to Fabric, Crashlytics and the old Firebase Crash Reporting library from my project, this consisted of removing the dependencies from both my build.gradle files and deleting the fabric.properties file.
I ran gradlew.bat clean (I'm on Windows) on my project from the command line.
I did a Ctrl+Shift+F (search all project files) for the words fabric and crash just to make sure there was absolutely no remaining references to Fabric, Crashlytics or the deprecated Crash Reporting library (which was still a dependency in my build.gradle without me realising it because Firebase assistant tool added that as the crash reporting dependency when I first tried to get this working and then immediately told me it was deprecated :/ ).
I searched in File Explorer on windows for any files referencing "Fabric" or "Crash" (there weren't any).
I created a new project on Firebase.
I imported my realtime database from the previously exported JSON file and copy/pasted back in my rules.
I recreated my one test user account (thankfully the app is not yet in production :p).
I reenabled analytics (although this was simply a matter of going to the Analytics tab and seeing that it was already enabled as I still had the dependency in my build.gradle for my app module).
I went to the Crashlytics tab and saw the original screen explaining how to enable Crashlytics. This time however I did not create the project on Fabric or install the Fabric plugin, I only copied in the Fabric dependencies as I think #jake was suggesting, these can be found here https://firebase.google.com/docs/crashlytics/get-started?authuser=0.
I then tried to produce a crash using the method described here https://firebase.google.com/docs/crashlytics/force-a-crash?authuser=0.
And then voilĂ ! The crash appeared immediately in the Crashlytics tab of Firebase. No Fabric project, no Fabric plugin, just the dependencies.
This method had the added benefit of allowing me to be rid of extra databases and user properties I did not want (which Firebase does not currently allow you to remove once you create them >:( ), but this method obviously may not be feasible for anyone who already has a lot invested in their Firebase project and cannot simply start over. Luckily for me this was not too much of a pain.
add apply plugin: 'com.google.firebase.crashlytics' in app module
and dependencies {
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.1.1'
classpath 'com.google.gms:google-services:4.3.3'
} in project module
I had the same issue as you. I was able to link Fabric to Firebase after unlinking Crashlytics from Firebase following the instructions from this answer. Sadly this could only be done with building a custom url myself.
I am developing a google map app using Android Studio. When I sync the project with Gradle Files (Select Tools/Android/Sync Project with Gradle Files on Android Studio), I get the following error:
Could not resolve com.google.android.gms:play-services-maps:11.6.2.
Couold not get resource 'https://dl.google.com/dl/android/maven2/com/google/android/gms/play-services-maps/11.62./play-services-maps-11.6.2.pom
This is the software I am using:
Android Studio 3.0.1
Android Version 7.1.1
Google Play Services 46
Google Repository 58 (under Support Repository)
I've tried using both the google maps project template and the empty activity project but I get the same error.
I am using the http proxy setup I used from earlier projects. I am able to install the play-services-maps and the google repository.
I also added the maven url to the Gradle Project file in the All Projects/Repositories section.
I have tried the suggestions from Stack Overflow but none have not worked.
Any ideas?
Gradle Project Build file:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url "https://maven.google.com"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
The gradle app build file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "hansen.scott.googlemaps"
minSdkVersion 25
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.android.gms:play-services-maps:11.6.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
The error is in the line:
implementation 'com.google.android.gms:play-services-maps:11.6.2'
try adding this in project level gradle file
allprojects {
repositories {
maven {
url "https://maven.google.com"
}
}
}
Someone had the same issue I did and entered it on Stack Overflow a day or two ago. The user was getting a build error when using player-services-maps 11.6.2. If the user changed the version to 11.0.0, the build succeeded.
Once I back-dated the maps version to 11.0.0, this solved my problem too. I created a new Google Map project and didn't modify the Gradle Build files.
The problem appears to be that play-services-maps won't build using a versions after 11.0.0. I will post the issue on Github.
Thank you for everybody's help.
I have been facing the same issue for the past one week and posted it on Stack Overflow as well but to no effect.
I have since then figured out the root cause. I am working out of my office and thus have firewalls in place. I got it checked with the system admin and on turning off the firewall the build worked.
So i would advice you to have this checked as well.
Kind regards,
Kabir
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!
I recently installed the latest Canary build of Android Studio which is currently using the Android Gradle plugin 3.0.0-alpha4 .
I now get a error:
Error:Failed to resolve: Could not resolve project :MyLib.
Required by:
project :app
I has read: Migrate dependency configurations for local modules
dependencies
{
// This is the old method and no longer works for local
// library modules:
// debugCompile project(path: ':foo', configuration: 'debug')
// releaseCompile project(path: ':foo', 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(':foo')
// 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'
}
I changed:
releaseCompile project(path: ':MyLib', configuration: 'appReleaseApp')
debugCompile project(path: ':MyLib', configuration: 'appDebug')
to:
implementation project(':MyLib')
but i still have this error: Error:Failed to resolve: Could not resolve project :MyLib.
lib gradle:
apply plugin: 'com.android.library'
android {
publishNonDefault true
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
minSdkVersion 14
targetSdkVersion 25
}
buildTypes {
debug {
...
}
releaseApp {
...
}
releaseSdk {
...'
}
}
flavorDimensions "default"
productFlavors {
flavor1{
...
flavor2{
...
}
flavor3{
...
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.google.android.gms:play-services-maps:10.2.6'
compile 'com.google.android.gms:play-services-gcm:10.2.6'
compile 'com.google.android.gms:play-services-location:10.2.6'
}
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
repository(url: mavenLocal().url)
}
}
}
app gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
minSdkVersion 19
targetSdkVersion 25
versionCode 12
versionName "5.0.2"
}
buildTypes {
release {
...
}
debug {
...
}
}
flavorDimensions "default"
productFlavors {
flavor1 {
...
}
flavor2 {
...
}
}
testOptions {
unitTests {
all {
jvmArgs '-noverify'
systemProperty 'robolectric.logging.enable', true
}
}
}
}
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
// releaseCompile project(path: ':MyLib', configuration: 'appRelease')
// debugCompile project(path: ':MyLib', configuration: 'appDebug')
implementation project(':MyLib')
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.google.android.gms:play-services-maps:10.2.6'
compile 'com.google.android.gms:play-services-location:10.2.6'
compile 'com.google.android.gms:play-services-analytics:10.2.6'
compile 'com.google.android.gms:play-services-gcm:10.2.6'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:gridlayout-v7:25.3.1'
compile 'com.android.volley:volley:1.0.0'
compile 'com.facebook.stetho:stetho:1.4.1'
compile 'com.facebook.stetho:stetho-okhttp3:1.4.1'
compile 'com.android.support:percent:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.squareup.picasso:picasso:2.5.2'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.1.0'
testCompile 'org.robolectric:robolectric:3.1.4'
testCompile 'org.assertj:assertj-core:1.7.1'
compile 'com.flipboard:bottomsheet-core:1.5.0'
compile 'com.flipboard:bottomsheet-commons:1.5.0'
compile 'com.android.support.constraint:constraint-layout:1.0.1'
}
apply plugin: 'com.google.gms.google-services'
Please help
Google added more instruction how to solve it: Resolve build errors related to dependency matching
Cause of build error:
Your app includes a build type that a library dependency does not.
For example, your app includes a "staging" build type, but a
dependency includes only a "debug" and "release" build type.
Note that there is no issue when a library dependency includes a build
type that your app does not. That's because the plugin simply never
requests that build type from the dependency.
Resolution
Use matchingFallbacks to specify alternative matches for a given build type, as shown below:
// In the app's build.gradle file.
android {
buildTypes {
debug {}
release {}
staging {
// Specifies a sorted list of fallback build types that the
// plugin should try to use when a dependency does not include a
// "staging" build type. You may specify as many fallbacks as you
// like, and the plugin selects the first build type that's
// available in the dependency.
matchingFallbacks = ['debug', 'qa', 'release']
}
}
}
After facing the same issue, I finally declared exactly the same buildTypes in both App and Modules' build.gradle files.
In your case, adding
buildTypes {
debug {}
releaseApp {}
releaseSdk {}
}
to your module's build.gradle should do the trick.
Be sure to change any "compile project" to "implementation project" too.
Hope it helps
With the new plugin, the variant-aware dependency resolution
implementation project(':MyLib')
needs to have exact matching build types. The migration guide describes this
For instance, it is not possible to make a 'debug' variant consume a
'release' variant through this mechanism because the producer and
consumer would not match. (In this case, the name 'debug' refers to
the published configuration object mentioned above in the Publishing
Dependencies section.) Now that we publish two configurations, one for
compiling and one for runtime, this old way of selecting one
configuration really doesn't work anymore.
So the old method of
releaseCompile project(path: ':foo', configuration: 'debug')
will not work anymore.
Example
With your example this would look like this:
In app build.gradle:
apply plugin: 'com.android.application'
android {
buildTypes {
debug {}
releaseApp {}
releaseSdk {}
}
...
dependencies {
implementation project(':MyLib')
}
}
In module/lib 'MyLib' build.gradle:
apply plugin: 'com.android.library'
android {
buildTypes {
debug {}
releaseApp {}
releaseSdk {}
}
}
Therefore the build type must exactly match, no more no less.
Using Build-Type Fallbacks
A new feature called "matchingFallbacks" can be used to define default buildtypes if a sub-module does not define the buildtype.
Use matchingFallbacks to specify alternative matches for a given build type (...)
For example if module/lib 'MyLib' gradle would look like this:
apply plugin: 'com.android.library'
android {
buildTypes {
debug {}
releaseLib {}
}
}
You could define the following in your app build.gradle:
apply plugin: 'com.android.application'
android {
buildTypes {
debug {}
releaseApp {
...
matchingFallbacks = ['releaseLib']
}
releaseSdk {
...
matchingFallbacks = ['releaseLib']
}
}
...
dependencies {
implementation project(':MyLib')
}
}
Missing Flavor Dimensions
Use missingDimensionStrategy in the defaultConfig block to specify the
default flavor the plugin should select from each missing dimension
android {
defaultConfig {
missingDimensionStrategy 'minApi', 'minApi18', 'minApi23'
...
}
}
I was facing the same problem, I found this migration page:
Build matching types
It states:
Select defaults for missing build types
If a consumer configures a build type that a producer does not, you need to manually match the consumer's build type to one from the producer. For example, if your app module configures a "staging" build type and its library module dependency, "mylibrary", does not, the Android plugin throws the following build error:
Error:Failed to resolve: Could not resolve project :mylibrary.
Required by: project :app
To resolve this error, you need to specify which build type from "mylibrary" the Android plugin should match to the app's "staging" build type. You can do this with the buildTypeMatching property in the app's build.gradle file, as shown below:
// Add the following to the consumer's build.gradle file.
android {
...
// Tells the Android plugin to use a library's 'debug' build type
// when a 'staging' build type is not available. You can include
// additional build types, and the plugin matches 'staging' to the
// first build type it finds from the one's you specify. That is,
// if 'mylibrary' doesn't include a 'debug' build type either, the
// plugin matches 'staging' with the producer's 'release' build type.
buildTypeMatching 'staging', 'debug', 'release'
}
Adding buildTypeMatching fixed it for me without creating unecessary types in my library
Today I also had the same problem after migrating to Android Studio 3.
The problem is the gradle is not able to resolve the certain libraries due to network issue. The reasons might be various.
If you work behind the proxy you need to add the proxy parameters in gradle.properties file:
systemProp.http.proxyHost=<proxy_host>
systemProp.http.proxyPort=<proxy_port
systemProp.https.proxyHost=<proxy_host>
systemProp.https.proxyPort=<proxy_port>
In my case I had one more issue. My company uses the self signed SSL certificate so the SSL connection had some problem. If same applies also for you, you can set the parameter again in gradle.properties file as follows:
org.gradle.jvmargs=-Djavax.net.ssl.trustStore="/usr/lib/jvm/java-8-oracle/jre/lib/security/cacerts" -Djavax.net.ssl.trustStoreType=JKS -Djavax.net.ssl.keyStorePassword=changeit
To be more clear you can click on "Show details" link in messages log in Android Studio. This log will be more helpful to decide what is the real problem.
This solution worked for me. I'm using Android Studio 3.1.2. Android Gradle plugin 3.1.2. Gradle 4.4. I have a library module with flavours such as trial and premium. As part of the process of migrating to the Android Gradle plugin 3.1.2 I added a flavour dimension of main to my library module's gradle build file. To correct the build error therefore in my app's build.gradle file I changed the following:
debugImplementation project(path: ':library', configuration: 'premiumDebug')
releaseImplementation project(path: ':library', configuration: 'premiumRelease')
became
implementation project(':library')
and I added the following line to my defaultConfig block: missingDimensionStrategy 'main', 'premium'
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'