The usage of Google Services requires the use of this Gradle plugin:
apply plugin: 'com.google.gms.google-services'
This thread on the Gradle forum has lead me to conclude that you can't selectively enable or disable a plugin, meaning that is not an option. I've tried a number of solutions offered by older threads involving conditionals surrounding the apply plugin statement and none of them work now.
The plugin itself is configured by a google-services.json that Google generates and is placed into the project directory. I'm aware that you can have different configurations for each flavour by having multiple google-services.json files, but what are we meant to do for build flavours that we explicitly want to not use Google Services (e.g. for targeting devices that do not have Play Services installed)? Blank files or dummy JSON files don't work as the plugin will refuse to accept it.
So the only solution that I can think of would be to manually disable the plugin (comment out that line) each time I want to test/build my flavour. Surely there has to be a better way to control how the plugin works?
I finally got a version to work. Tested with gradle 4.6, build tools 3.0.1, google-services plugin 3.1.1
apply plugin: 'com.google.gms.google-services'
android.applicationVariants.all { variant ->
if (variant.name == 'someVariantNameYouDontwantFirebase') {
project.tasks.getByName('process' + variant.name.capitalize() + 'GoogleServices').enabled = false
}
}
Related
In my gradle file I have:
apply plugin: 'checkstyle'
I'm trying to create my own checkstyle rules. For this reason I have added a dependency to my gradle file.
dependencies {
checkstyle 'com.puppycrawl.tools:checkstyle:8.2'
}
I am trying to extend "Check" class of checkstyle. But there are a lot of version of checkstyle and I dont know which one is used by gradle.
How can I find the exact version number of checkstyle which gradle is using?
There are three ways I can think of right now, least attractive first:
You can look into the Gradle source code.
You can check the Checkstyle Compatibility Matrix (column L, yellow cells).Both say that from Gradle 3.3 onwards, the default Checkstyle version is 6.19; before, it was 5.9. Only Gradle versions prior to 2.4 used even older versions of Checkstyle.
But the recommended way is to choose the Checkstyle version explicitly, by specifying it in your build.gradle file:
checkstyle {
configFile file('your/checkstyle.xml');
toolVersion '8.2'; // your choice here
}
This is better than relying on the default version, because you can use much newer versions of Checkstyle, and your Checkstyle setup won't break when you update Gradle.
You can check the current value of checkstyle.toolVersion by writing this in your build.gradle file and reloading your gradle project
plugins {
id 'java'
id 'checkstyle'
}
println checkstyle.toolVersion
Provide a better solution, and you can refer the to the examples of configuration file from checkstyle source codes without worrying about compatibility.
Simply run gradle dependencies | grep checkstyle
Then get corresponding checkstyle sources here to refer the configuration samples: https://github.com/checkstyle/checkstyle/releases
with out-of-box apply plugin: 'checkstyle', the checkstyle would use v6.19.
Downloaded dependencies:
Download https://repo1.maven.org/maven2/com/puppycrawl/tools/checkstyle/6.19/checkstyle-6.19.pom
Download https://repo1.maven.org/maven2/org/antlr/antlr4-runtime/4.5.3/antlr4-runtime-4.5.3.pom
Download https://repo1.maven.org/maven2/org/antlr/antlr4-master/4.5.3/antlr4-master-4.5.3.pom
Download https://repo1.maven.org/maven2/commons-beanutils/commons-beanutils/1.9.3/commons-beanutils-1.9.3.pom
Download https://repo1.maven.org/maven2/commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.pom
Download https://repo1.maven.org/maven2/org/apache/commons/commons-parent/41/commons-parent-41.pom
Download https://repo1.maven.org/maven2/commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.jar
Download https://repo1.maven.org/maven2/com/puppycrawl/tools/checkstyle/6.19/checkstyle-6.19.jar
Download https://repo1.maven.org/maven2/commons-beanutils/commons-beanutils/1.9.3/commons-beanutils-1.9.3.jar
Download https://repo1.maven.org/maven2/org/antlr/antlr4-runtime/4.5.3/antlr4-runtime-4.5.3.jar
after dependencies { checkstyle 'com.puppycrawl.tools:checkstyle:8.2' }, the downloaded dependency list:
Download https://repo1.maven.org/maven2/com/puppycrawl/tools/checkstyle/8.2/checkstyle-8.2.pom
Download https://repo1.maven.org/maven2/org/antlr/antlr4-runtime/4.7/antlr4-runtime-4.7.pom
Download https://repo1.maven.org/maven2/org/antlr/antlr4-master/4.7/antlr4-master-4.7.pom
Download https://repo1.maven.org/maven2/commons-cli/commons-cli/1.4/commons-cli-1.4.pom
Download https://repo1.maven.org/maven2/net/sf/saxon/Saxon-HE/9.8.0-4/Saxon-HE-9.8.0-4.pom
Download https://repo1.maven.org/maven2/org/antlr/antlr4-runtime/4.7/antlr4-runtime-4.7.jar
Download https://repo1.maven.org/maven2/net/sf/saxon/Saxon-HE/9.8.0-4/Saxon-HE-9.8.0-4.jar
Download https://repo1.maven.org/maven2/commons-cli/commons-cli/1.4/commons-cli-1.4.jar
Download https://repo1.maven.org/maven2/com/puppycrawl/tools/checkstyle/8.2/checkstyle-8.2.jar
Has the same effect add the "apply plugin" at the beginning or end of the file build.gradle in Android Studio projects?
For example to add the 'com.google.gms.google-services' plugin, Firebase official documentation recommends adding at the end, but I've seen other codes add it at the beginning.
I know the question seems irrelevant, but I'm developing a plugin for Android Studio to manage dependencies and have this doubt.
Thanks in advance
Gradle scripts are interpreted top to bottom so order can be important. Keep in mind that gradle has a configuration phase and an execution phase so sometimes order isn't important. It's common to apply your plugins at the top of the script since plugins often add extension objects and tasks to the gradle model which can then be configured lower down in the build script.
For example, you can't do the following because the test task is added by the java plugin:
test {
include 'org/foo/**'
}
apply plugin: 'java'
EDITED
As #doug-stevenson tells us in his answer, the Firebase dependency declaration does not have to be at the bottom of build.gradle anymore.
On the Firebase SDK setup page, it says,
apply plugin: 'com.android.application'
android {
// ...
}
dependencies {
// ...
compile 'com.google.firebase:firebase-core:9.0.2'
}
// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'
Why is that? Does the ordering matter?
UPDATE: With the latest version of the play services plugin, it is no longer necessary to apply it at the bottom of build.gradle.
It has to do with the way the plugin helps to manage dependencies, and the order of events that Gradle uses to build the project.
The plugin will actually add a dependency on firebase-core if it's not present in your project. It will also check the version of Firebase and Play Services dependencies. However, in order for it to do all this without conflict with other plugins, the Google Services plugin has to run against a project after the project dependencies are already defined. So, applying the plugin after the dependencies block (typically at the bottom of the file) makes all this possible.
The important thing to know is that some projects may experience a version conflict problem if the plugin is at the top. You will avoid these problems by adding it to the bottom.
I have been using Dexguard for my Android project, and it's been working fine until recently I had to use a another plugin. Because the way the other plugin is built, it is required that the project applies either "com.android.application" or "com.android.library". but since the dexguard plugin is an extension of the com.android.application which got replaced by dexguard, I can't use the other plugins that requires the "android" plugin.
//apply plugin: 'android'
apply plugin: 'dexguard'
Does anyone know if there's a way to get around this? I have contacted the authors of the plugin but it won't be practical to bother every plugin author for a solution.
Reference to my problem:
Dexguard plugin specification
And here's the plugin (android-apt) I'm trying to use that requires plugin: Android and only Android not dexguard.
The latest DexGuard plugin (6.1.03) works alongside the Android plugin (1.0.0), instead of extending it. This should improve its compatibility with other third-party plugins.
I have a question very similar to Gradle build fails looking for Google Play Services in Android Studio
I have a working android project but when I add
compile 'com.google.android.gms:play-services:5.0.77'
I get a gradle build error
Error:Failed to find: com.google.android.gms:play-services:5.2.8
Open File<br>Open in Project Structure dialog
I have added the SDKs through the manager. Other threads have suggested that there might be a second SDK library on the computer causing the issue, but I have not been able to resolve this. I am using a mac (and normally a PC user so please bear with me) and looking at the SDK manager and the project structure dialog both say the SDK is located at:
/Applications/Android Studio.app/sdk
Given they are pointing to the same place it must be some other cause of the error?
Maybe I have a version other than 5.2.8 (although unlikely, as andoid studio says this is the most up to date and I have just updated the SDK)? How can I check the version installed on my machine - its not in the file names?
If you are going to use version 5.2.8 you have to set on your build.gradle
compile 'com.google.android.gms:play-services:5.2.08'
Because it is store on the directory :
ANDROID_SDK_PATH/extras/google/m2repository/com/google/android/gms/play-services/5.2.08
You can use '+' instead '08' and you will be using the last minor version
compile 'com.google.android.gms:play-services:5.2.+'
OK so it looks as though my version number was off and thats all that was causing the problem. If anyone else has this issue check your version number in the AndroidManifest.xml file in the google_play_services_lib directory
There is a problem with the 5.0.77 release version. Change com.google.android.gms:play-services:5.0.77 to com.google.android.gms:play-services:5.0.89 and things will work fine.
Google pulled out 5.0.77 from the Maven repository due to some critical bugs. Read More.
I had a different problem altogether. I'd put the line
apply plugin: 'com.google.gms.google-services'
at the start of the file. Even before:
apply plugin: 'com.android.application'
And had to scratch my head for half an hour.
So the final (working) condition looked like:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'