apktool.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "tools/apktool.h"
  2. #include "base/jarprocess.h"
  3. #include "base/application.h"
  4. #include "base/settings.h"
  5. #include "base/utils.h"
  6. #include <QFile>
  7. #include <QStringList>
  8. void Apktool::Decode::run()
  9. {
  10. emit started();
  11. QStringList arguments;
  12. arguments << "decode" << source;
  13. arguments << "--output" << destination;
  14. arguments << "--force";
  15. if (!frameworks.isEmpty()) {
  16. arguments << "--frame-path" << frameworks;
  17. }
  18. if (!resources) {
  19. arguments << "--no-res";
  20. }
  21. if (!sources) {
  22. arguments << "--no-src";
  23. }
  24. if (noDebugInfo) {
  25. arguments << "--no-debug-info";
  26. }
  27. if (onlyMainClasses) {
  28. arguments << "--only-main-classes";
  29. }
  30. if (keepBrokenResources) {
  31. arguments << "--keep-broken-res";
  32. }
  33. auto process = new JarProcess(this);
  34. connect(process, &JarProcess::finished, this, [=](bool success, const QString &output) {
  35. resultOutput = output;
  36. emit finished(success);
  37. process->deleteLater();
  38. });
  39. process->run(getPath(), arguments);
  40. }
  41. const QString &Apktool::Decode::output() const
  42. {
  43. return resultOutput;
  44. }
  45. void Apktool::Build::run()
  46. {
  47. emit started();
  48. QStringList arguments;
  49. arguments << "build" << source;
  50. arguments << "--output" << destination;
  51. arguments << "--force";
  52. if (!frameworks.isEmpty()) {
  53. arguments << "--frame-path" << frameworks;
  54. }
  55. if (aapt2) {
  56. arguments << "--use-aapt2";
  57. }
  58. if (debuggable) {
  59. arguments << "--debug";
  60. }
  61. auto process = new JarProcess(this);
  62. connect(process, &JarProcess::finished, this, [=](bool success, const QString &output) {
  63. resultOutput = output;
  64. emit finished(success);
  65. process->deleteLater();
  66. });
  67. process->run(getPath(), arguments);
  68. }
  69. const QString &Apktool::Build::output() const
  70. {
  71. return resultOutput;
  72. }
  73. void Apktool::InstallFramework::run()
  74. {
  75. emit started();
  76. QStringList arguments;
  77. arguments << "install-framework" << source;
  78. arguments << "--frame-path" << destination;
  79. auto process = new JarProcess(this);
  80. connect(process, &JarProcess::finished, this, [=](bool success, const QString &output) {
  81. resultOutput = output;
  82. emit finished(success);
  83. process->deleteLater();
  84. });
  85. process->run(getPath(), arguments);
  86. }
  87. const QString &Apktool::InstallFramework::output() const
  88. {
  89. return resultOutput;
  90. }
  91. void Apktool::Version::run()
  92. {
  93. emit started();
  94. auto process = new JarProcess(this);
  95. connect(process, &JarProcess::finished, this, [=](bool success, const QString &output) {
  96. if (success) {
  97. resultVersion = output;
  98. }
  99. emit finished(success);
  100. process->deleteLater();
  101. });
  102. process->run(getPath(), {"-version"});
  103. }
  104. const QString &Apktool::Version::version() const
  105. {
  106. return resultVersion;
  107. }
  108. void Apktool::reset()
  109. {
  110. auto versionCommand = new Version;
  111. app->connect(versionCommand, &Version::finished, [=]() {
  112. const QString currentVersion = versionCommand->version();
  113. const QString previousVersion = app->settings->getApktoolVersion();
  114. if (currentVersion.isNull() || currentVersion != previousVersion) {
  115. QFile::remove(getFrameworksPath() + "/1.apk");
  116. app->settings->setApktoolVersion(currentVersion);
  117. }
  118. versionCommand->deleteLater();
  119. });
  120. versionCommand->run();
  121. }
  122. QString Apktool::getPath()
  123. {
  124. const QString path = Utils::toAbsolutePath(app->settings->getApktoolPath());
  125. return !path.isEmpty() ? path : getDefaultPath();
  126. }
  127. QString Apktool::getDefaultPath()
  128. {
  129. return Utils::getSharedPath("tools/apktool.jar");
  130. }
  131. QString Apktool::getOutputPath()
  132. {
  133. const QString path = Utils::toAbsolutePath(app->settings->getOutputDirectory());
  134. return !path.isEmpty() ? path : getDefaultOutputPath();
  135. }
  136. QString Apktool::getDefaultOutputPath()
  137. {
  138. return Utils::getTemporaryPath("apk");
  139. }
  140. QString Apktool::getFrameworksPath()
  141. {
  142. const QString path = Utils::toAbsolutePath(app->settings->getFrameworksDirectory());
  143. return !path.isEmpty() ? path : getDefaultFrameworksPath();
  144. }
  145. QString Apktool::getDefaultFrameworksPath()
  146. {
  147. return Utils::getLocalConfigPath("frameworks");
  148. }