多模块项目中的 Javadoc 生成 "error package does not exist"

Javadoc generation "error package does not exist" in multi module project

我需要通过 Bintray 将我的库(多模块项目)上传到 sonatype 存储库。所以我需要为每个模块单独生成 Javadoc。

首先我遇到了这些类似问题中的问题

Gradle Javadoc fails with “error: package … does not exist”

从第一个 的解决方案中,我可以解决 类 我使用的第三方库(retrofit2,gson)和 @NonNull 注释错误。

但在我的例子中,我的一个模块(Lib2)依赖于另一个模块(Lib1)。

当为 Lib2 模块生成 Javadoc 时,它的警告 package does not exist & cannot find symbol 对于另一个模块 (Lib1) 的 类。

并且两个模块都报告 android 支持和设计库的 package does not exist 错误。

并且每个 import <packagename>.R; 报告 cannot find symbol

我的 Javadoc 生成任务

task androidJavadocs(type: Javadoc) {
      source = android.sourceSets.main.java.source
      classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
      ext.androidJar ="${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
      classpath += files(ext.androidJar)
      project.android.libraryVariants.all { variant ->
        classpath += files(variant.javaCompile.classpath.files)
      }
}

Lib1 中的依赖项build.gradle

compile rootProject.appcompat
compile rootProject.design
compile rootProject.retrofit2
compile rootProject.support
compile rootProject.gsonConverter
compile rootProject.httpLogInterceptor
compile rootProject.universalImageLoader

Lib2 中的依赖项build.gradle

compile project(':core')
compile rootProject.appcompat
compile rootProject.design
compile rootProject.retrofit2
compile rootProject.support
compile rootProject.cardview

我试过了

task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

afterEvaluate {
    androidJavadocs.classpath += project.android.libraryVariants.toList().first().javaCompile.classpath
}

我的整个gradle-mvn-push.gradle

apply plugin: 'maven'
apply plugin: 'com.jfrog.bintray'
// Declaring variables

afterEvaluate {  project ->
  bintray {
    user = bin_user
    key = bin_key
    configurations = ['archives']
    publish = true
    dryRun = false
    pkg {
      repo = REPO
      name = POM_ARTIFACT_ID
      userOrg = USER_ORG
      desc = POM_DESCRIPTION
      websiteUrl = POM_URL
      vcsUrl = GIT_URL
      licenses = ['Apache-2.0']
      version {
        name = VERSION_NAME
        vcsTag = VERSION_NAME
        gpg {
          sign = true
          try {
            passphrase = PASS_PHRASE
          } catch (Exception ignored) {
            println 'Failed to find credentials. GPG sign disabled'
          }
        }
      }
    }
  }

  if (project.getPlugins().hasPlugin('com.android.application') ||
          project.getPlugins().hasPlugin('com.android.library')) {
    task install(type: Upload, dependsOn: assemble) {
      repositories.mavenInstaller {
        configuration = configurations.archives

        pom.groupId = GROUP
        pom.artifactId = POM_ARTIFACT_ID
        pom.version = VERSION_NAME

        pom.project {
          name POM_NAME
          packaging POM_PACKAGING
          description POM_DESCRIPTION
          url POM_URL

          scm {
            url POM_SCM_URL
            connection POM_SCM_CONNECTION
            developerConnection POM_SCM_DEV_CONNECTION
          }

          licenses {
            license {
              name POM_LICENCE_NAME
              url POM_LICENCE_URL
              distribution POM_LICENCE_DIST
            }
          }

          developers {
            developer {
              id pom_developer_id
              name pom_developer_name
            }
          }

        }

        pom.withXml {
          def dependenciesNode = asNode().appendNode('dependencies')

          //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
          configurations.compile.allDependencies.each {
            def dependencyNode = dependenciesNode.appendNode('dependency')
            dependencyNode.appendNode('groupId', it.group)
            dependencyNode.appendNode('artifactId', it.name)
            dependencyNode.appendNode('version', it.version)
          }
        }
      }
    }

    task androidJavadocs(type: Javadoc) {
      source = android.sourceSets.main.java.source
      classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
      ext.androidJar ="${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
      classpath += files(ext.androidJar)
      project.android.libraryVariants.all { variant ->
        classpath += files(variant.javaCompile.classpath.files)
      }
    }

    task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
      classifier = 'javadoc'
      from androidJavadocs.destinationDir
    }

    task androidSourcesJar(type: Jar) {
      classifier = 'sources'
      from android.sourceSets.main.java.source
    }


  } else {
    install {
      repositories.mavenInstaller {
        pom.groupId = GROUP
        pom.artifactId = POM_ARTIFACT_ID
        pom.version = VERSION_NAME

        pom.project {
          name POM_NAME
          packaging POM_PACKAGING
          description POM_DESCRIPTION
          url POM_URL

          scm {
            url POM_SCM_URL
            connection POM_SCM_CONNECTION
            developerConnection POM_SCM_DEV_CONNECTION
          }

          licenses {
            license {
              name POM_LICENCE_NAME
              url POM_LICENCE_URL
              distribution POM_LICENCE_DIST
            }
          }

          developers {
            developer {
              id POM_DEVELOPER_ID
              name POM_DEVELOPER_NAME
            }
          }
        }
      }
    }

    task sourcesJar(type: Jar, dependsOn:classes) {
      classifier = 'sources'
      from sourceSets.main.allSource
    }

    task javadocJar(type: Jar, dependsOn:javadoc) {
      classifier = 'javadoc'
      from javadoc.destinationDir
    }
  }

  if (JavaVersion.current().isJava8Compatible()) {
    allprojects {
      tasks.withType(Javadoc) {
        options.addStringOption('Xdoclint:none', '-quiet')
      }
    }
  }

  artifacts {
    if (project.getPlugins().hasPlugin('com.android.application') ||
            project.getPlugins().hasPlugin('com.android.library')) {
      archives androidSourcesJar
      archives androidJavadocsJar
    } else {
      archives sourcesJar
      archives javadocJar
    }
  }
}

我使用以下代码段解决了问题

task androidJavadocs(type: Javadoc, dependsOn: 'assembleRelease') {
  classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
  android.libraryVariants.all { variant ->
    if (variant.name == 'release') {
      owner.classpath += variant.javaCompile.classpath
    }
  }
  source = android.sourceSets.main.java.srcDirs
  exclude '**/R.html', '**/R.*.html', '**/index.html'
}

但仍然 import <packagename>.R; 报告 cannot find symbol

参考文献:

Javadoc 冒险,第 2 部分 (介绍 Android 框架 类)

Adventures with Javadocs, part 3 (Introducing Classes From Third Party Dependencies)