Reputation: 2497
I published my Android library (with dependencies) to an AWS S3 maven repository using this guide.
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:
implementation ('com.mydomain:my_library:1.4:release@aar') {
transitive=true
}
("transitive=true" is required so that the library's dependencies are included.)
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. 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.
<?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>
Upvotes: 7
Views: 3709
Reputation: 2691
If you are building the library using maven, you need to add the maven android plugin. That way you can package it as an AAR file. 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.
plugin website: https://simpligility.github.io/android-maven-plugin/plugin-info.html
Example 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. 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. 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.
api: is the exact opposite of implementation. Dependencies labeled as api will creep into the consumer's path, therefore, it must be used everytime you are exposing one of your dependencies. Using the same example as before, you should import RxAndroid with api, and retrofit with implementation. 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.
Upvotes: 0
Reputation: 1060
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:
implementation 'com.codesgood:justifiedtextview:1.1.0'
Just the way you want to make yours available through gradle. Let me know if you need some help going through the steps of that post, I'll be happy to help π
Upvotes: 1
Reputation: 5954
If your project happens to be hosted on Github consider using https://jitpack.io/ instead.
Jitpack allows you to very easily publish your libraries to gradle directly from Github.
While this answer does not directly answer your question, it does offer a valid and simpler alternative.
Upvotes: 3