简体   繁体   中英

Maven publish an aar embedding jar libs

I am trying to publish an aar to a my brand new private Maven server. My aar has a dependency on another aar, that is embedding jar files.

What is the "good" (and working..) way to go with Artifactory here ?

Main aar
  |
  |__ Shared aar
        |
        |__ Toy jar
        |
        |__ Totoy jar

When I try to use Main in an Android app, when it gets to a class used in Shared, I got a nice - and brutal - error:

java.lang.NoClassDefFoundError: Failed resolution of: [.. class embedded in Totoy jar]

I tried multiple combinaisons, like publishing the Shared on maven, referencing it as compile "com.myapp.Shared:1.0.0" in Main, doing the same for Main, or just referencing it as a compile (project_module)

Should my .jar files also be published to the private Maven and referenced in the Daugther (so I'll have all my dependencies in my Maven)?

The jar are Cling and associated dependencies (seamless), used for some classes in Shared, that are called from the Main aar.

When I publish the Shared library and use it directly in a sample, I don't have the same error (meaning the crash doesn't occur, it happens only when I am using the Main library, itself embedding Shared).

When all this is defined in one project to rule them all and all compile orders are defined as "internal module" dependencies, it works fine, si I guess the problem is with the jar of the Shared lib, but didn't find how to solve this...

Thanks for the help!

Edit

I managed to make it work, see my answer.

So, I managed to make it work:

  1. I deleted my .jar in the libs directory and added Cling & the jetty dep as maven dependencies. I guess if you have a custom jar, you should just do the same and publish it to your Maven repository.

  2. I tracked another java.lang.NoClassDefFoundError: Art is not as chatty as Delvik was.

My problem was that I was using some class from the Cling library in my Main.aar, and the depedency was not transitive as I thought when publishing to Maven (but it was no problem when local compiling). So adding Cling as a dep of Main.aar solved my problem.< br/> That will serve as a lesson to track my dep more carefully.

A thing to remember is that the default .pom file generated will not include the dependencies to your publications: you have to add this kind of thing in your gradle task:

publishing {
    publications {
        aar(MavenPublication) {
            groupId packageName
            version = libraryVersion
            artifactId project.getName()

            // Tell maven to prepare the generated "*.aar" file for publishing
            artifact("$buildDir/outputs/aar/$artifactId-release.aar")

            //The publication doesn't know about our dependencies, so we have to manually add them to the pom
            pom.withXml {
                // for dependencies and exclusions
                def dependenciesNode = asNode().appendNode('dependencies')
                configurations.compile.allDependencies.each { dp ->
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', dp.group)
                    dependencyNode.appendNode('artifactId', dp.name)
                    dependencyNode.appendNode('version', dp.version)
                }
            }
        }
    }
}

Also, keep in mind that gradle will simply ignore any repository that you add in the .pom. So in my case, as Cling is not on jcenter or MavenCentral, I have to add the Cling repository in the gradle of my lib, but future users of the lib will have to add it too (or the Cling dependency will not be resolved.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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