AidlCompile.gradle 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. apply from: "$rootDir/Script/FrameworkConfig.gradle"
  2. gradle.projectsEvaluated {
  3. def libraryExtension = getExtensions().findByName("android")
  4. if (libraryExtension == null) {
  5. return
  6. }
  7. // 获取aidl源码路径
  8. // TODO 这里没有区分debug还是release,可能会有问题,遇到问题再修改
  9. def aidlSrcDirs = new HashSet<String>()
  10. for (sourceSets in libraryExtension.sourceSets) {
  11. def aidl = sourceSets.aidl
  12. if (aidl.name.contains("android test") || aidl.name.contains("test ")) {
  13. continue
  14. }
  15. aidlSrcDirs.addAll(aidl.srcDirs)
  16. }
  17. // 拼接aidl编译参数
  18. // -I DIR, --include=DIR
  19. // 指定依赖的aidl目录
  20. String aidlInclude = ""
  21. for (String dir : aidlSrcDirs) {
  22. aidlInclude += "-I$dir "
  23. }
  24. aidlInclude = aidlInclude.trim()
  25. // 获取编译aidl的Task,一般是compileDebugAidl和compileReleaseAidl
  26. def aidlCompile = new HashSet<Task>()
  27. for (task in tasks.collect()) {
  28. def taskName = task.name
  29. if (taskName.contains("AndroidTest")) {
  30. continue
  31. }
  32. if (taskName.toLowerCase().contains("aidl")) {
  33. aidlCompile.add(task)
  34. }
  35. }
  36. def frameworksAidl = "$frameworkAidlPath"
  37. if (frameworksAidl == null || frameworksAidl.isBlank()) {
  38. println("frameworksAidl is null")
  39. return
  40. }
  41. for (aidlTask in aidlCompile) {
  42. aidlTask.doFirst { task ->
  43. def buildTools = task.buildTools
  44. def aidlExecutable = buildTools.aidlExecutableProvider().get()
  45. def aidlFrameworkProvider = buildTools.aidlFrameworkProvider().get()
  46. def aidlSourceOutputDir = null
  47. for (File file : task.getOutputs().files.files) {
  48. if (file.absolutePath.contains("aidl_source_output_dir")) {
  49. aidlSourceOutputDir = file
  50. }
  51. }
  52. for (File file : task.getInputs().files.files) {
  53. if (file.isDirectory()) {
  54. continue
  55. }
  56. if (file.name == "framework.aidl") {
  57. continue
  58. }
  59. // aidl编译命令
  60. // -p FILE, --preprocessed=FILE
  61. // Include FILE which is created by --preprocess.
  62. // -d FILE, --dep=FILE
  63. // Generate dependency file as FILE. Don't use this when
  64. // there are multiple input files. Use -a then.
  65. // -o DIR, --out=DIR aidl编译生成的Java文件所在目录
  66. String cmd = String.format("%s -p%s -I%s %s -o%s %s", aidlExecutable,
  67. aidlFrameworkProvider, frameworksAidl, aidlInclude, aidlSourceOutputDir, file)
  68. runtimeExec(cmd)
  69. }
  70. // 结束自带的aidl编译Task
  71. throw new StopExecutionException()
  72. }
  73. }
  74. }