使用Maven存储库将Java库添加到Android Studio项目


问题内容

我想在我的android项目中尝试这个。我正在使用 Android Studio 0.4.6

README.markdown 文件告诉我要插入这里面 的pom.xml

<!-- in the 'repositories' section -->
<repository>
  <id>keytwo.net</id>
  <name>Keytwo.net Repository</name>
  <url>http://audiobox.keytwo.net</url>
</repository>

<!-- in the 'dependencies' section -->
<dependency>
  <groupId>io.socket</groupId>
  <artifactId>socket.io-client</artifactId>
  <version>0.2.1</version> <!-- the desidered version -->
</dependency>

问题是 我没有任何pom.xml
。我在项目根目录中创建了一个,并同步了gradle设置,但是它什么也没做。直到现在,我只使用已经编译的.jar文件或使用gradle编译功能。

如何在项目中使用该库?


问题答案:

Android
Studio不使用Maven作为其构建器;它改用Gradle。幸运的是,Gradle可以使用Maven存储库来获取依赖关系,因此只需将要放入pom文件的信息并以Gradle格式使用即可。这些修改位于模块目录中的
build.gradle 文件中(而不是项目根目录中的生成文件中)。

首先,建立可在其中找到依赖项的存储库。

repositories {
    maven { url 'http://audiobox.keytwo.net' }
}

然后通过将以下行添加到您的代码dependencies块中来添加依赖项本身:

dependencies {
    ...
    compile 'io.socket:socket.io-client:0.2.1'
}

更新:
从POM文件:

compile '<groupId>:<artifactId>:<version>'