11

I am using gradle in Android studio for an android project. I have a jar that I downloaded called TestFlightAppLib.jar. This jar isn't present in the maven repository so I can't just put it in my build.gradle.

How can I add this JAR file to my project? I don't see any option to add an external jar to the project.

enter image description here

Update

This is my complete build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 18
    }

}

dependencies {
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile 'com.android.support:support-v4:18.0.+'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.squareup.retrofit:retrofit:1.2.2'
    compile 'com.github.rtyley:roboguice-sherlock:1.5'
    compile 'org.roboguice:roboguice:2.0'
    compile files('libs/TestFlightLib.jar')
}

This is the error message:

Gradle: Execution failed for task ':MyProject:compileDebug'.
> Compilation failed; see the compiler error output for details.
/Users/droid/android/MyProjectProject/MyProject/src/main/java/com/mypkg/ui/activity/MainApplication.java
Gradle: error: package com.testflightapp.lib does not exist

Here is the class:

import com.testflightapp.lib.TestFlight;


public class MainApplication  {

}
birdy
  • 9,286
  • 24
  • 107
  • 171
  • Please include your full build.gradle file and show the actual error messages you're getting. Please don't refer to your other SO question; individual questions here need to be self-contained. – Scott Barta Jan 10 '14 at 17:51
  • I use this method and it works well: http://stackoverflow.com/questions/1051640/correct-way-to-add-external-jars-lib-jar-to-an-intellij-idea-project#43596431 – Ali Hesari Apr 24 '17 at 19:44

2 Answers2

16

just add

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

to your dependencies in the build.gradle then all the jars in the libs folder will be included.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • This didn't work for me :( I've given more details here http://stackoverflow.com/questions/21049813/adding-external-jar-file-in-gradle-project-on-android-studio – birdy Jan 10 '14 at 17:00
  • I've read your extra question but not sure how to help you on this one. The line I answer to you above was directly copied from our app that currently have half a million users and compiles fine both locally and our build server. Good luck. – Budius Jan 10 '14 at 17:17
  • 1
    try to sync your project with gradle and check if you are getting any error in syncing. – Piyush Agarwal Jan 10 '14 at 18:02
  • why do you need a command to sync with gradle if option available in Android Studio.whatever, you can see all available gradle command line otion here http://www.gradle.org/docs/current/userguide/gradle_command_line.html – Piyush Agarwal Jan 10 '14 at 22:31
  • 1
    @birdy the sync is a good suggestion. It's an AS thing, so there's no command line for Gradle. There's a button on AS on the top bar for that. Also it does every time you open the app. – Budius Jan 11 '14 at 22:45
5

Put the jar in a folder called libs (created on the root of your project). Once moved right click on the jar and you will find "add as library". Click on it and select the module!

iGio90
  • 3,251
  • 7
  • 30
  • 43
  • This didn't work for me. I was able to import the jar but when I run the project, gradle can't find the package. I've asked a detailed question here http://stackoverflow.com/questions/21049813/adding-external-jar-file-in-gradle-project-on-android-studio – birdy Jan 10 '14 at 17:01
  • In Eclipse, I created a \lib directory, added my 3rd party jar file (drag and drop in Windows 10 to the Eclipse project), then right clicked on the jar within Eclipse and I selected Build Path -> Add to Build Path. Then Eclipse added a new virtual folder named Referenced Libraries that contained my jar file. – JasonH Jan 19 '17 at 21:31