Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a JAR in my gradle project?

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  {

}
like image 844
birdy Avatar asked Jan 10 '14 13:01

birdy


People also ask

Where are jars stored in gradle?

Gradle declares dependencies on JAR files inside your project's module_name /libs/ directory (because Gradle reads paths relative to the build.gradle file). This declares a dependency on version 12.3 of the "app-magic" library, inside the "com.example.android" namespace group.

How do I create a runnable jar with gradle?

In its simplest form, creating an executable JAR with Gradle is just a matter of adding the appropriate entries to the manifest. However, it's much more common to have dependencies that need to be included on the classpath, making this approach tricky in practice.


1 Answers

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!

like image 115
iGio90 Avatar answered Oct 14 '22 10:10

iGio90