XmlClassGuard is a gradle plugin that can obfuscate any class, and it is the killer tool of Google Play

1 Introduction

Dear classmates, long time no see, this time I bring you a very useful plugin XmlClassGuard , which can confuse any classes such as Android 4 major components, custom View, etc. Maybe you will question the use of this, but it has been listed on Google Paly. Classmates, it is estimated that you can't wait to learn about XmlClassGuard when you see the title . It is simple to understand that if your account is removed or banned from appGoogle Play, and you want to re-list it, you must manually change a bunch of them 包名+类名. The workload is as little as 2- 3 days, up to a week, or even longer, and it is easy to make mistakes; however XmlClassGuard, if you use it, you only need to perform one task, and you can get it done within 30 seconds without making mistakes. Simply put, XmlClassGuardit is to free your hands and help you successfully launch. Google Play.

ok, then go directly to the topic! !

Add me on WeChat ljx-studio to pull you into the XmlClassGuard exchange group

2. Introduction to XmlClassGuard

  • XmlClassGuardIt is a plug-in that can obfuscate any class of Android 4 major components, custom View, etc.

  • XmlClassGuardIt can be seen as ProGuarda supplement, ProGuardhas nothing to do with, and will not have any conflict

  • Quickly change properties in manifestfiles packageand sync them to other files

  • Quickly move n directories to other directories and synchronize to other files

  • XmlClassGuardThe main function is to confuse the classes used in the xml file, so it is named XmlClassGuard, corresponding to AndResGuard and AadResGuard

3. What is the use?

  • Make up for ProGuardthe problem of not confusing the 4 major components and other classes

  • Increase the difficulty of decompilation of aab and apk

  • 极大降低aab包查重率,避免上架Google Play因查重率过高,导致下架或封号问题

关于第三点,有过上架Google Play 商店的同学应该知道,如果之前的包被下架或封号,想要同套代码再次上架,那99%概率是再次封号,很大一部分原因就是上述说到的类未被混淆,很容易被Google断定为包重复,从而导致再次封号,因此,如果想要再次上架,就必须要更改四大组件、自定义View等的包名+类名以降低查重率,然而,如果手动去完成这项任务,估计会累死一个程序员,于是乎,就有了XmlClassGuard,通过插件去完成手工的活,一个任务便可搞定

4、原理

XmlClassGuard不同于AndResGuard(apk资源混淆)、AadResGuard(aab资源混淆)侵入打包流程的方案,XmlClassGuard需要在打包前执行xmlClassGuard任务,该任务会检索AndroidManifest.xmlnavigation、layout文件夹下的xml,找出xml文件中引用的类,如4大组件及自定义View等,更改其包名+类名,并将更改后的内容同步到其他文件中,说直白点,就是在打包前,在本地更改包名+类名

警告警告!!!!!! 由于是在本地操作,任务执行是不可逆的,故务必做好代码备份,否则代码将很难还原

5、上手

1、在build.gradle(root project)中配置

buildscript {
    repositories {
        maven { url 'https://jitpack.io' }
    }
    dependencies {
        classpath "com.github.liujingxing:XmlClassGuard:1.0.0"
    }
}
复制代码

2、在 build.gradle(application) 中配置

apply plugin: "xml-class-guard"

//以下均为非必须
xmlClassGuard {
    //用于增量混淆的 mapping 文件
    mappingFile = file("xml-class-mapping.txt")
    //更改manifest文件的package属性,即包名
    packageChange = ["com.ljx.example": "ab.cd"]
    //移动目录
    moveDir = ["com.ljx.example": "ef.gh"]
}
复制代码

此时就可以在Gradle栏中,找到以下3个任务

guard.jpg

6、任务介绍

XmlClassGuard插件共有3个任务,分别是moveDirpackageChangexmlClassGuard,下面将一一介绍

6.1、moveDir

moveDir是一个移动目录的任务,它支持同时移动任意一个目录,它会将原目录下的所有文件(包括子目录)移动到另外一个文件夹下,并将移动的结果,同步到其他文件中,配置如下:

xmlClassGuard {
    //移动目录
    moveDir = ["com.ljx.example": "ef.gh",
               "com.ljx.example.test": "ff.gg"]
}
复制代码

上面代码中moveDir是一个Map对象,其中key代表要移动的目录,value代表目标目录; 上面任务会把com.ljx.example目录下的所有文件,移动到ef.gh 目录下,将com.ljx.example.test目录下的所有文件移动到ff.gg目录下

6.2、packageChange

packageChange是一个更改manifest文件里package属性的任务,也就是更改app包名的任务(不会更改applicationId) ,改完后,会将更改结果,同步到其他文件中(不会更改项目结构),配置如下:

xmlClassGuard {
    //更改manifest文件的package属性,即包名
    packageChange = ["com.ljx.example": "ab.cd"]
}
复制代码

以上packageChange是一个Map对象,key为原始package属性,value为要更改的package属性,原始package属性不匹配,将更改失败

6.3、xmlClassGuard

xmlClassGuard是一个混淆类的任务,该任务会检索AndroidManifest.xmlnavigation、layout 文件夹下的xml文件,找出xml文件中引用到的类,如4大组件及自定义View等,更改其包名+类名,并将更改的结果,同步到其他文件中,最后会将混淆映射写出到mapping文件中,配置如下:

xmlClassGuard {
    //用于增量混淆的 mapping 文件
    mappingFile = file("xml-class-mapping.txt")
}
复制代码

上面配置的mappingFile可以是一个不存在的文件,混淆结束后,会将混淆映射写出到该文件中,如下:

dir mapping:
    com.ljx.example -> e
    com.ljx.example.activity -> dh

class mapping:
    com.ljx.example.AppHolder -> e.B
    com.ljx.example.activity.MainActivity -> dh.C
复制代码

image.png

dir mapping是混淆的目录列表,class mapping是具体类的混淆列表

6.4、混淆任意类

xmlClassGuard任务是支持增量混淆的,如果你需要混淆指定的类com.ljx.example.test.Test,便可以在dir mapping下写入com.ljx.example.test -> h, 此时再次执行xmlClassGuard任务,便会将com.ljx.example.test目录下的所有类(不包含子目录下的类) 移动到h文件夹中,并将所有类名混淆,再次混淆的后mapping文件如下:

dir mapping:
    com.ljx.example -> e
    com.ljx.example.activity -> dh
    com.ljx.example.test -> h

class mapping:
    com.ljx.example.AppHolder -> e.B
    com.ljx.example.activity.MainActivity -> dh.C
    com.ljx.example.test.Test -> h.D
复制代码

手动输入混淆规则,需要注意以下几条规则

mapping_rule.jpg

6.5、每次混淆产生不一样的结果

默认情况下,每次混淆,都将产生一样的结果,混淆的包名根据哈希算法得出,混淆的类名,从大写字母A开启,依次递增,如:A B C ... Y Z BA BB .. ZY ZZ BAA...(可以看做26进制的字符串)

如果你需要每次混淆产生不一样的结果,只需做两步

  • 对于包名,需要你配置每一个

  • 对于类名,可以每一个都去配置,但类太多时,配置每一个,就显得繁琐,此时仅需要配置一个即可

如我们修改一下上面的mapping文件,如下

dir mapping:
    com.ljx.example -> hh
    com.ljx.example.activity -> jk
    com.ljx.example.test -> et

class mapping:
    com.ljx.example.AppHolder -> hh.Z
复制代码

此时执行xmlClassGuard任务,就会产生不一样的结果,如下:

dir mapping:
	com.ljx.example -> hh
	com.ljx.example.activity -> jk
	com.ljx.example.test -> et

class mapping:
	com.ljx.example.AppHolder -> hh.Z
	com.ljx.example.activity.MainActivity -> jk.BA
	com.ljx.example.test.Test -> et.BC
复制代码

可以看到,包名完全是根据自定义生成的结果,而类名便从Z开始,依次递增Z BA BC ..., 这里可以把包名看成26进制的字符串依次递增

7、注意事项⚠️

  • 混淆的类,要避免与其他类同名,否则类名替换时,会出现误杀情况

  • 类混淆后,类的包名(路径)也会被混淆,所以,如果你用到一些三方库,有配置包名的地方,记得手动更改

  • XmlClassGuard不会更改proguard-rules.pro文件的内容,所以,类混淆后,如果该文件内容有混淆前的类或目录,也记得手动更改

  • XmlClassGuard只会帮你更改包名+类名,并同步带其他文件中,不会更改你的任何代码逻辑,如混淆后,出现部分功能不正常问题,需要你自己查找原因,如果是XmlClassGuard的问题,欢迎提issuePR

8、打赏

如果它对你帮助很大,并且你很想支持库的后续开发和维护,那么你可以扫下方二维码随意打赏我,就当是请我喝杯咖啡或是啤酒,开源不易,感激不尽

rxhttp_donate.png

9、推荐阅读

RxHttp 让你眼前一亮的Http请求框架 (基础篇)

RxHttp ,比Retrofit 更优雅的协程体验 (RxHttp + Await)

RxHttp + Flow 三步搞定任意请求

RxHttp 完美适配Android 10/11 上传/下载/进度监听

RxHttp 全网Http缓存最优解

1.jpg

Guess you like

Origin juejin.im/post/7079955014822674468