简体   繁体   English

如何将Android库发布到Maven存储库以便轻松使用?

[英]How to publish an Android library to a Maven repository to be used easily?

I published my Android library (with dependencies) to an AWS S3 maven repository using this guide. 我使用指南将我的Android库(带有依赖项)发布到AWS S3 maven存储库。

In a new project, I added the S3 bucket as a Maven repository, and I can successfully use it if I add the following to the new project's build.gradle file: 在一个新项目中,我将S3存储桶添加为Maven存储库,如果我将以下内容添加到新项目的build.gradle文件中,我可以成功使用它:

implementation ('com.mydomain:my_library:1.4:release@aar') {
    transitive=true
}

("transitive=true" is required so that the library's dependencies are included.) (“transitive = true”是必需的,以便包含库的依赖项。)

However, I'd like for others to be able to use a simplified version of that like I've seen so many other libraries do: 但是,我希望其他人能够使用我已经看过很多其他库的简化版本:

implementation 'com.mydomain:my_library:1.4'

When I do that, I get "Cannot resolve symbol" errors in code and (of course) get "does not exist" errors when I try to build my project. 当我这样做时,我在代码中得到“无法解决符号”错误,并且(当然)在我尝试构建项目时会出现“不存在”错误。

My library's POM file is shown below. 我的图书馆的POM文件如下所示。 Is there something I can change there so I can use the "simplified" implementation line above, or is there something else I can do? 有什么我可以在那里改变所以我可以使用上面的“简化”实现行,或者我还能做些什么呢?

Note: I tried replacing "< packaging>pom< /packaging>" with "< packaging>aar< /packaging>", invalidated the cache, and ran again, but the behavior was identical. 注意:我尝试将“<packaging> pom </ packaging>”替换为“<packaging> aar </ packaging>”,使缓存无效,然后再次运行,但行为完全相同。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mydomain</groupId>
  <artifactId>my_library</artifactId>
  <version>1.4</version>
  <packaging>pom</packaging>
  <dependencies>
    <dependency>
      <groupId>com.android.support</groupId>
      <artifactId>appcompat-v7</artifactId>
      <version>27.1.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.android.support.constraint</groupId>
      <artifactId>constraint-layout</artifactId>
      <version>1.1.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.android.support</groupId>
      <artifactId>design</artifactId>
      <version>27.1.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>io.reactivex.rxjava2</groupId>
      <artifactId>rxandroid</artifactId>
      <version>2.1.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>io.reactivex.rxjava2</groupId>
      <artifactId>rxjava</artifactId>
      <version>2.2.4</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.github.instacart.truetime-android</groupId>
      <artifactId>library-extension-rx</artifactId>
      <version>3.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.google.dagger</groupId>
      <artifactId>dagger-android</artifactId>
      <version>2.15</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.nostra13.universalimageloader</groupId>
      <artifactId>universal-image-loader</artifactId>
      <version>1.9.5</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.google.firebase</groupId>
      <artifactId>firebase-core</artifactId>
      <version>16.0.6</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.google.firebase</groupId>
      <artifactId>firebase-messaging</artifactId>
      <version>17.3.4</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

If your project happens to be hosted on Github consider using https://jitpack.io/ instead. 如果您的项目恰好托管在Github上,请考虑使用https://jitpack.io/

Jitpack allows you to very easily publish your libraries to gradle directly from Github. Jitpack允许您直接从Github轻松发布库以进行gradle。

While this answer does not directly answer your question, it does offer a valid and simpler alternative. 虽然这个答案没有直接回答你的问题,但确实提供了一个有效且简单的替代方案。

I had exactly the same issue! 我有完全相同的问题! and asked the same question, you can see my question here . 并问了同样的问题,你可以在这里看到我的问题。 My problem was the explanations in the answers and the posts shared to me were not clear enough (or maybe I wasn't able to understand them properly)... BUT! 我的问题是答案中的解释和分享给我的帖子不够清楚(或者我可能无法正确理解它们)......但是! then I found this super useful post thanks to AndroidWeekly, I followed the steps in that post and then I was able to publish my library to maven, you can check my library in here 😃and you can add it to your project using this: 然后我发现这个超级有用的帖子感谢AndroidWeekly,我按照那篇文章中的步骤然后我能够将我的库发布到maven,你可以在这里检查我的库😃你可以使用这个将它添加到你的项目中:

implementation 'com.codesgood:justifiedtextview:1.1.0'

Just the way you want to make yours available through gradle. 只是你希望通过gradle提供你的方式。 Let me know if you need some help going through the steps of that post, I'll be happy to help 🙂 如果您需要一些帮助来完成该帖子的步骤,请告诉我,我很乐意帮助🙂

If you are building the library using maven, you need to add the maven android plugin. 如果您使用maven构建库,则需要添加maven android插件。 That way you can package it as an AAR file. 这样您就可以将其打包为AAR文件。 In this case, in my experience, the compile scope does not spread your library's dependencies into the path, you still need to add the transitive=true flag. 在这种情况下,根据我的经验,编译范围不会将库的依赖项扩展到路径中,您仍然需要添加transitive = true标志。

plugin website: https://simpligility.github.io/android-maven-plugin/plugin-info.html 插件网站: https//simpligility.github.io/android-maven-plugin/plugin-info.html

Example POM: https://github.com/fcopardo/EasyRest/blob/master/pom.xml 示例POM: https//github.com/fcopardo/EasyRest/blob/master/pom.xml

Now, if you are using gradle to create the library, you need to understand how gradle packages the dependencies. 现在,如果您使用gradle创建库,则需要了解gradle如何打包依赖项。 There are two ways: 有两种方法:

implementation: by using this word, the dependency is kept into the library's path. 实现:通过使用这个词,依赖关系保存在库的路径中。 It will not be exposed to the consumer's path. 它不会暴露在消费者的道路上。 Therefore, the consumer project will fail if the library's dependency needs to be located. 因此,如果需要定位库的依赖项,则使用者项目将失败。 For example, if you are going to return RxAndroid observables, compiling will fail, since the compiler will not be able to locate the RxAndroid classes mentioned in the return's methods signature. 例如,如果要返回RxAndroid observable,则编译将失败,因为编译器将无法找到返回方法签名中提到的RxAndroid类。 In the other hand, if the library's dependency is used only internally (for example, you build a rest module using retrofit and expose only RxJava in the return signatures), compilation will be possible. 另一方面,如果库的依赖关系仅在内部使用(例如,您使用改进构建rest模块并在返回签名中仅公开RxJava),则可以进行编译。

api: is the exact opposite of implementation. api:与实现完全相反。 Dependencies labeled as api will creep into the consumer's path, therefore, it must be used everytime you are exposing one of your dependencies. 标记为api的依赖关系将渗透到使用者的路径中,因此,每次暴露其中一个依赖关系时都必须使用它。 Using the same example as before, you should import RxAndroid with api, and retrofit with implementation. 使用与以前相同的示例,您应该使用api导入RxAndroid,并使用实现进行改造。 That should allow you to consume the library without any extra imports or flags. 这应该允许您使用库而不需要任何额外的导入或标志。

Keep in mind, the maven android plugin supports the gradle project's format since version 4, so you can have both a POM file and a .gradle file. 请记住,maven android插件支持自版本4以来的gradle项目格式,因此您可以同时拥有POM文件和.gradle文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM