简体   繁体   中英

Module library Jar not shown in External Libraries in Android Studio

I am having trouble getting a jar library to show up under External Libraries in Android Studio. I am trying to add javadoc for this library, and the only method I've found online is to right click on the library in External Libraries and select Library Properties... .

The project structure is a tree of many modules:

rootsdk / 
        main.jar
        main-javadoc.jar
        plugins /
                plugin1 / 
                        build.gradle
                        ...
                plugin2 /
                        build.gradle
                        ...
                ...

The dependency is declared in the build.gradle files like:

compileOnly files('../../main.jar')

If I open up the individual directories plugin1 , then the dependency shows up in External Libraries correctly. But if I open up the rootsdk project, it does not appear. All of the modules are listed and compilable from the root project, and I can use classes defined in the library just fine, but it does not appear under External Libraries , so I cannot add the javadoc for it.

The strange thing is some of the plugins use other libraries, but defined differently:

repositories {
    flatDir {
     dirs 'libs'
    }
}

...

implementation(name: 'core-debug', ext: 'aar')

And these libraries show up under External Libraries as expected.

Is there something missing to force main.jar to show up under External Libraries , or is this a bug in AS?

It's a silly thing, but if you make it an Ivy or Maven repository, it should work. files don't. Both of the below solutions should support -sources and -javadoc suffixes. I think IDEA only implemented artifact resolution from repositories, and didn't think about direct file references.

Ivy

repositories {
    def repoRoot = file(rootProject.projectDir)
    ivy {
        name = "local libs"
        url = repoRoot.toURI()
        patternLayout {
            artifact("[module](-[classifier]).[ext]")
        }
        metadataSources {
            artifact()
        }
    }
}
dependencies {
    // `local` group and version `0` are just a hack so Gradle dependency notation can be used.
    implementation("local:main:0")
    implementation("local:core-debug:0@aar")
}

Maven

You can do something similar to above, but the repo structure is less flexible. You need to move the.jar/.aar files around. I recommend creating a folder for them (even if there's one right now). In the example I called it libs .

repositories {
    def repoRoot = file(rootProject.projectDir.resolve("libs"))
    exclusiveContent {
        // Work around
        // > Could not GET 'https://repo.gradle.org/gradle/libs-releases-local/local/main/0/main-0.pom'. Received status code 409 from server: Conflict
        // by not allowing Gradle to contact other repositories for "local" files.
        filter {
            includeGroup("local")
        }
        forRepository {
            maven {
                name = "local libs"
                url = repoRoot.toURI()
                metadataSources {
                    artifact()
                }
            }
        }
    }
}
dependencies {
    // `local` group and version `0` are just a hack so Gradle dependency notation can be used.
    implementation("local:main:0")
    implementation("local:core-debug:0@aar")
}

The error message will tell you where to put it, for example:

* What went wrong:
Execution failed for task ':...'.
> Could not resolve all files for configuration ':...'.
   > Could not find local:main:0.
     Searched in the following locations:
       - file:/.../libs/local/main/0/main-0.jar
     Required by:
         project :...
   > Could not find local:core-debug:0.
     Searched in the following locations:
       - file:/.../libs/local/core-debug/0/core-debug-0.aar
     Required by:
         project :...

Note that while this is more complex, it's also more flexible, because you can hand-write or download POM files and therefor include transitive dependencies for the local JAR files, just remove the metadataSources block if you have POM XML files for your artifacts.

metadataSources

This magic is worth a mention ( docs ):

metadataSources {
    artifact()
}

it tells Gradle, that there's no metadata (POM or Ivy XML file) associated with the artifacts in the repository, that there's only artifacts exists. Without this, it would fail by looking for metadata.

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