使用 maven 存储库将 java 库添加到 Android Studio 项目中

2022-09-01 00:28:31

我想在我的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编译功能。

如何在我的项目中使用此库?


答案 1

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>'

答案 2

语法:实现 'groupId:artifactId:version'

如果这是您必须在Android Studio项目中导入的内容...

// Maven : Add these dependecies to your pom.xml (java6+)
// <dependency>
//     <groupId>org.glassfish.jersey.core</groupId>
//     <artifactId>jersey-client</artifactId>
//     <version>2.8</version>
// </dependency>
// <dependency>
//     <groupId>org.glassfish.jersey.media</groupId>
//     <artifactId>jersey-media-json-jackson</artifactId>
//     <version>2.8</version>
// </dependency>

然后它翻译成这个...

implementation 'org.glassfish.jersey.core:jersey-client:2.8'
implementation 'org.glassfish.jersey.media:jersey-media-json-jackson:2.8'

推荐