Saturday, June 30, 2018

Gradle: More than one variant of project :myLib matches the consumer attributes

I have written a custom library gsp-library which uses RemoteMessage from Firebase. My main app also uses Firebase for checking crush issue and so on.
Environment:
gradle 4.7
java 8

Exception:

More than one variant of project :gsp-library matches the consumer attributes How to fix it? Any kind of help will be highly appreciated.

project.gradle

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath 'com.google.gms:google-services:3.3.0'
    }
}

allprojects {
    repositories {
        ...
    }
}

app.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.beauty.app"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 12
        versionName "1.12"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    // Keep the following configuration in order to target Java 8.
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.google.android.gms:play-services-gcm:12.0.1'
    implementation 'com.google.android.gms:play-services:12.0.1'

    implementation 'com.google.firebase:firebase-core:12.0.1'
    implementation 'com.google.firebase:firebase-messaging:12.0.1'
    implementation 'com.google.firebase:firebase-crash:12.0.1'

    implementation project(":gsp-library")
}

apply plugin: 'com.google.gms.google-services'

gsp-library.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 27

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 12
        versionName "1.12"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
    implementation 'com.google.firebase:firebase-messaging:12.0.1'
}
Error comes from both following command:
beautyApp\gsp-library> gradle clean assembleDebug
beautyApp\app> gradle clean assembleDebug
Exception:
More than one variant of project :myModule matches the consumer attributes:
  - Configuration ':myModule:debugApiElements' variant android-aidl:
      - Found artifactType 'android-aidl' but wasn't required.
      - Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
      - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
      - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
      - Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
  - Configuration ':myModule:debugApiElements' variant android-classes:
  - Configuration ':myModule:debugApiElements' variant android-manifest:
  - Configuration ':myModule:debugApiElements' variant android-renderscript:
  - Configuration ':myModule:debugApiElements' variant jar:

Root Cause Analysis:

The google-services gradle plugin is made to work with Google Play Services and Firebase 15+, but you're using it with version 12.0.1.
Solution#1:
There is a known bug with Google services 3.3.0.
Either use the 4.0.1 version which fixes the bug or downgrade to 3.2.1
Try to set
classpath 'com.google.gms:google-services:3.2.1'
instead of
classpath 'com.google.gms:google-services:3.3.0'

Resource Link: https://stackoverflow.com/questions/50132601/gradle-more-than-one-variant-of-project-mylib-matches-the-consumer-attributes

Thursday, June 7, 2018

Starting a Gradle Daemon, 1 busy and 6 stopped Daemons could not be reused, use --status for details

Question Details:
When I was running the gradle clean command, I got the following message
Starting a Gradle Daemon, 1 busy and 6 stopped Daemons could not be reused, use --status for details
For the investigation, I run the following command.
$ gradle --status
   PID STATUS   INFO
 11506 BUSY     4.3.1
  8027 STOPPED  (stop command received)
  9347 STOPPED  (stop command received)
 11727 STOPPED  (by user or operating system)
  4786 STOPPED  (by user or operating system)
 14569 STOPPED  (by user or operating system)
 31237 STOPPED  (by user or operating system)

Only Daemons for the current Gradle version are displayed. See https://docs.gradle.org/4.3.1/userguide/gradle_daemon.html#sec:status
So, some questions have arisen in my mind.
  1. How can I stop an existing daemon?
  2. How to kill daemon process?
Answer:
Stopping an existing Daemon:
If you want to explicitly stop running Daemon processes for any reason, just use the command gradle --stop.
But gradle --stop won’t stop daemons running with a different version of Gradle.
How to kill all Daemon process on my machine?
Gradle will kill any Daemon that has been idle for 3 hours or more, so you don’t have to worry about cleaning them up manually.
So the command and output are given below:
$ gradle --stop
Stopping Daemon(s)
2 Daemons stopped
After stopping daemons, status will be look like below:
$ gradle --status
No Gradle daemons are running.
   PID STATUS   INFO
  8027 STOPPED  (stop command received)
  9347 STOPPED  (stop command received)
 12448 STOPPED  (stop command received)
 11506 STOPPED  (stop command received)
 11727 STOPPED  (by user or operating system)
  4786 STOPPED  (by user or operating system)
 14569 STOPPED  (by user or operating system)
 31237 STOPPED  (by user or operating system)
If you are stuck with gradle build running, you can to through this post: Stuck with Gradle Build Running

Difference among mavenCentral(), jCenter() and mavenLocal()?

Question Details:
Actually, I am studying on build.gradle file. In some cases, I got that sometimes they are using mavenCentral()jCenter() and mavenLocal() in repositories section. Sometimes they also using URL. So some question arises in my mind?
i) Difference among mavenCentral(), jCenter() and mavenLocal()?
ii) Which one should I use most?
iii) Is there any performance related issue?
Answer:
Actually, all 3 are Maven repository. We use these maven repositories to our build by using its URL address or its location in the local file system.
By using URL:
repositories {
    maven { url("https://plugins.gradle.org/m2/") }
}
By using local file system:
repositories {
    maven { url '../maven-repo' }
}
Gradle has three “aliases” which we can use when we are adding Maven repositories to our build. These aliases are:

1. mavenCentral():

The mavenCentral() alias means that dependencies are fetched from the central Maven 2 repository.
repositories {
    mavenCentral()
}
The URL used to access this repository is https://repo.maven.apache.org/maven2/. The name of the repository is MavenRepo.

2. jcenter():

The jcenter() alias means that dependencies are fetched from the Bintray’s JCenter Maven repository

3. mavenLocal():

The mavenLocal() alias means that dependencies are fetched from the local Maven repository.

Resource Link:


Jcenter vs. mavenCentral

jcenter() and mavenCentral() is a repository for the Gradle plugin in Android Studio
Earlier versions of Android Studio used mavenCentral(), and after some time, it switched to jcenter.
This is because jcenter() is superior to mavenCentral() in terms of performance and memory footprint:
  • Jcenter is the world's largest Java repository
  • Jcenter through the CDN service, using the https protocol, highly secured, and Android Studio 0.8 version mavenCentral() using the http protocol
  • Jcenter is a superset of mavenCentral, including many additional jars
  • Jcenter performance is better than mavenCentral
  • mavenCentral will automatically download many IDE-related indexes, and these are used less often which are not required.

Monday, June 4, 2018

Why daemon from Android Studio is not compatible for terminal?

Question Details:

If we use gradle clean build, it will create a daemon. On the other hand, if we clean or build through Android studio, it also generates another daemon.
So, How can we make daemons compatible?

Answer:
Actually, there are two main reasons to be incompatible.
  • One is version mismatch of Gradle and Java. Gradle version may be same but JDK versions are different.
  • By default, Android Studio is using embedded JDK, which most likely has a different version from Java installed in our machine.
How can we make daemon compatible?
If we use the local java in Android Studio instead of embedded JDK, this issue will be solved.

Procedure:

  1. In Android Studio, go to Project Structure -> SDK location.
  2. Uncheck “Use embedded JDK” and
  3. specify your local JDK.

Pictorial View is given below:

First Part:
enter image description here
Second Part:
enter image description here

Intellij IDEA crashed, and then throws an error [Content is not allowed in prolog]

Question Details:
I am using IntelliJ Idea 2017.2. And my project is a multi-module project built by gradle. I have given a build of my project. Then it crashed. I have started IntelliJ again and got the following message
Cannot load settings from file '.../config/options/code.style.schemes.xml': java.lang.AssertionError: Unexpected content storage modification File content will be recreated.
Then I have thought, it may be cache related issue. And then I restarted my PC again and do the following,
File -> Invalidate Caches / Restart
This time I got another error.
Error:Internal error: (org.jdom.input.JDOMParseException) Error on line 1: Content is not allowed in prolog.
org.jdom.input.JDOMParseException: Error on line 1: Content is not allowed in prolog.
    at org.jdom.input.SAXBuilder.build(SAXBuilder.java:533)
    at org.jdom.input.SAXBuilder.build(SAXBuilder.java:946)
    at com.intellij.openapi.util.JDOMUtil.loadDocument(JDOMUtil.java:364)
    at com.intellij.openapi.util.JDOMUtil.loadDocument(JDOMUtil.java:342)
    at org.jetbrains.jps.model.serialization.JpsLoaderBase.loadRootElement(JpsLoaderBase.java:69)
    at org.jetbrains.jps.model.serialization.JpsLoaderBase.loadRootElement(JpsLoaderBase.java:40)
    at org.jetbrains.jps.model.serialization.JpsLoaderBase.loadComponents(JpsLoaderBase.java:52)
    at org.jetbrains.jps.model.serialization.JpsProjectLoader.loadFromDirectory(JpsProjectLoader.java:119)
    at org.jetbrains.jps.model.serialization.JpsProjectLoader.loadProject(JpsProjectLoader.java:98)
    at org.jetbrains.jps.model.serialization.impl.JpsSerializationManagerImpl.loadModel(JpsSerializationManagerImpl.java:41)
    at org.jetbrains.jps.cmdline.JpsModelLoaderImpl.loadModel(JpsModelLoaderImpl.java:45)
    at org.jetbrains.jps.cmdline.BuildRunner.load(BuildRunner.java:71)
    at org.jetbrains.jps.cmdline.BuildSession.runBuild(BuildSession.java:198)
    at org.jetbrains.jps.cmdline.BuildSession.run(BuildSession.java:113)
    at org.jetbrains.jps.cmdline.BuildMain$MyMessageHandler$1.run(BuildMain.java:133)
    at org.jetbrains.jps.service.impl.SharedThreadPoolImpl$1.run(SharedThreadPoolImpl.java:41)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1436)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:999)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
    at org.jdom.input.SAXBuilder.build(SAXBuilder.java:518)
    at org.jdom.input.SAXBuilder.build(SAXBuilder.java:946)
    at com.intellij.openapi.util.JDOMUtil.loadDocument(JDOMUtil.java:364)
    at com.intellij.openapi.util.JDOMUtil.loadDocument(JDOMUtil.java:342)
    at org.jetbrains.jps.model.serialization.JpsLoaderBase.loadRootElement(JpsLoaderBase.java:69)
    at org.jetbrains.jps.model.serialization.JpsLoaderBase.loadRootElement(JpsLoaderBase.java:40)
    at org.jetbrains.jps.model.serialization.JpsLoaderBase.loadComponents(JpsLoaderBase.java:52)
    at org.jetbrains.jps.model.serialization.JpsProjectLoader.loadFromDirectory(JpsProjectLoader.java:119)
    at org.jetbrains.jps.model.serialization.JpsProjectLoader.loadProject(JpsProjectLoader.java:98)
    at org.jetbrains.jps.model.serialization.impl.JpsSerializationManagerImpl.loadModel(JpsSerializationManagerImpl.java:41)
    at org.jetbrains.jps.cmdline.JpsModelLoaderImpl.loadModel(JpsModelLoaderImpl.java:45)
    at org.jetbrains.jps.cmdline.BuildRunner.load(BuildRunner.java:71)
    at org.jetbrains.jps.cmdline.BuildSession.runBuild(BuildSession.java:198)
    at org.jetbrains.jps.cmdline.BuildSession.run(BuildSession.java:113)
    at org.jetbrains.jps.cmdline.BuildMain$MyMessageHandler$1.run(BuildMain.java:133)
    at org.jetbrains.jps.service.impl.SharedThreadPoolImpl$1.run(SharedThreadPoolImpl.java:41)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
I have again tried to clean/build the project. But getting same error always. I have searched throguh internet and got the following issues.
But No LUCK. Any kind of help will be highly appreciated.

Answer:

At last I have solved this issue by deleting the [caches] folder in the following path.
/home/<username>/.IntelliJIdea2017.2/system/caches (Linux)
/Users/<username>/.IntelliJIdea2017.2/system/caches (Mac)
C:\Users\<username>\.IntelliJIdea2017.2\system\caches (Windows)
Then restarted IntelliJ Idea again and it works fine.