javaandroidandroid-studioeasypost

How to add EasyPost Library to Android Studio Project


I have downloaded the library which is a folder containing several files from: https://github.com/EasyPost/easypost-java/archive/master.zip

I have added my own folder named myLibs and added the unzipped project folder to it (folder named easypost-java-master).

My settings.gradle looks as follows:

include ':app'
include ':myLibs:easypost-java-master' 

My build.gradle looks as follows:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile files('libs/gson-2.2.4.jar', 'libs/gson-2.2.4-javadoc.jar', 'libs/gson-2.2.4-javadoc.jar')

    compile fileTree(dir: 'myLibs', include: ['easypost-java-master'])
}

I am getting no errors and able to sync. But when I try to import for example import com.easypost.EasyPost;at my MainActivity, I get the error

Cannot resolve easypost

Am I missing a step?


Solution

  • Am I missing a step?

    Yes, the BIG one. On EasyPost library GitHub profile, there are installation instructions. Did you notice that:

    Installation

    mvn package or build the jar from src!

    To do it, just follow these steps:

    In the latest build of Android Studio 1.2, the creation of JAR library has been made as simple as point and click.

    Steps to follow :

    • Goto File -> New -> New Module enter image description here
    • Select "Java Library" at the end of the options list enter image description here
    • Enter the name of the jar lib and name of class in it and hit finish button enter image description here
    • Thats it !

    The next step is adding your Jar Library as dependency in your app. Simple as that just

    • Goto File -> Project Structure -> Select 'app' -> Select 'Dependency'
    • Select the '+' at the bottom -> Select 'Module Dependency' enter image description here
    • Select your jar lib module you just created above enter image description here enter image description here
    • Select Ok and thats it!

    ....Or you could just add the below line to your App gradle file

    dependencies {
          compile fileTree(dir: 'libs', include: ['*.jar']) // Default Gradle Task, should be already present
          compile 'com.android.support:appcompat-v7:21.0.3' // Default Gradle Task, should be already present
    
          compile project(':nameOfYourJarLibraryModule') // This is your jar library module
     }
    

    Google is promoting the Android Archive(AAR), even though JAR supported is brought back to android studio.To find out the difference between AAR and JAR refer this link

    From: Create an Android Jar library for distribution

    Hope it help