How can I add a JAR in my gradle project?

江枫思渺然 提交于 2019-11-27 03:22:01

问题


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.

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  {

}

回答1:


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.




回答2:


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!



来源:https://stackoverflow.com/questions/21046095/how-can-i-add-a-jar-in-my-gradle-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!