Android Studio 3.0 - "Failed to transform file 'gson-2.2.4.jar' to match attributes {artifactType=android-classes}"

10,538

Solution 1

Here is best solution for importing any jar, arr libs

enter image description here

allprojects {
    repositories {
        flatDir {
            dirs 'libs'
        }
    }
}

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

You do not need to add repeatable lines with libs file name.

if you are a new to android studio, you can check the project structure from android by tapping drop-down menu.

enter image description here

Solution 2

Since Gson is in maven central, you have to add mavenCentral() in buildscripts phase. you have to add the Gson library in the dependency using project structure, now to update the gradle, for the add the .jar file to your build.gradle file like:

dependencies {
    compile files('libs/gson-2.2.4.jar')
}

or

dependencies {
    compile 'com.google.code.gson:gson:2.2.+'
}

And make sure you are connected to the internet

Solution 3

did you try

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

in your build.graddle file of app module

Share:
10,538
marcuthh
Author by

marcuthh

BEng Software Engineering student and Sheffield Hallam University, currently searching for a work placement for around the time of the next academic year. C# is my specialty, although I'm proficient in other object-oriented languages such as C++ and Java. Currently working on my web design and development and have a solid level of knowledge in HTML, CSS, PHP, SQL, JavaScript and jQuery. Keen to keep learning so I do ask a lot of questions. Love the researching and testing side of things so always happy to try and solve people's problems when I get the chance. Realised I was truly a programmer the day I watched my coursemate type the word 'python' in a suggestive text to a female and it autocorrected to an uppercase 'P'. No going back once you're in with a circle like that.

Updated on October 14, 2022

Comments

  • marcuthh
    marcuthh over 1 year

    I'm a newcomer to Android Studio development, and I'm currently following an online tutorial to make a messenger app.

    I've followed the steps of this tutorial, and it now needs me to download the Library files for Smack API & GSON, and add the .jar files to the libs folder in my project.

    With this done, I right-clicked the files in the directory and clicked Add as library (the tutorial says to do Build Path -> Add to Build Path, but this doesn't seem to be an option in this version of Android Studio).

    At this point, the project directory menu sidebar looks like this: enter image description here

    From here, I tried to rebuild the project but was met with the following error:

    enter image description here

    I've not been able to find a way around this error at all, and don't know enough about Android Studio to know if this is an issue with the IDE or the package itself. I've had a look around S/O but none of the posts seem to describe the same issue, or offer any solutions to similar issues that appear to work for this too.

    If anyone has run into this before, or if it is a common Android Studio problem, would you be able to advise me on how to resolve it please? Very frustrating when I just want to get on with learning how to code for Android!

    Any help massively appreciated, thanks in advance!

    Mark

    • F43nd1r
      F43nd1r over 6 years
      That tutorial is meant for eclipse, not android studio. Plus it ignores the vital concept of build tools (for android usually gradle). I'd advise against using it to learn android concepts in general, and android studio in particular.
    • portfoliobuilder
      portfoliobuilder over 4 years
      Did you ever find an answer to this?
  • marcuthh
    marcuthh over 6 years
    Thanks for your reply. I have gotten past the original error but I am now getting an error of "Could not find method compile() for arguments [file collection]", which is referring to the first of the two lines in your response. I put this line inside dependencies {} which is nested inside buildscripts {}, is this the right place for it or have I missed something? Thanks!