ModifyClassTransform.groovy 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. //delete all
  44. transformInvocation.outputProvider.deleteAll()
  45. // Collecting inputs.
  46. transformInvocation.inputs.each { input ->
  47. input.directoryInputs.each { dirInput ->
  48. if (transformInvocation.incremental) {
  49. dirInput.changedFiles.each { changedFile ->
  50. handleChangedFile(changedFile.key)
  51. }
  52. } else {
  53. dirInput.file.eachFileRecurse { File file ->
  54. handleChangedFile(file)
  55. }
  56. }
  57. //output modify class file
  58. File file = transformInvocation.outputProvider.getContentLocation(dirInput.file.name, dirInput.contentTypes, dirInput.scopes, Format.DIRECTORY)
  59. FileUtils.copyDirectory(dirInput.file, file)
  60. }
  61. input.jarInputs.each { jarInput ->
  62. switch (jarInput.status) {
  63. case Status.NOTCHANGED:
  64. if (transformInvocation.incremental) break
  65. case Status.ADDED:
  66. case Status.CHANGED:
  67. handleChangedFile(jarInput.file)
  68. }
  69. //output modify jar file
  70. File file = transformInvocation.outputProvider.getContentLocation(getUniqueFileName(jarInput.file), jarInput.contentTypes, jarInput.scopes, Format.JAR)
  71. FileUtils.copyFile(jarInput.file, file)
  72. }
  73. }
  74. }
  75. /**
  76. * 生成唯一文件名
  77. * @param fileInput
  78. * @return
  79. */
  80. protected String getUniqueFileName(File fileInput) {
  81. final String fileInputName = fileInput.getName()
  82. if (fileInput.isDirectory()) {
  83. return fileInputName
  84. }
  85. final String parentDirPath = fileInput.getParentFile().getAbsolutePath()
  86. final String pathMD5 = DigestUtils.md5Hex(parentDirPath.getBytes()).substring(0, 6)
  87. final int extSepPos = fileInputName.lastIndexOf('.')
  88. final String fileInputNamePrefix =
  89. (extSepPos >= 0 ? fileInputName.substring(0, extSepPos) : fileInputName)
  90. return fileInputNamePrefix + '_' + pathMD5
  91. }
  92. /**
  93. * logger
  94. * @param msg
  95. */
  96. protected void debug(def msg) {
  97. this.logger.lifecycle(":" + this.project.name + ":" + msg)
  98. }
  99. /**
  100. * 增量文件
  101. * @param input
  102. */
  103. protected abstract void handleChangedFile(File input)
  104. }