ModifyClassTransform.groovy 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package com.gradle
  2. import com.android.build.api.transform.*
  3. import com.google.common.collect.ImmutableSet
  4. import org.apache.commons.codec.digest.DigestUtils
  5. import org.apache.commons.io.FileUtils
  6. import org.gradle.api.Project
  7. import org.gradle.api.logging.Logger
  8. import org.gradle.api.logging.Logging
  9. /**
  10. * Created by Glen on 2017/2/7.
  11. */
  12. public abstract class ModifyClassTransform extends Transform {
  13. protected Project project;
  14. protected Logger logger;
  15. public ModifyClassTransform(Project project) {
  16. this.project = project
  17. this.logger = Logging.getLogger(ModifyClassTransform.class)
  18. }
  19. @Override
  20. public String getName() {
  21. return "Modify"
  22. }
  23. @Override
  24. public Set<QualifiedContent.ContentType> getInputTypes() {
  25. return ImmutableSet.of(QualifiedContent.DefaultContentType.CLASSES)
  26. }
  27. @Override
  28. public Set<QualifiedContent.Scope> getScopes() {
  29. return ImmutableSet.of(
  30. QualifiedContent.Scope.PROJECT,// app class
  31. QualifiedContent.Scope.PROJECT_LOCAL_DEPS,// app libs local jar
  32. QualifiedContent.Scope.SUB_PROJECTS,//sub lib class
  33. QualifiedContent.Scope.SUB_PROJECTS_LOCAL_DEPS,//sub lib libs local jar
  34. QualifiedContent.Scope.EXTERNAL_LIBRARIES//compile dependencies
  35. )
  36. }
  37. @Override
  38. public boolean isIncremental() {
  39. return true
  40. }
  41. @Override
  42. void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
  43. long time = System.currentTimeMillis()
  44. //delete all
  45. transformInvocation.outputProvider.deleteAll()
  46. // Collecting inputs.
  47. transformInvocation.inputs.each { input ->
  48. input.directoryInputs.each { dirInput ->
  49. if (transformInvocation.incremental) {
  50. // 待修复bug
  51. // dirInput.changedFiles.each { changedFile ->
  52. // handleChangedFile(changedFile.key)
  53. // }
  54. dirInput.file.eachFileRecurse { File file ->
  55. handleChangedFile(file)
  56. }
  57. } else {
  58. dirInput.file.eachFileRecurse { File file ->
  59. handleChangedFile(file)
  60. }
  61. }
  62. //output modify class file
  63. File file = transformInvocation.outputProvider.getContentLocation(dirInput.file.name, dirInput.contentTypes, dirInput.scopes, Format.DIRECTORY)
  64. FileUtils.copyDirectory(dirInput.file, file)
  65. }
  66. input.jarInputs.each { jarInput ->
  67. switch (jarInput.status) {
  68. case Status.NOTCHANGED:
  69. if (transformInvocation.incremental) break
  70. case Status.ADDED:
  71. case Status.CHANGED:
  72. handleChangedFile(jarInput.file)
  73. }
  74. //output modify jar file
  75. File file = transformInvocation.outputProvider.getContentLocation(getUniqueFileName(jarInput.file), jarInput.contentTypes, jarInput.scopes, Format.JAR)
  76. FileUtils.copyFile(jarInput.file, file)
  77. }
  78. }
  79. this.project.logger.error(":" + this.project.name + ":" + getName() + " consume time " + (System.currentTimeMillis() - time) + "ms")
  80. }
  81. /**
  82. * 生成唯一文件名
  83. * @param fileInput
  84. * @return
  85. */
  86. protected String getUniqueFileName(File fileInput) {
  87. final String fileInputName = fileInput.getName()
  88. if (fileInput.isDirectory()) {
  89. return fileInputName
  90. }
  91. final String parentDirPath = fileInput.getParentFile().getAbsolutePath()
  92. final String pathMD5 = DigestUtils.md5Hex(parentDirPath.getBytes()).substring(0, 6)
  93. final int extSepPos = fileInputName.lastIndexOf('.')
  94. final String fileInputNamePrefix =
  95. (extSepPos >= 0 ? fileInputName.substring(0, extSepPos) : fileInputName)
  96. return fileInputNamePrefix + '_' + pathMD5
  97. }
  98. /**
  99. * logger
  100. * @param msg
  101. */
  102. protected void debug(def msg) {
  103. //this.logger.lifecycle(":" + this.project.name + ":" + msg)
  104. }
  105. /**
  106. * 增量文件
  107. * @param input
  108. */
  109. protected abstract void handleChangedFile(File input)
  110. }