如何将 EasyPost 库添加到 Android Studio 项目

How to add EasyPost Library to Android Studio Project

我已经下载了库,它是一个包含多个文件的文件夹: https://github.com/EasyPost/easypost-java/archive/master.zip

我添加了我自己的名为myLibs的文件夹,并将解压后的项目文件夹添加到其中(文件夹名为easypost-java-master)。

我的 settings.gradle 看起来如下:

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

我的 build.gradle 看起来如下:

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'])
}

我没有收到任何错误并且能够同步。但是当我尝试在我的 MainActivity 中导入例如 import com.easypost.EasyPost; 时,我收到错误

无法解析 easypost

我是不是漏了一步?

Am I missing a step?

是的,大的。在 EasyPost 库 GitHub 配置文件中,有安装说明。你注意到了吗:

Installation

mvn package or build the jar from src!

要做到这一点,只需按照以下步骤操作:

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
  • Select "Java Library" at the end of the options list
  • Enter the name of the jar lib and name of class in it and hit finish button
  • 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'
  • Select your jar lib module you just created above
  • 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

希望对您有所帮助