Android混淆——解決與其它包包名衝突

問題

最近,在第三方集成我們的aar的時候,出現混淆後的文件,與他們項目引入的其他包名字衝突了。

解決方式

默認情況下,混淆後的名字一般爲a、b、c、d以及它們的組合。通過修改混淆規則,控制混淆後的文件命名規則,從根本上避免與其他包可能出現同名的問題。具體步驟是需要在混淆規則文件proguard-rules.pro中做如下設置:

-obfuscationdictionary filename.txt
-classobfuscationdictionary filename.txt
-packageobfuscationdictionary filename.txt
  • -obfuscationdictionary filename .txt:指定一個混淆類名、成員變量名、方法名的字典。
  • -classobfuscationdictionary filename .txt:指定一個混淆類名的字典,字典的格式與-obfuscationdictionary相同;
  • -packageobfuscationdictionary filename .txt:filename 指定一個混淆包名的字典,字典格式與-obfuscationdictionary相同。

filename.txt是用來指定混淆後生成的名字的字典文件,字典文件中的空格,標點符號,重複的詞,還有以’#'開頭的行都會被忽略。需要注意的是添加了字典並不會顯著提高混淆的效果,添加字典有兩個作用:一是避免與其他包混淆後重名;二是更不利與閱讀;
與proguard-rules.pro同級目錄下。
在這裏插入圖片描述
下面是字典的示例,建議可在每個關鍵字之前加上一些特殊的前綴,例如: xxdo、xxif

# 使用java中的關鍵字作字典:避免混淆後與其他包重名,而且混淆之後的代碼更加不利於閱讀
#
# This obfuscation dictionary contains reserved Java keywords. They can't
# be used in Java source files, but they can be used in compiled class files.
# Note that this hardly improves the obfuscation. Decent decompilers can
# automatically replace reserved keywords, and the effect can fairly simply be
# undone by obfuscating again with simpler names.
# Usage:
#     java -jar proguard.jar ..... -obfuscationdictionary filename.txt
#

do
if
for
int
new
try
byte
case
char
else
goto
long
this
void
break
catch
class
const
final
float
short
super
throw
while
double
import
native
public
return
static
switch
throws
boolean
default
extends
finally
package
private
abstract
continue
strictfp
volatile
interface
protected
transient
implements
instanceof
synchronized

參考

1.Android混淆優化之如何解決混淆後與其他包裏面的class名字衝突的問題
2.Android 字符串及字典混淆開源實現

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章