2017-10-18 89 views
4

背景:运行Android Studio 3.0-beta7并尝试获取javadoc任务以用于Android库(事实上,这不适用于首先一个现成的任务是很奇怪的),我想办法调整的答案我需要一个不同的问题,与此代码(https://stackoverflow.com/a/46810617/1226020)结束了:如何使用“api”或“implementation”指令从gradle插件获取依赖关系

task javadoc(type: Javadoc) { 
    failOnError false 
    source = android.sourceSets.main.java.srcDirs 
    // Also add the generated R class to avoid errors... 
    // TODO: debug is hard-coded 
    source += "$buildDir/generated/source/r/debug/" 
    // ... but exclude the R classes from the docs 
    excludes += "**/R.java" 

    // TODO: "compile" is deprecated in Gradle 4.1, 
    // but "implementation" and "api" are not resolvable :(
    classpath += configurations.compile 

    afterEvaluate { 
     // Wait after evaluation to add the android classpath 
     // to avoid "buildToolsVersion is not specified" error 
     classpath += files(android.getBootClasspath()) 

     // Process AAR dependencies 
     def aarDependencies = classpath.filter { it.name.endsWith('.aar') } 
     classpath -= aarDependencies 
     aarDependencies.each { aar -> 
      System.out.println("Adding classpath for aar: " + aar.name) 
      // Extract classes.jar from the AAR dependency, and add it to the javadoc classpath 
      def outputPath = "$buildDir/tmp/exploded-aar/${aar.name.replace('.aar', '.jar')}" 
      classpath += files(outputPath) 

      // Use a task so the actual extraction only happens before the javadoc task is run 
      dependsOn task(name: "extract ${aar.name}").doLast { 
       extractEntry(aar, 'classes.jar', outputPath) 
      } 
     } 
    } 
} 

// Utility method to extract only one entry in a zip file 
private def extractEntry(archive, entryPath, outputPath) { 
    if (!archive.exists()) { 
     throw new GradleException("archive $archive not found") 
    } 

    def zip = new java.util.zip.ZipFile(archive) 

    zip.entries().each { 
     if (it.name == entryPath) { 
      def path = new File(outputPath) 

      if (!path.exists()) { 
       path.getParentFile().mkdirs() 

       // Surely there's a simpler is->os utility except 
       // the one in java.nio.Files? Ah well... 
       def buf = new byte[1024] 
       def is = zip.getInputStream(it) 
       def os = new FileOutputStream(path) 
       def len 

       while ((len = is.read(buf)) != -1) { 
        os.write(buf, 0, len) 
       } 
       os.close() 
      } 
     } 
    } 
    zip.close() 
} 

此代码试图找到所有依赖项AAR:s循环遍历它们并从中提取classes.jar,并将它们放入临时文件夹中,该文件夹在javadoc生成期间添加到类路径中。基本上试图重现真正的旧版android gradle插件用于“爆炸 - 空气”的做法。

但是,代码依赖于使用compile依赖关系。在Gradle 4.1中推荐使用apiimplementation将不起作用,因为这些无法从Gradle任务中解析。

问题:如何在例如使用apiimplementation指令时获得依赖项列表。 configuration.api呈现“不可解析”错误?

奖金问题:是否有一种新的更好的方式来为Android Studio 3.0库创建javadoc,而不涉及100行解决方法?

+1

如果你用'project.configurations.getByName(“archives”)'替换'configurations.compile',那么你能够使用'implementation'而不是'compile'来声明你的依赖吗?它似乎为我工作,虽然这是一个谜。 我刚刚在我的gradle文件中插入了这个调试:'project.configurations.each {println it.name}'查看所有配置,并开始逐个尝试'project.configurations.getByName()',直到找到似乎工作的一个:“档案”。 – Carmen

回答