FrameworkConfig.gradle 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * 用于下载framework jar包和framework aidl文件
  3. * 下载用到git中的sparse-checkout命令,请保证当前环境中的git版本支持此命令
  4. * 如不支持,请将git升级到最新版本2.45.1版本或最新版本
  5. */
  6. import java.nio.file.Path
  7. ext {
  8. getFrameworkJarPath = (gradleUserHomeDir, sdkInt) -> getFrameworkJarPathImpl(gradleUserHomeDir, sdkInt)
  9. runtimeExec = (cmd) -> runtimeExecImpl(cmd)
  10. getFrameworkAidlPath = (gradleUserHomeDir, tag) -> getFrameworkAidlPathImpl(gradleUserHomeDir, tag)
  11. }
  12. private static String getFrameworkJarPathImpl(File gradleUserHomeDir, String sdkInt) {
  13. // framework jar包下载地址
  14. String FRAMEWORK_JAR_URL = "https://gitcode.com/Reginer/aosp-android-jar.git"
  15. String FRAMEWORK_JAR_DIR_NAME = "framework-jar-$sdkInt"
  16. File cachesDir = new File(gradleUserHomeDir, "caches")
  17. File frameworkJarDir = new File(cachesDir, FRAMEWORK_JAR_DIR_NAME)
  18. String frameworkJarPath = Path.of(frameworkJarDir.getAbsolutePath(), "aosp-android-jar",
  19. "android-$sdkInt", "android.jar").toString()
  20. File frameworkJar = new File(frameworkJarPath)
  21. println("frameworkJarPath = $frameworkJarPath")
  22. if (frameworkJar.exists()) {
  23. return frameworkJarPath
  24. }
  25. if (!frameworkJarDir.exists()) {
  26. if (!frameworkJarDir.mkdirs()) {
  27. throw new RuntimeException(String.format("mkdirs error %s", frameworkJarDir))
  28. }
  29. }
  30. File baseDir = new File(frameworkJarDir, "aosp-android-jar")
  31. File gitDir = new File(baseDir, ".git")
  32. if (gitDir.exists()) {
  33. deleteDir(gitDir)
  34. }
  35. try {
  36. println("exec git:")
  37. runtimeExecImpl("git -C $frameworkJarDir clone --filter=blob:none --sparse --depth 1 -b main --single-branch --no-checkout $FRAMEWORK_JAR_URL")
  38. runtimeExecImpl("git -C $baseDir sparse-checkout init --no-cone")
  39. runtimeExecImpl("git -C $baseDir sparse-checkout set !/*")
  40. runtimeExecImpl("git -C $baseDir sparse-checkout add android-$sdkInt/android.jar")
  41. runtimeExecImpl("git -C $baseDir checkout main")
  42. } catch (Exception ex) {
  43. throw new RuntimeException(ex)
  44. }
  45. return frameworkJarPath
  46. }
  47. private static String getFrameworkAidlPathImpl(File gradleUserHomeDir, String tag) {
  48. final String PLATFORM_URL = "https://mirrors.tuna.tsinghua.edu.cn/git/AOSP/platform/"
  49. final String FRAMEWORKS_BASE_URL = "${PLATFORM_URL}frameworks/base"
  50. final String FRAMEWORK_AIDL_DIR_NAME = "framework-aidl_$tag"
  51. File cachesDir = new File(gradleUserHomeDir, "caches")
  52. File frameworkAidlDir = new File(cachesDir, FRAMEWORK_AIDL_DIR_NAME)
  53. String frameworkAidlPath = Path.of(frameworkAidlDir.getAbsolutePath(), "base",
  54. "core", "java").toString()
  55. File frameworkAidl = new File(frameworkAidlPath)
  56. println("frameworkAidlPath = $frameworkAidlPath")
  57. if (frameworkAidl.exists()) {
  58. return frameworkAidl
  59. }
  60. if (!frameworkAidlDir.exists()) {
  61. if (!frameworkAidlDir.mkdirs()) {
  62. throw new RuntimeException(String.format("mkdirs error %s", frameworkAidlDir))
  63. }
  64. }
  65. def baseDir = new File(frameworkAidlDir, "base")
  66. def gitDir = new File(baseDir, ".git")
  67. if (gitDir.exists()) {
  68. deleteDir(gitDir)
  69. }
  70. try {
  71. println("exec git:")
  72. runtimeExecImpl("git -C $frameworkAidlDir clone --filter=blob:none --sparse --depth 1 -b $tag --single-branch --no-checkout $FRAMEWORKS_BASE_URL")
  73. runtimeExecImpl("git -C $baseDir sparse-checkout init --no-cone")
  74. runtimeExecImpl("git -C $baseDir sparse-checkout set !/*")
  75. runtimeExecImpl("git -C $baseDir sparse-checkout add /core/java/**/*.aidl")
  76. runtimeExecImpl("git -C $baseDir checkout -b $tag $tag")
  77. } catch (Exception ex) {
  78. throw new RuntimeException(ex)
  79. }
  80. return frameworkAidlPath
  81. }
  82. private static void runtimeExecImpl(String cmd) throws Exception {
  83. println("$cmd")
  84. Runtime runtime = Runtime.getRuntime()
  85. Process process = runtime.exec(cmd)
  86. String result = readInputStream(process.getInputStream())
  87. String errorMsg = readInputStream(process.getErrorStream())
  88. if (process.waitFor() == 0) {
  89. if (!result.isEmpty()) {
  90. println("$result")
  91. }
  92. if (!errorMsg.isEmpty()) {
  93. println("$errorMsg")
  94. }
  95. } else {
  96. if (!result.isEmpty()) {
  97. println("$result")
  98. }
  99. if (!errorMsg.isEmpty()) {
  100. println("$errorMsg")
  101. }
  102. throw new RuntimeException("exec error")
  103. }
  104. }
  105. private static String readInputStream(InputStream input) {
  106. StringBuilder sb = new StringBuilder()
  107. try(BufferedReader br = new BufferedReader(new InputStreamReader(input))) {
  108. String line
  109. while ((line = br.readLine()) != null) {
  110. sb.append(line).append(System.lineSeparator())
  111. }
  112. } catch (Throwable ex) {
  113. ex.printStackTrace(System.err)
  114. }
  115. return sb.toString().trim()
  116. }
  117. private static void deleteDir(File dir) {
  118. if (dir.isDirectory()) {
  119. File[] files = dir.listFiles()
  120. if (files == null) {
  121. return
  122. }
  123. for (File file : files) {
  124. if (file.isFile()) {
  125. file.delete()
  126. } else {
  127. deleteDir(file)
  128. }
  129. }
  130. }
  131. dir.delete()
  132. }