parsers.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package aapt
  2. import (
  3. "strconv"
  4. "strings"
  5. "notabug.org/Umnik/GoAndroidSDK/v2/components/misc"
  6. )
  7. // ParseAppInfoDumpFromBytes returns AppInfo from raw input bytes
  8. func ParseAppInfoDumpFromBytes(rawOutput []byte) AppInfo {
  9. return ParseAppInfoDumpFromString(string(rawOutput))
  10. }
  11. // ParseAppInfoDumpFromString returns AppInfo from raw input string
  12. func ParseAppInfoDumpFromString(stringOutput string) AppInfo {
  13. appInfoResult := AppInfo{
  14. AppLabel: map[string]string{},
  15. NativeCode: map[string]struct{}{},
  16. MetaData: map[string]string{},
  17. IsSplitBase: false,
  18. }
  19. for _, rawLine := range strings.Split(stringOutput, "\n") {
  20. rawLine = strings.TrimSpace(rawLine)
  21. switch {
  22. case strings.HasPrefix(rawLine, "package:"):
  23. values := processPackageLine(rawLine)
  24. appInfoResult.PackageName = values[0]
  25. appInfoResult.VersionCode = misc.IntExtractor(values[1])
  26. appInfoResult.VersionName = values[2]
  27. case strings.HasPrefix(rawLine, "sdkVersion"):
  28. appInfoResult.MinSDKVersion = misc.IntExtractor(misc.TwoSpotRexValue(misc.RemoveQuoteMark(rawLine)))
  29. case strings.HasPrefix(rawLine, "targetSdkVersion"):
  30. appInfoResult.TargetSDKVersion = misc.IntExtractor(misc.TwoSpotRexValue(misc.RemoveQuoteMark(rawLine)))
  31. case strings.HasPrefix(rawLine, "uses-permission: name="):
  32. if strings.Contains(rawLine, "android.permission.") {
  33. appInfoResult.UsesAndroidPermission = append(
  34. appInfoResult.UsesAndroidPermission, extractPermission(rawLine),
  35. )
  36. } else {
  37. appInfoResult.UsesCustomPermission = append(
  38. appInfoResult.UsesCustomPermission, extractPermission(rawLine),
  39. )
  40. }
  41. case strings.HasPrefix(rawLine, "application-label"):
  42. key, value := extractLabel(rawLine)
  43. appInfoResult.AppLabel[key] = value
  44. case strings.HasPrefix(rawLine, "launchable-activity"):
  45. appInfoResult.LaunchActivity = append(appInfoResult.LaunchActivity, extractLaunchActivity(rawLine))
  46. case strings.HasPrefix(rawLine, "native-code"):
  47. appInfoResult.NativeCode = extractNativeCode(rawLine)
  48. case strings.HasPrefix(rawLine, "application-debuggable"):
  49. appInfoResult.Debuggable = true
  50. case strings.HasPrefix(rawLine, "testOnly="):
  51. appInfoResult.TestOnly = true
  52. case strings.HasPrefix(rawLine, "meta-data:"):
  53. res := extractMetaData(rawLine)
  54. appInfoResult.MetaData[res[0]] = res[1]
  55. if res[0] == "com.android.vending.splits.required" {
  56. appInfoResult.IsSplitBase = true
  57. }
  58. }
  59. }
  60. return appInfoResult
  61. }
  62. // ParseDumpResourcesFromBytes returns Resources from raw input bytes
  63. func ParseDumpResourcesFromBytes(rawOutput []byte) Resources {
  64. return ParseDumpResourcesFromString(string(rawOutput))
  65. }
  66. // ParseDumpResourcesFromString returns Resources from raw input string
  67. func ParseDumpResourcesFromString(stringOutput string) Resources {
  68. out := strings.Split(stringOutput, "\n")
  69. resourcesResult := Resources{
  70. Array: map[string][][2]string{},
  71. String: map[string][][2]string{},
  72. }
  73. last := 0
  74. for i, line := range out {
  75. if i < last {
  76. continue
  77. }
  78. line = strings.TrimSpace(line)
  79. if !strings.HasPrefix(line, "resource") {
  80. continue
  81. }
  82. if strings.Contains(line, "android:array/") {
  83. val, err := strconv.Atoi(strings.SplitN(out[i+1], "=", 4)[3])
  84. if err != nil {
  85. panic(err)
  86. }
  87. last = i + val + 2
  88. resourcesResult.Array[resArrName(line)] = resArrValues(out[i+2 : last])
  89. continue
  90. }
  91. if strings.Contains(line, "android:string/") {
  92. paramName := resStringName(line)
  93. last = i + 1
  94. resourcesResult.String[paramName] = append(resourcesResult.String[paramName], resStringValue(out[last]))
  95. continue
  96. }
  97. }
  98. return resourcesResult
  99. }