Adding external JAR file in Gradle project on Andr

2019-06-14 15:07发布

问题:

This question already has an answer here:

  • How can I add a JAR in my gradle project? 2 answers

I'm having a terrible time adding an external JAR to my gradle project in Android Studio. I asked a similar question here but those answers haven't worked for me.

I created a libs folder and added my JAR in there:

~/android/MyProject/libs $ pwd
/Users/me/android/MyProject/libs
~/android/MyProject/libs $ ls
TestFlightLib.jar

And I added the following in my build.gradle

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

After doing all this I clicked Build -> Rebuild project but that still doesn't let me add import com.testflightapp.lib.TestFlight

I also tried the second solution of right clicking TestFlightLib.jar and adding it as a Module. This lets me add import com.testflightapp.lib.TestFlight successfully but when I Run the project I still get the error: Gradle: error: package com.testflightapp.lib does not exist

What am I doing wrong?

How can I simply add an external JAR to my gradle project?

回答1:

Try this:

  • right click on your project
  • choose "open module settings"
  • click on module, then "dependencies"
  • check that your file exists in the list
  • if not, press + and add it as a file


回答2:

try this:compile files('libs/x.jar', 'libs/y.jar')