ModifyClassTransform.groovy 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 class ModifyClassTransform extends Transform {
  13. Project project;
  14. 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(getUniqueHashName(jarInput.file), jarInput.contentTypes, jarInput.scopes, Format.JAR)
  71. FileUtils.copyFile(jarInput.file, file)
  72. }
  73. }
  74. }
  75. /**
  76. * 增量文件
  77. * @param input
  78. */
  79. private void handleChangedFile(File input) {
  80. String path = input.absolutePath.replace("/", "\\")
  81. if (input.isFile()) {
  82. debug("changed:" + input.absolutePath)
  83. if (path.endsWith(".class")) {
  84. modifyClass(input)
  85. } else if (path.endsWith(".jar")) {
  86. if (path.contains("exploded-aar")) {
  87. if (!path.contains("com.android.support")) {
  88. //需要处理的jar包
  89. File tmp = new File(input.getParent() + File.separator + "tmp")
  90. tmp.deleteOnExit()
  91. boolean modify = false
  92. if (!modify) JarZipUtil.unzipJar(input, tmp.absolutePath)
  93. tmp.eachFileRecurse { File file ->
  94. if (file.isFile()) {
  95. if (modifyClass(file)) {
  96. modify = true
  97. }
  98. }
  99. }
  100. if (modify) JarZipUtil.zipJar(tmp.absolutePath, input)
  101. }
  102. }
  103. }
  104. }
  105. }
  106. /**
  107. * 修改类文件
  108. * @param clazz
  109. * @return
  110. */
  111. private boolean modifyClass(File clazz) {
  112. debug("modify:" + clazz.absolutePath)
  113. return false
  114. }
  115. /**
  116. * 生成唯一文件名
  117. * @param fileInput
  118. * @return
  119. */
  120. private String getUniqueHashName(File fileInput) {
  121. final String fileInputName = fileInput.getName()
  122. if (fileInput.isDirectory()) {
  123. return fileInputName
  124. }
  125. final String parentDirPath = fileInput.getParentFile().getAbsolutePath()
  126. final String pathMD5 = DigestUtils.md5Hex(parentDirPath.getBytes()).substring(0, 6)
  127. final int extSepPos = fileInputName.lastIndexOf('.')
  128. final String fileInputNamePrefix =
  129. (extSepPos >= 0 ? fileInputName.substring(0, extSepPos) : fileInputName)
  130. return fileInputNamePrefix + '_' + pathMD5
  131. }
  132. private void debug(def msg) {
  133. this.logger.lifecycle(":" + this.project.name + ":" + msg)
  134. }
  135. }