2017-10-18 93 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

回答