proguard.html 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta name="generator" content="Asciidoctor 2.0.15">
  8. <meta name="author" content="pxzxj, pudge.zxj@gmail.com, 2022/02/19">
  9. <title>ProGuard Reference</title>
  10. <link rel="stylesheet" href="css/site.css">
  11. <link href="css/custom.css" rel="stylesheet">
  12. <script src="js/setup.js"></script><script defer src="js/site.js"></script>
  13. </head>
  14. <body class="article toc2 toc-left"><div id="banner-container" class="container" role="banner">
  15. <div id="banner" class="contained" role="banner">
  16. <div id="switch-theme">
  17. <input type="checkbox" id="switch-theme-checkbox" />
  18. <label for="switch-theme-checkbox">Dark Theme</label>
  19. </div>
  20. </div>
  21. </div>
  22. <div id="tocbar-container" class="container" role="navigation">
  23. <div id="tocbar" class="contained" role="navigation">
  24. <button id="toggle-toc"></button>
  25. </div>
  26. </div>
  27. <div id="main-container" class="container">
  28. <div id="main" class="contained">
  29. <div id="doc" class="doc">
  30. <div id="header">
  31. <h1>ProGuard Reference</h1>
  32. <div class="details">
  33. <span id="author" class="author">pxzxj</span><br>
  34. <span id="author2" class="author">pudge.zxj@gmail.com</span><br>
  35. <span id="author3" class="author">2022/02/19</span><br>
  36. </div>
  37. <div id="toc" class="toc2">
  38. <div id="toctitle">Table of Contents</div>
  39. <span id="back-to-index"><a href="index.html">Back to index</a></span><ul class="sectlevel1">
  40. <li><a href="#_简介">1. 简介</a>
  41. <ul class="sectlevel2">
  42. <li><a href="#_基本功能">1.1. 基本功能</a></li>
  43. <li><a href="#_入口点entry_points">1.2. 入口点(Entry Points)</a></li>
  44. <li><a href="#_反射reflection">1.3. 反射(Reflection)</a></li>
  45. </ul>
  46. </li>
  47. <li><a href="#_用法">2. 用法</a>
  48. <ul class="sectlevel2">
  49. <li><a href="#_下载">2.1. 下载</a></li>
  50. <li><a href="#_参数">2.2. 参数</a>
  51. <ul class="sectlevel3">
  52. <li><a href="#_输入输出选项inputoutput_options">2.2.1. 输入输出选项(Input/Output Options)</a></li>
  53. <li><a href="#_保持选项keep_option">2.2.2. 保持选项(keep Option)</a></li>
  54. <li><a href="#_压缩选项shrinking_options">2.2.3. 压缩选项(Shrinking Options)</a></li>
  55. <li><a href="#_优化选项optimization_options">2.2.4. 优化选项(Optimization Options)</a></li>
  56. <li><a href="#_通用选项general_options">2.2.5. 通用选项(General options)</a></li>
  57. </ul>
  58. </li>
  59. <li><a href="#_示例">2.3. 示例</a></li>
  60. <li><a href="#_常见错误及原因">2.4. 常见错误及原因</a></li>
  61. </ul>
  62. </li>
  63. <li><a href="#_实践">3. 实践</a>
  64. <ul class="sectlevel2">
  65. <li><a href="#_入口点分析">3.1. 入口点分析</a></li>
  66. <li><a href="#_编写配置文件">3.2. 编写配置文件</a></li>
  67. <li><a href="#_其它说明">3.3. 其它说明</a></li>
  68. </ul>
  69. </li>
  70. </ul>
  71. </div>
  72. </div>
  73. <div id="content">
  74. <div class="sect1">
  75. <h2 id="_简介"><a class="anchor" href="#_简介"></a>1. 简介</h2>
  76. <div class="sectionbody">
  77. <div class="sect2">
  78. <h3 id="_基本功能"><a class="anchor" href="#_基本功能"></a>1.1. 基本功能</h3>
  79. <div class="paragraph">
  80. <p>ProGuard是一个压缩、优化和混淆Java字节码文件的免费的工具,压缩步骤它会检测并移除不会使用的类、方法、字段和class属性,优化步骤会分析和优化方法的字节码,以更高效的语法实现原有逻辑,混淆步骤使用简短无意义的字母序列重命名类、方法、字段;这三步完成后还会对生成的字节码做预验证并添加预验证信息保证文件格式正确。最终处理后的字节码比原文件小但执行更高效且很难反向工程。</p>
  81. </div>
  82. <div class="paragraph">
  83. <p>ProGuard的处理流程可以用下图来表示,通常将待混淆的所有类打成jar包进行处理,最终也输出为jar包,需要注意的是整个处理过程需要依赖库的参与,如常见的WEB-INF/lib下的所有jar包或者pom.xml中的所有dependency,也就是下图中的Library jars,依赖库在处理前后不发生变化。</p>
  84. </div>
  85. <div class="imageblock">
  86. <div class="content">
  87. <img src="images/proguard.png" alt="proguard">
  88. </div>
  89. </div>
  90. </div>
  91. <div class="sect2">
  92. <h3 id="_入口点entry_points"><a class="anchor" href="#_入口点entry_points"></a>1.2. 入口点(Entry Points)</h3>
  93. <div class="paragraph">
  94. <p>为了告知ProGuard哪些代码是需要保留的哪些是需要丢弃或者混淆的,你需要指定一些入口点,典型的入口点包括main方法、Servlet、Controller等。<br>
  95. 压缩步骤中,ProGuard从这些入口点出发,根据方法和属性的调用关系确定其它需要保留的类或属性,最终未被引用的类或成员被移除。<br>
  96. 优化步骤中,ProGuard尽力优化代码,上一步中保留下来的类或方法除入口点外会被声明为final,未使用的方法参数会被移除,多个方法的逻辑可能会合并到一个方法内实现。<br>
  97. 混淆步骤中,ProGuard重命名类和成员,主要针对上一步中保留下来的除入口点外的类和成员,通常是使用一个简短的英文字母进行重命名,当然也可以手动指定重命名前后的名称映射关系。</p>
  98. </div>
  99. </div>
  100. <div class="sect2">
  101. <h3 id="_反射reflection"><a class="anchor" href="#_反射reflection"></a>1.3. 反射(Reflection)</h3>
  102. <div class="paragraph">
  103. <p>反射是Java语言的强大特性之一,但也经常会带来一些特殊问题,一个常见场景是将全类名声明在配置文件中,在代码中使用Class.forName()生成实例对象,为了保证对应的类不会在压缩步骤中被删除必须将其声明为入口点。
  104. 然而ProGuard也能自动探测并处理下面几种反射语法:</p>
  105. </div>
  106. <div class="ulist">
  107. <ul>
  108. <li>
  109. <p>Class.forName("SomeClass")</p>
  110. </li>
  111. <li>
  112. <p>SomeClass.class</p>
  113. </li>
  114. <li>
  115. <p>SomeClass.class.getField("someField")</p>
  116. </li>
  117. <li>
  118. <p>SomeClass.class.getDeclaredField("someField")</p>
  119. </li>
  120. <li>
  121. <p>SomeClass.class.getMethod("someMethod", null)</p>
  122. </li>
  123. <li>
  124. <p>SomeClass.class.getMethod("someMethod", new Class[] { A.class,&#8230;&#8203; })</p>
  125. </li>
  126. <li>
  127. <p>SomeClass.class.getDeclaredMethod("someMethod", null)</p>
  128. </li>
  129. <li>
  130. <p>SomeClass.class.getDeclaredMethod("someMethod", new Class[] { A.class,&#8230;&#8203; })</p>
  131. </li>
  132. <li>
  133. <p>AtomicIntegerFieldUpdater.newUpdater(SomeClass.class, "someField")</p>
  134. </li>
  135. <li>
  136. <p>AtomicLongFieldUpdater.newUpdater(SomeClass.class, "someField")</p>
  137. </li>
  138. <li>
  139. <p>AtomicReferenceFieldUpdater.newUpdater(SomeClass.class, SomeType.class, "someField")</p>
  140. </li>
  141. </ul>
  142. </div>
  143. <div class="paragraph">
  144. <p>上述示例虽然使用了反射,但类名或者方法名、属性名都已经确定,ProGuard会识别这些类或成员进行保留,在混淆步骤中如果对应类或成员被重命名,这些反射代码也被相应更新。</p>
  145. </div>
  146. </div>
  147. </div>
  148. </div>
  149. <div class="sect1">
  150. <h2 id="_用法"><a class="anchor" href="#_用法"></a>2. 用法</h2>
  151. <div class="sectionbody">
  152. <div class="sect2">
  153. <h3 id="_下载"><a class="anchor" href="#_下载"></a>2.1. 下载</h3>
  154. <div class="paragraph">
  155. <p>在 <a href="https://sourceforge.net/projects/proguard/">sourceforge</a> 页面中点击Download下载ProGuard
  156. 解压后的文件夹中bin目录有proguard.bat和proguardgui.bat两个脚本可以在windows下运行, 前者是在命令行下运行,后者可以打开一个图形界面;类似的有Unix环境下运行的.sh脚本</p>
  157. </div>
  158. </div>
  159. <div class="sect2">
  160. <h3 id="_参数"><a class="anchor" href="#_参数"></a>2.2. 参数</h3>
  161. <div class="paragraph">
  162. <p>要用ProGuard处理一个web项目需要用到它的诸多参数,还要指定多个类和对应的方法,这些配置内容较多所以通常会将其写在一个文件中供ProGuard使用,最终执行命令如下,</p>
  163. </div>
  164. <div class="listingblock">
  165. <div class="content">
  166. <pre class="highlight"><code class="language-bash" data-lang="bash">bin/proguard @myconfig.pro</code></pre>
  167. </div>
  168. </div>
  169. <div class="paragraph">
  170. <p>其中 <code>myconfig.pro</code> 就是包含所有配置的文件,文件中可以按行指定不同的配置参数,#开头的行为注释信息。<br>
  171. ProGuard的参数主要可分为如下几组, 这部分建议访参考 <a href="https://www.guardsquare.com/en/products/proguard/manual/usage">官方网站</a></p>
  172. </div>
  173. <div class="sect3">
  174. <h4 id="_输入输出选项inputoutput_options"><a class="anchor" href="#_输入输出选项inputoutput_options"></a>2.2.1. 输入输出选项(Input/Output Options)</h4>
  175. <div class="dlist">
  176. <dl>
  177. <dt class="hdlist1">@filename </dt>
  178. <dd>
  179. <p>-include filename的缩写形式</p>
  180. </dd>
  181. <dt class="hdlist1">-include filename </dt>
  182. <dd>
  183. <p>读取文件中所有选项, filename为绝对路径或相对路径, 相对路径可以和下面的-basedirectory配合使用<br>
  184. filename也能以尖括号的方式使用Java系统属性, 如&lt;java.home&gt;/lib/rt.jar, &lt;user.home&gt;指用户家目录, &lt;user.dir&gt;表示当前目录</p>
  185. </dd>
  186. <dt class="hdlist1">-basedirectory directoryname </dt>
  187. <dd>
  188. <p>指定文件所在的目录名, 此参数必须在-include前使用</p>
  189. </dd>
  190. <dt class="hdlist1">-injars class_path </dt>
  191. <dd>
  192. <p>class_path由多个个体(entrty)组成, 每个个体可以是class文件的任何表示形式, 如常见的class目录、apk文件、jar文件、aar文件、war文件、ear文件、jmod文件、zip文件, 多个个体使用路径分隔符分隔(Unix使用冒号:,Windows使用分号;), entry的顺序决定了它的优先级</p>
  193. </dd>
  194. <dt class="hdlist1">-outjars class_path </dt>
  195. <dd>
  196. <p>同上, 多个injar可以聚合为一个outjar, 同样一个injar也可以通过filter拆分为多个outjar</p>
  197. </dd>
  198. <dt class="hdlist1">-libraryjars class_path </dt>
  199. <dd>
  200. <p>injar需要依赖的jar包</p>
  201. </dd>
  202. <dt class="hdlist1">-skipnonpubliclibraryclasses </dt>
  203. <dd>
  204. <p>跳过-libraryjars中非public的类, 这样可以提升处理速度和减少Proguard的内存占用。 默认Proguard会读取-libraryjars中所有的类, 但通常非public的类不会和injars中的类直接相关</p>
  205. </dd>
  206. <dt class="hdlist1">-dontskipnonpubliclibraryclasses </dt>
  207. <dd>
  208. <p>4.5版本后这是默认设置</p>
  209. </dd>
  210. <dt class="hdlist1">-dontskipnonpubliclibraryclassmembers </dt>
  211. <dd>
  212. <p>是否跳过非public的属性和方法, 默认会跳过</p>
  213. </dd>
  214. <dt class="hdlist1">-keepdirectories [directory_filter] </dt>
  215. <dd>
  216. <p>为了减小jar文件体积, 默认所有目录都会被删除, 仅指定-keepdirectories则所有目录都会保留, 指定directory_filter时仅保留filter筛选的目录</p>
  217. </dd>
  218. <dt class="hdlist1">-target version </dt>
  219. <dd>
  220. <p>指定目标class文件的版本号, 可以是1.0,&#8230;&#8203;, 1.9, 或者是简短的5,&#8230;&#8203;, 12, 默认版本号保持不变</p>
  221. </dd>
  222. <dt class="hdlist1">-forceprocessing </dt>
  223. <dd>
  224. <p>强制处理, 即使outjar已经存在</p>
  225. </dd>
  226. </dl>
  227. </div>
  228. </div>
  229. <div class="sect3">
  230. <h4 id="_保持选项keep_option"><a class="anchor" href="#_保持选项keep_option"></a>2.2.2. 保持选项(keep Option)</h4>
  231. <div class="dlist">
  232. <dl>
  233. <dt class="hdlist1">-keep [,modifier,&#8230;&#8203;] class_specification </dt>
  234. <dd>
  235. <p>modifier包括如下类型</p>
  236. <div class="ulist">
  237. <ul>
  238. <li>
  239. <p>includedescriptorclasses: -keep选项声明的所有属性和方法归属的类不做任何修改</p>
  240. </li>
  241. <li>
  242. <p>includecode: -keep选项声明的方法的所有属性都不做任何修改, 例如不能被优化和混淆</p>
  243. </li>
  244. <li>
  245. <p>allowshrinking: -keep选项声明的入口点能被移除,但不能优化或混淆</p>
  246. </li>
  247. <li>
  248. <p>allowoptimization:-keep选项声明的入口点能被优化,但不能移除或混淆</p>
  249. </li>
  250. <li>
  251. <p>allowobfuscation:-keep选项声明的入口点能被混淆,但不能移除或优化</p>
  252. </li>
  253. </ul>
  254. </div>
  255. </dd>
  256. </dl>
  257. </div>
  258. <div class="listingblock">
  259. <div class="title">示例</div>
  260. <div class="content">
  261. <pre class="highlight"><code class="language-bash" data-lang="bash">-keep public class * extends android.app.Activity
  262. -keep public class * implements javax.servlet.Servlet
  263. -keep public class * {
  264. public protected *;
  265. }</code></pre>
  266. </div>
  267. </div>
  268. <div class="dlist">
  269. <dl>
  270. <dt class="hdlist1">-keepclassmembers [,modifier,&#8230;&#8203;] class_specification </dt>
  271. <dd>
  272. <p>保留类成员不变</p>
  273. </dd>
  274. </dl>
  275. </div>
  276. <div class="listingblock">
  277. <div class="title">示例</div>
  278. <div class="content">
  279. <pre class="highlight"><code class="language-bash" data-lang="bash">-keepclassmembers class * implements java.io.Serializable {
  280. private static final java.io.ObjectStreamField[] serialPersistentFields;
  281. private void writeObject(java.io.ObjectOutputStream);
  282. private void readObject(java.io.ObjectInputStream);
  283. java.lang.Object writeReplace();
  284. java.lang.Object readResolve();
  285. }</code></pre>
  286. </div>
  287. </div>
  288. <div class="dlist">
  289. <dl>
  290. <dt class="hdlist1">-keepclasseswithmembers [,modifier,&#8230;&#8203;] class_specification </dt>
  291. <dd>
  292. <p>保留满足条件的类</p>
  293. </dd>
  294. </dl>
  295. </div>
  296. <div class="listingblock">
  297. <div class="title">示例</div>
  298. <div class="content">
  299. <pre class="highlight"><code class="language-bash" data-lang="bash">-keepclasseswithmembers public class * {
  300. public static void main(java.lang.String[]);
  301. }</code></pre>
  302. </div>
  303. </div>
  304. <div class="dlist">
  305. <dl>
  306. <dt class="hdlist1">-keepnames class_specification </dt>
  307. <dd>
  308. <p>-keep,allowshrinkingclass_specification的缩写形式</p>
  309. </dd>
  310. <dt class="hdlist1">-keepclassmembernames class_specification </dt>
  311. <dd>
  312. <p>-keepclassmembers,allowshrinkingclass_specification的缩写形式</p>
  313. </dd>
  314. <dt class="hdlist1">-keepclasseswithmembernames class_specification </dt>
  315. <dd>
  316. <p>-keepclasseswithmembers,allowshrinkingclass_specification的缩写形式,保留类和成员的名称</p>
  317. </dd>
  318. </dl>
  319. </div>
  320. <div class="listingblock">
  321. <div class="title">示例</div>
  322. <div class="content">
  323. <pre class="highlight"><code class="language-bash" data-lang="bash">-keepclasseswithmembernames,includedescriptorclasses class * {
  324. native &lt;methods&gt;;
  325. }</code></pre>
  326. </div>
  327. </div>
  328. </div>
  329. <div class="sect3">
  330. <h4 id="_压缩选项shrinking_options"><a class="anchor" href="#_压缩选项shrinking_options"></a>2.2.3. 压缩选项(Shrinking Options)</h4>
  331. <div class="dlist">
  332. <dl>
  333. <dt class="hdlist1">-dontshrink </dt>
  334. <dd>
  335. <p>不压缩, 默认会移除没用的类或成员, 每个优化步骤完成后都会执行一次收缩</p>
  336. </dd>
  337. <dt class="hdlist1">-printusage [filename] </dt>
  338. <dd>
  339. <p>把没用的代码输出到文件或标准输出</p>
  340. </dd>
  341. <dt class="hdlist1">-whyareyoukeeping class_specification </dt>
  342. <dd>
  343. <p>输出保留类的原因</p>
  344. </dd>
  345. </dl>
  346. </div>
  347. </div>
  348. <div class="sect3">
  349. <h4 id="_优化选项optimization_options"><a class="anchor" href="#_优化选项optimization_options"></a>2.2.4. 优化选项(Optimization Options)</h4>
  350. <div class="dlist">
  351. <dl>
  352. <dt class="hdlist1">-dontoptimize </dt>
  353. <dd>
  354. <p>不优化, 内联并且合并类或成员, 在字节码级别上优化所有方法</p>
  355. </dd>
  356. <dt class="hdlist1">-optimizations [optimization_filter] </dt>
  357. <dd>
  358. <p>更细粒度级别上指定优化打开和关闭, 这是高级选项</p>
  359. </dd>
  360. <dt class="hdlist1">-optimizationpasses n </dt>
  361. <dt class="hdlist1">-assumenosideeffects class_specification </dt>
  362. <dt class="hdlist1">-assumenoexternalsideeffects class_specification </dt>
  363. <dt class="hdlist1">-assumenoescapingparameters class_specification </dt>
  364. <dt class="hdlist1">-assumenoexternalreturnvalues class_specification </dt>
  365. <dt class="hdlist1">-assumevalues class_specification </dt>
  366. <dt class="hdlist1">-allowaccessmodification </dt>
  367. <dd>
  368. <p>优化时允许扩大权限实现内联, 如修改属性为public删除get/set方法, 此选项需谨慎使用</p>
  369. </dd>
  370. <dt class="hdlist1">-mergeinterfacesaggressively </dt>
  371. <dd>
  372. <p>==== 混淆选项(Obfuscation options)</p>
  373. </dd>
  374. <dt class="hdlist1">-dontobfuscate </dt>
  375. <dd>
  376. <p>不混淆, 默认会混淆</p>
  377. </dd>
  378. <dt class="hdlist1">-printmapping [filename] </dt>
  379. <dd>
  380. <p>打印新旧名称的对应关系</p>
  381. </dd>
  382. <dt class="hdlist1">-applymapping filename </dt>
  383. <dd>
  384. <p>根据文件中指定的对应关系进行重命名,通常是在-printmapping生成文件基础上修改</p>
  385. </dd>
  386. <dt class="hdlist1">-obfuscationdictionary filename </dt>
  387. <dt class="hdlist1">-classobfuscationdictionary filename </dt>
  388. <dt class="hdlist1">-packageobfuscationdictionary filename </dt>
  389. <dt class="hdlist1">-overloadaggressively </dt>
  390. <dt class="hdlist1">-useuniqueclassmembernames </dt>
  391. <dt class="hdlist1">-dontusemixedcaseclassnames </dt>
  392. <dt class="hdlist1">-keeppackagenames [package_filter] </dt>
  393. <dt class="hdlist1">-flattenpackagehierarchy [package_name] </dt>
  394. <dt class="hdlist1">-repackageclasses [package_name] </dt>
  395. <dd>
  396. <p>重命名包名, 使代码更难理解, package_name没有值或值为''时包名会完全移除, 类中基于包名获取资源文件的代码会因此失效, 如Freemarker模板, 需谨慎使用</p>
  397. </dd>
  398. <dt class="hdlist1">-keepattributes [attribute_filter] </dt>
  399. <dd>
  400. <p>保留一些可选属性, 混淆时生效, 常见可选属性包括</p>
  401. <div class="ulist">
  402. <ul>
  403. <li>
  404. <p>InnerClasses: 类和内部类的链接关系</p>
  405. </li>
  406. <li>
  407. <p>MethodParameters: 方法参数名和参数修饰符</p>
  408. </li>
  409. <li>
  410. <p>Exceptions: 方法可能抛出的异常</p>
  411. </li>
  412. <li>
  413. <p>LineNumberTable: 方法 的行号</p>
  414. </li>
  415. <li>
  416. <p>RuntimeVisibleAnnotations: 运行时生效的注解</p>
  417. </li>
  418. </ul>
  419. </div>
  420. </dd>
  421. </dl>
  422. </div>
  423. <div class="listingblock">
  424. <div class="title">示例</div>
  425. <div class="content">
  426. <pre class="highlight"><code class="language-bash" data-lang="bash">-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,\*Annotation*,EnclosingMethod</code></pre>
  427. </div>
  428. </div>
  429. <div class="dlist">
  430. <dl>
  431. <dt class="hdlist1">-keepparameternames </dt>
  432. <dd>
  433. <p>保持方法的参数名和参数类型不变, 仅在混淆时生效, 通常在处理类库jar包使用, 因为一些IDE会根据参数名和参数类型给开发者相应帮助, 如自动补全等</p>
  434. </dd>
  435. <dt class="hdlist1">-renamesourcefileattribute [string] </dt>
  436. <dd>
  437. <p>修改SourceFile和SourceDir的属性值为一个常量, 与keepattributes一起使用</p>
  438. </dd>
  439. <dt class="hdlist1">-adaptclassstrings [class_filter] </dt>
  440. <dt class="hdlist1">-adaptresourcefilenames [file_filter] </dt>
  441. <dt class="hdlist1">-adaptresourcefilecontents [file_filter] </dt>
  442. <dd>
  443. <p>==== 预验证选项(Preverification options)</p>
  444. </dd>
  445. <dt class="hdlist1">-dontpreverify </dt>
  446. <dd>
  447. <p>不对class文件预验证, 不常用</p>
  448. </dd>
  449. <dt class="hdlist1">-microedition </dt>
  450. <dt class="hdlist1">-android </dt>
  451. <dd>
  452. <p>表示class文件是基于安卓平台的, ProGuard处理时会考虑诸多安卓相关的特性</p>
  453. </dd>
  454. </dl>
  455. </div>
  456. </div>
  457. <div class="sect3">
  458. <h4 id="_通用选项general_options"><a class="anchor" href="#_通用选项general_options"></a>2.2.5. 通用选项(General options)</h4>
  459. <div class="ulist">
  460. <ul>
  461. <li>
  462. <p>-verbose</p>
  463. </li>
  464. <li>
  465. <p>-dontnote [class_filter]</p>
  466. </li>
  467. <li>
  468. <p>-dontwarn [class_filter]</p>
  469. </li>
  470. <li>
  471. <p>-ignorewarnings</p>
  472. </li>
  473. <li>
  474. <p>-printconfiguration [filename]</p>
  475. </li>
  476. <li>
  477. <p>-dump [filename]</p>
  478. </li>
  479. <li>
  480. <p>-addconfigurationdebugging</p>
  481. </li>
  482. </ul>
  483. </div>
  484. </div>
  485. </div>
  486. <div class="sect2">
  487. <h3 id="_示例"><a class="anchor" href="#_示例"></a>2.3. 示例</h3>
  488. <div class="paragraph">
  489. <p>本节主要从官网Examples部分选取部分JavaEE相关示例,可以在官网查看 <a href="https://www.guardsquare.com/en/products/proguard/manual/examples">完整示例</a></p>
  490. </div>
  491. <div class="exampleblock">
  492. <div class="title">Example 1. 保留所有Servlet</div>
  493. <div class="content">
  494. <div class="listingblock">
  495. <div class="content">
  496. <pre class="highlight"><code class="language-bash" data-lang="bash">-injars in.jar
  497. -outjars out.jar
  498. -libraryjars &lt;java.home&gt;/lib/rt.jar
  499. -libraryjars /usr/local/java/servlet/servlet.jar
  500. -printseeds
  501. -keep public class * implements javax.servlet.Servlet</code></pre>
  502. </div>
  503. </div>
  504. </div>
  505. </div>
  506. <div class="exampleblock">
  507. <div class="title">Example 2. 保留枚举的values()和valueOf()方法</div>
  508. <div class="content">
  509. <div class="listingblock">
  510. <div class="content">
  511. <pre class="highlight"><code class="language-bash" data-lang="bash">-keepclassmembers,allowoptimization enum * {
  512. public static **[] values();
  513. public static ** valueOf(java.lang.String);
  514. }</code></pre>
  515. </div>
  516. </div>
  517. </div>
  518. </div>
  519. <div class="exampleblock">
  520. <div class="title">Example 3. 保留序列化的类的重要方法</div>
  521. <div class="content">
  522. <div class="listingblock">
  523. <div class="content">
  524. <pre class="highlight"><code class="language-bash" data-lang="bash">-keepclassmembers class * implements java.io.Serializable {
  525. private static final java.io.ObjectStreamField[] serialPersistentFields;
  526. private void writeObject(java.io.ObjectOutputStream);
  527. private void readObject(java.io.ObjectInputStream);
  528. java.lang.Object writeReplace();
  529. java.lang.Object readResolve();
  530. }</code></pre>
  531. </div>
  532. </div>
  533. </div>
  534. </div>
  535. <div class="exampleblock">
  536. <div class="title">Example 4. 保留POJO的方法</div>
  537. <div class="content">
  538. <div class="listingblock">
  539. <div class="content">
  540. <pre class="highlight"><code class="language-bash" data-lang="bash">-keep class mybeans.** {
  541. void set*(***);
  542. void set*(int, ***);
  543. boolean is*();
  544. boolean is*(int);
  545. *** get*();
  546. *** get*(int);
  547. }</code></pre>
  548. </div>
  549. </div>
  550. </div>
  551. </div>
  552. <div class="exampleblock">
  553. <div class="title">Example 5. 保留注解信息</div>
  554. <div class="content">
  555. <div class="listingblock">
  556. <div class="content">
  557. <pre class="highlight"><code class="language-bash" data-lang="bash">-keepattributes *Annotation*</code></pre>
  558. </div>
  559. </div>
  560. </div>
  561. </div>
  562. <div class="exampleblock">
  563. <div class="title">Example 6. 保留依赖注入信息</div>
  564. <div class="content">
  565. <div class="listingblock">
  566. <div class="content">
  567. <pre class="highlight"><code class="language-bash" data-lang="bash">-keepclassmembers class * {
  568. @org.springframework.beans.factory.annotation.Autowired *;
  569. }</code></pre>
  570. </div>
  571. </div>
  572. </div>
  573. </div>
  574. </div>
  575. <div class="sect2">
  576. <h3 id="_常见错误及原因"><a class="anchor" href="#_常见错误及原因"></a>2.4. 常见错误及原因</h3>
  577. <div class="paragraph">
  578. <p>命令执行出错时可优先在官网的 <a href="https://www.guardsquare.com/en/products/proguard/manual/troubleshooting">Troubleshooting</a>章节查询对应错误,找不到时再到其它站点搜索</p>
  579. </div>
  580. </div>
  581. </div>
  582. </div>
  583. <div class="sect1">
  584. <h2 id="_实践"><a class="anchor" href="#_实践"></a>3. 实践</h2>
  585. <div class="sectionbody">
  586. <div class="paragraph">
  587. <p>本章以一个简单的ssm项目为例来说明使用ProGuard完成代码混淆的基本流程</p>
  588. </div>
  589. <div class="sect2">
  590. <h3 id="_入口点分析"><a class="anchor" href="#_入口点分析"></a>3.1. 入口点分析</h3>
  591. <div class="paragraph">
  592. <p>从上一节对用法的介绍可以看出使用ProGuard的关键就是配置文件的编写,而配置文件中最重要的部分就是入口点选择,如果不能选对入口点,会使关键的类或成员被移除或重命名,最终处理后的项目无法正常运行。</p>
  593. </div>
  594. <div class="paragraph">
  595. <p>本项目使用了SpringMVC + Spring + Mybatis + Shiro框架进行构建,所以在代码混淆时要注意以下入口点</p>
  596. </div>
  597. <div class="ulist">
  598. <ul>
  599. <li>
  600. <p>Controller类的方法用于处理@RequestMapping注解URL对应的请求,所以Controller类和它的所有方法都要保留</p>
  601. </li>
  602. <li>
  603. <p>Controller类的方法名可以变化,但方法参数名不能变化,否则无法完成参数赋值</p>
  604. </li>
  605. <li>
  606. <p>AOP声明中涉及的类的包名和方法名不能变化,此项目中AOP主要用于声明式事务,对Service类的add、update、delete开头的方法做事务控制,这些类和对应的方法名必须声明为入口点</p>
  607. </li>
  608. <li>
  609. <p>Mybatis使用的Mapper接口名、包名、方法名不能变化,sql查询结果映射的实体类名、属性名不能变化,这些也需要声明为入口点</p>
  610. </li>
  611. <li>
  612. <p>web.xml中声明的过滤器、监听器以及Spring配置文件中涉及的其它Bean定义都需要声明为入口点</p>
  613. </li>
  614. </ul>
  615. </div>
  616. </div>
  617. <div class="sect2">
  618. <h3 id="_编写配置文件"><a class="anchor" href="#_编写配置文件"></a>3.2. 编写配置文件</h3>
  619. <div class="paragraph">
  620. <p>根据第一步的入口点分析编写如下配置文件</p>
  621. </div>
  622. <div class="listingblock">
  623. <div class="content">
  624. <pre class="highlight"><code class="language-bash" data-lang="bash">-injars example.jar
  625. -outjars example_out.jar
  626. -libraryjars &lt;java.home&gt;/lib/rt.jar
  627. #jdk中加密相关的jar包
  628. -libraryjars &lt;java.home&gt;/lib/jce.jar
  629. #所有maven依赖的目录
  630. -libraryjars mavenlibs
  631. #tomcat中的依赖库, 主要是servlet-api
  632. -libraryjars tomcatlibs
  633. -printseeds
  634. #混淆使用的映射文件
  635. -applymapping classname.map
  636. -printmapping baseframe.map
  637. #不提示通知信息
  638. -dontnote
  639. #优化时不修饰类或方法为final, 否则无法使用cglib代理
  640. -optimizations !class/marking/final,!method/marking/final
  641. -renamesourcefileattribute SourceFile
  642. -keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod,LocalVariableTable,LocalVariableTypeTable
  643. -keepclassmembers enum * {
  644. public static **[] values();
  645. public static ** valueOf(java.lang.String);
  646. }
  647. #Controller类的属性和方法都保留, 但可以重命名
  648. -keep,allowobfuscation @org.springframework.web.bind.annotation.RestController public class * {
  649. &lt;fields&gt;;
  650. &lt;methods&gt;;
  651. }
  652. #保留无参构造器, 否则无法实例化为bean, 保留@Autowired声明的属性
  653. -keepclassmembers class * {
  654. &lt;init&gt;();
  655. @org.springframework.beans.factory.annotation.Autowired *;
  656. }
  657. #保留参数名, 否则Controller方法参数赋值存在问题
  658. -keepparameternames
  659. #保留service的包名, 否则声明式事务失效
  660. -keeppackagenames io.github.**.service
  661. #保留service中事务相关的方法名, 否则声明式事务失效
  662. -keepclassmembernames class io.github.**.service.* {
  663. *** add*(...);
  664. *** save*(...);
  665. *** insert*(...);
  666. *** update*(...);
  667. *** del*(...);
  668. *** get*(...);
  669. *** find*(...);
  670. }
  671. #保留Mapper接口
  672. -keep interface io.github.**.dao.*Mapper {
  673. &lt;methods&gt;;
  674. }
  675. #保留实体类不变化, 否则sql数据映射和Controller参数映射失效
  676. -keep class io.github.**.entity.* {
  677. *;
  678. }
  679. #保留Servlet
  680. -keep,allowoptimization public class * implements javax.servlet.Servlet
  681. #保留SpringMVC拦截器
  682. -keep,allowoptimization public class * extends org.springframework.web.servlet.handler.HandlerInterceptorAdapter
  683. #保留Shiro过滤器
  684. -keep,allowoptimization public class * extends org.apache.shiro.web.servlet.AdviceFilter
  685. #保留Shiro的Realm
  686. -keep,allowoptimization public class * extends org.apache.shiro.realm.AuthorizingRealm
  687. #保留SpringMVC的过滤器
  688. -keep public class * extends org.springframework.web.filter.OncePerRequestFilter {
  689. *;
  690. }
  691. # 移除不会产生边际效用的方法
  692. # Remove - System method calls. Remove all invocations of System
  693. # methods without side effects whose return values are not used.
  694. -assumenosideeffects public class java.lang.System {
  695. public static long currentTimeMillis();
  696. static java.lang.Class getCallerClass();
  697. public static int identityHashCode(java.lang.Object);
  698. public static java.lang.SecurityManager getSecurityManager();
  699. public static java.util.Properties getProperties();
  700. public static java.lang.String getProperty(java.lang.String);
  701. public static java.lang.String getenv(java.lang.String);
  702. public static java.lang.String mapLibraryName(java.lang.String);
  703. public static java.lang.String getProperty(java.lang.String,java.lang.String);
  704. }
  705. # Remove - Math method calls. Remove all invocations of Math
  706. # methods without side effects whose return values are not used.
  707. -assumenosideeffects public class java.lang.Math {
  708. public static double sin(double);
  709. public static double cos(double);
  710. public static double tan(double);
  711. public static double asin(double);
  712. public static double acos(double);
  713. public static double atan(double);
  714. public static double toRadians(double);
  715. public static double toDegrees(double);
  716. public static double exp(double);
  717. public static double log(double);
  718. public static double log10(double);
  719. public static double sqrt(double);
  720. public static double cbrt(double);
  721. public static double IEEEremainder(double,double);
  722. public static double ceil(double);
  723. public static double floor(double);
  724. public static double rint(double);
  725. public static double atan2(double,double);
  726. public static double pow(double,double);
  727. public static int round(float);
  728. public static long round(double);
  729. public static double random();
  730. public static int abs(int);
  731. public static long abs(long);
  732. public static float abs(float);
  733. public static double abs(double);
  734. public static int max(int,int);
  735. public static long max(long,long);
  736. public static float max(float,float);
  737. public static double max(double,double);
  738. public static int min(int,int);
  739. public static long min(long,long);
  740. public static float min(float,float);
  741. public static double min(double,double);
  742. public static double ulp(double);
  743. public static float ulp(float);
  744. public static double signum(double);
  745. public static float signum(float);
  746. public static double sinh(double);
  747. public static double cosh(double);
  748. public static double tanh(double);
  749. public static double hypot(double,double);
  750. public static double expm1(double);
  751. public static double log1p(double);
  752. }
  753. # Remove - Number method calls. Remove all invocations of Number
  754. # methods without side effects whose return values are not used.
  755. -assumenosideeffects public class java.lang.* extends java.lang.Number {
  756. public static java.lang.String toString(byte);
  757. public static java.lang.Byte valueOf(byte);
  758. public static byte parseByte(java.lang.String);
  759. public static byte parseByte(java.lang.String,int);
  760. public static java.lang.Byte valueOf(java.lang.String,int);
  761. public static java.lang.Byte valueOf(java.lang.String);
  762. public static java.lang.Byte decode(java.lang.String);
  763. public int compareTo(java.lang.Byte);
  764. public static java.lang.String toString(short);
  765. public static short parseShort(java.lang.String);
  766. public static short parseShort(java.lang.String,int);
  767. public static java.lang.Short valueOf(java.lang.String,int);
  768. public static java.lang.Short valueOf(java.lang.String);
  769. public static java.lang.Short valueOf(short);
  770. public static java.lang.Short decode(java.lang.String);
  771. public static short reverseBytes(short);
  772. public int compareTo(java.lang.Short);
  773. public static java.lang.String toString(int,int);
  774. public static java.lang.String toHexString(int);
  775. public static java.lang.String toOctalString(int);
  776. public static java.lang.String toBinaryString(int);
  777. public static java.lang.String toString(int);
  778. public static int parseInt(java.lang.String,int);
  779. public static int parseInt(java.lang.String);
  780. public static java.lang.Integer valueOf(java.lang.String,int);
  781. public static java.lang.Integer valueOf(java.lang.String);
  782. public static java.lang.Integer valueOf(int);
  783. public static java.lang.Integer getInteger(java.lang.String);
  784. public static java.lang.Integer getInteger(java.lang.String,int);
  785. public static java.lang.Integer getInteger(java.lang.String,java.lang.Integer);
  786. public static java.lang.Integer decode(java.lang.String);
  787. public static int highestOneBit(int);
  788. public static int lowestOneBit(int);
  789. public static int numberOfLeadingZeros(int);
  790. public static int numberOfTrailingZeros(int);
  791. public static int bitCount(int);
  792. public static int rotateLeft(int,int);
  793. public static int rotateRight(int,int);
  794. public static int reverse(int);
  795. public static int signum(int);
  796. public static int reverseBytes(int);
  797. public int compareTo(java.lang.Integer);
  798. public static java.lang.String toString(long,int);
  799. public static java.lang.String toHexString(long);
  800. public static java.lang.String toOctalString(long);
  801. public static java.lang.String toBinaryString(long);
  802. public static java.lang.String toString(long);
  803. public static long parseLong(java.lang.String,int);
  804. public static long parseLong(java.lang.String);
  805. public static java.lang.Long valueOf(java.lang.String,int);
  806. public static java.lang.Long valueOf(java.lang.String);
  807. public static java.lang.Long valueOf(long);
  808. public static java.lang.Long decode(java.lang.String);
  809. public static java.lang.Long getLong(java.lang.String);
  810. public static java.lang.Long getLong(java.lang.String,long);
  811. public static java.lang.Long getLong(java.lang.String,java.lang.Long);
  812. public static long highestOneBit(long);
  813. public static long lowestOneBit(long);
  814. public static int numberOfLeadingZeros(long);
  815. public static int numberOfTrailingZeros(long);
  816. public static int bitCount(long);
  817. public static long rotateLeft(long,int);
  818. public static long rotateRight(long,int);
  819. public static long reverse(long);
  820. public static int signum(long);
  821. public static long reverseBytes(long);
  822. public int compareTo(java.lang.Long);
  823. public static java.lang.String toString(float);
  824. public static java.lang.String toHexString(float);
  825. public static java.lang.Float valueOf(java.lang.String);
  826. public static java.lang.Float valueOf(float);
  827. public static float parseFloat(java.lang.String);
  828. public static boolean isNaN(float);
  829. public static boolean isInfinite(float);
  830. public static int floatToIntBits(float);
  831. public static int floatToRawIntBits(float);
  832. public static float intBitsToFloat(int);
  833. public static int compare(float,float);
  834. public boolean isNaN();
  835. public boolean isInfinite();
  836. public int compareTo(java.lang.Float);
  837. public static java.lang.String toString(double);
  838. public static java.lang.String toHexString(double);
  839. public static java.lang.Double valueOf(java.lang.String);
  840. public static java.lang.Double valueOf(double);
  841. public static double parseDouble(java.lang.String);
  842. public static boolean isNaN(double);
  843. public static boolean isInfinite(double);
  844. public static long doubleToLongBits(double);
  845. public static long doubleToRawLongBits(double);
  846. public static double longBitsToDouble(long);
  847. public static int compare(double,double);
  848. public boolean isNaN();
  849. public boolean isInfinite();
  850. public int compareTo(java.lang.Double);
  851. public byte byteValue();
  852. public short shortValue();
  853. public int intValue();
  854. public long longValue();
  855. public float floatValue();
  856. public double doubleValue();
  857. public int compareTo(java.lang.Object);
  858. public boolean equals(java.lang.Object);
  859. public int hashCode();
  860. public java.lang.String toString();
  861. }
  862. # Remove - String method calls. Remove all invocations of String
  863. # methods without side effects whose return values are not used.
  864. -assumenosideeffects public class java.lang.String {
  865. public static java.lang.String copyValueOf(char[]);
  866. public static java.lang.String copyValueOf(char[],int,int);
  867. public static java.lang.String valueOf(boolean);
  868. public static java.lang.String valueOf(char);
  869. public static java.lang.String valueOf(char[]);
  870. public static java.lang.String valueOf(char[],int,int);
  871. public static java.lang.String valueOf(double);
  872. public static java.lang.String valueOf(float);
  873. public static java.lang.String valueOf(int);
  874. public static java.lang.String valueOf(java.lang.Object);
  875. public static java.lang.String valueOf(long);
  876. public boolean contentEquals(java.lang.StringBuffer);
  877. public boolean endsWith(java.lang.String);
  878. public boolean equalsIgnoreCase(java.lang.String);
  879. public boolean equals(java.lang.Object);
  880. public boolean matches(java.lang.String);
  881. public boolean regionMatches(boolean,int,java.lang.String,int,int);
  882. public boolean regionMatches(int,java.lang.String,int,int);
  883. public boolean startsWith(java.lang.String);
  884. public boolean startsWith(java.lang.String,int);
  885. public byte[] getBytes();
  886. public byte[] getBytes(java.lang.String);
  887. public char charAt(int);
  888. public char[] toCharArray();
  889. public int compareToIgnoreCase(java.lang.String);
  890. public int compareTo(java.lang.Object);
  891. public int compareTo(java.lang.String);
  892. public int hashCode();
  893. public int indexOf(int);
  894. public int indexOf(int,int);
  895. public int indexOf(java.lang.String);
  896. public int indexOf(java.lang.String,int);
  897. public int lastIndexOf(int);
  898. public int lastIndexOf(int,int);
  899. public int lastIndexOf(java.lang.String);
  900. public int lastIndexOf(java.lang.String,int);
  901. public int length();
  902. public java.lang.CharSequence subSequence(int,int);
  903. public java.lang.String concat(java.lang.String);
  904. public java.lang.String replaceAll(java.lang.String,java.lang.String);
  905. public java.lang.String replace(char,char);
  906. public java.lang.String replaceFirst(java.lang.String,java.lang.String);
  907. public java.lang.String[] split(java.lang.String);
  908. public java.lang.String[] split(java.lang.String,int);
  909. public java.lang.String substring(int);
  910. public java.lang.String substring(int,int);
  911. public java.lang.String toLowerCase();
  912. public java.lang.String toLowerCase(java.util.Locale);
  913. public java.lang.String toString();
  914. public java.lang.String toUpperCase();
  915. public java.lang.String toUpperCase(java.util.Locale);
  916. public java.lang.String trim();
  917. }
  918. # Remove - StringBuffer method calls. Remove all invocations of StringBuffer
  919. # methods without side effects whose return values are not used.
  920. -assumenosideeffects public class java.lang.StringBuffer {
  921. public java.lang.String toString();
  922. public char charAt(int);
  923. public int capacity();
  924. public int codePointAt(int);
  925. public int codePointBefore(int);
  926. public int indexOf(java.lang.String,int);
  927. public int lastIndexOf(java.lang.String);
  928. public int lastIndexOf(java.lang.String,int);
  929. public int length();
  930. public java.lang.String substring(int);
  931. public java.lang.String substring(int,int);
  932. }
  933. # Remove - StringBuilder method calls. Remove all invocations of StringBuilder
  934. # methods without side effects whose return values are not used.
  935. -assumenosideeffects public class java.lang.StringBuilder {
  936. public java.lang.String toString();
  937. public char charAt(int);
  938. public int capacity();
  939. public int codePointAt(int);
  940. public int codePointBefore(int);
  941. public int indexOf(java.lang.String,int);
  942. public int lastIndexOf(java.lang.String);
  943. public int lastIndexOf(java.lang.String,int);
  944. public int length();
  945. public java.lang.String substring(int);
  946. public java.lang.String substring(int,int);
  947. }</code></pre>
  948. </div>
  949. </div>
  950. </div>
  951. <div class="sect2">
  952. <h3 id="_其它说明"><a class="anchor" href="#_其它说明"></a>3.3. 其它说明</h3>
  953. <div class="olist arabic">
  954. <ol class="arabic">
  955. <li>
  956. <p>@myconfig.pro最好搭配-basedirectory选项使用且-basedirectory在前,这样配置中的所有文件都可以使用相对路径,最终执行混淆命令如下<br>
  957. <code>proguard -basedirectory C:/Users/abc/Desktop/proguard6.1.0/test/example @example.pro</code></p>
  958. </li>
  959. <li>
  960. <p>web项目在部署时会将项目内容打包为war形式,因为web容器能够自动解压部署,但ProGuard主要是用于处理字节码文件且要求包名和文件夹路径一致,所以需要将所有class文件打包为jar形式并指定依赖库进行处理,可以在target/classes目录下执行如下命令完成打包<br>
  961. <code>jar -cvf example.jar io</code></p>
  962. </li>
  963. <li>
  964. <p>由于SpringMVC大量使用注解创建Bean,而Bean的名称默认是类名首字母小写,混淆后的class在不同包下存在大量的重复类名如下图,这样会出现Bean名称冲突的问题,由于ProGuard并未提供保证类名唯一的选项,所以需要使用-applymapping参数指定混淆前后类名的对应关系如下,从而保证混淆后的类名不重复,不过此文件内容也不必完全自行编写,可以在-printmapping生成的文件基础上修改即可</p>
  965. </li>
  966. <li>
  967. <p>org.slf4j.Logger属性必须声明为static或final如下,否则不能正确处理</p>
  968. </li>
  969. <li>
  970. <p>在入口点分析一节中已经分析了必须保持Controller的方法参数名不变才能实现参数赋值,实际上SpringMVC也支持名称不同时的参数赋值,即给方法参数添加@RequestParam注解,注解中声明请求参数名,所以只要在开发时简单类型的参数都使用@RequestParam注解,那么方法的参数名也可以变化</p>
  971. </li>
  972. <li>
  973. <p>在入口点分析一节中已经分析了Mybatis的Mapper文件对应的接口不能变化,否则所有查询都会失效,这一点实际上也可以规避,只要在混淆后根据前后接口名和方法名的映射关系修改对应Mapper文件内容即可,这一步需要自行开发工具进行实现。</p>
  974. </li>
  975. <li>
  976. <p>实践后发现方法参数名是否变化取决于是否保留LocalVariableTable属性,而与-keepparameternames无关,保留LocalVariableTable属性时所有方法参数和局部变量名称都保持不变,不保留时都重命名,而且参数名是否保留无法在类或方法的粒度进行控制,仅能做全局控制。</p>
  977. </li>
  978. <li>
  979. <p>SpringMVC中Controller类的方法参数名必须与前端参数名对应,要注意混淆可能导致的传参失败问题。</p>
  980. </li>
  981. <li>
  982. <p>MyBatis在Mapper文件中声明对应的接口和方法,要注意混淆可能导致的Mapper文件失效问题</p>
  983. </li>
  984. </ol>
  985. </div>
  986. </div>
  987. </div>
  988. </div>
  989. </div>
  990. <div id="footer">
  991. <div id="footer-text">
  992. Last updated 2024-03-18 05:44:42 UTC
  993. </div>
  994. </div>
  995. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.3/highlight.min.js"></script>
  996. <script>
  997. if (!hljs.initHighlighting.called) {
  998. hljs.initHighlighting.called = true
  999. ;[].slice.call(document.querySelectorAll('pre.highlight > code')).forEach(function (el) { hljs.highlightBlock(el) })
  1000. }
  1001. </script>
  1002. <script src="https://utteranc.es/client.js"
  1003. repo="pxzxj/articles"
  1004. issue-term="title"
  1005. label="utteranc"
  1006. theme="github-light"
  1007. crossorigin="anonymous"
  1008. async>
  1009. </script>
  1010. </div>
  1011. </div>
  1012. </div>
  1013. </body>
  1014. </html>