Javadoc生成"错误包不存在"在多模块项目中

时间:2016-12-10 13:48:08

标签: android javadoc gradle-dependencies

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

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

Android Studio Javadoc: Cannot find symbol

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

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

但在我的情况下,我的一个模块(Lib2)依赖于另一个模块(Lib1)。

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

两个模块都报告了{support}的错误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
    }
  }
}

1 个答案:

答案 0 :(得分:10)

我使用下面的代码段解决了这个问题

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

参考文献:

Adventures with Javadocs, part 2 (Introducing Android Framework Classes)

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