记一次自定义插件(字符串加密插件)
第一次编写插件,开始搞的有点狼狈,一直没有搞明白怎么自定义属性进去,后来看了微信AndResGuard里面的就理解了
首先要做的是先定义好自己需要的属性
/**
* 用于build.gradle中的参数传递
*/
public class SettingParams {
public Iterable<Integer> decryptKey
/**
* 解密文件路径
*/
public String decryptFile
/**
* 解密方法
*/
public String decryptMethod
/**
* 需要加密字符串的目录
*/
public Iterable<String> stringEncryDirList
/**
* 需要手动添加的包
*/
public Iterable<String> stringEncryList
public SettingParams() {
decryptKey = []
stringEncryDirList = []
stringEncryList = []
decryptFile = null
decryptMethod = null
}
Iterable<Integer> getDecryptKey(){
return decryptKey;
}
String getDecryptFile() {
return decryptFile
}
String getDecryptMethod() {
return decryptMethod
}
Iterable<String> getStringEncryList() {
return stringEncryList
}
Iterable<String> getStringEncryDirList() {
return stringEncryDirList
}
}
插件编写的第一步是要实现Plugin
接口
public class StringEncryPlugin implements Plugin<Project>{
@Override
void apply(Project project) {
System.out.println("========================");
System.out.println("开始进入字符串加密Plugin");
System.out.println("========================");
project.extensions.create('stringEncry', SettingParams)
GlobalConfig.setProject(project)
def android = project.extensions.getByType(AppExtension)
def classTransform = new StrProguard(project)
android.registerTransform(classTransform)
}
}
要使自己自定义的属性有效,需要将你的属性创建进扩展容器里面
project.extensions.create('stringEncry', SettingParams)
自定义插件是需要指明插件入口的
StringEncry.properties 这个文件可随意命名,但是要以 .properties 做后续
这个文件里面的内容是
implementation-class=com.kawa.strplugin.StringEncryPlugin
就是实现Plugin接口的类路径
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
编写好的插件上传到本地maven配置
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'java'
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
dependencies {
//gradle sdk
compile gradleApi()
//groovy sdk
compile localGroovy()
// transform 时需要用到gradle tool的api,需要单独引入
implementation 'com.android.tools.build:gradle:3.1.3'
implementation 'com.android.tools.build:gradle-api:3.1.3'
implementation 'org.javassist:javassist:3.20.0-GA'
implementation 'org.ow2.asm:asm:5.0.4'
}
repositories {
mavenCentral()
}
group='com.kawa.plugin'
version='1.0.2'
uploadArchives{
repositories {
mavenDeployer {
//本地的Maven地址
repository(url: uri('F:/maven/repo'))
}
}
}
下面项目是一个字符串加密插件的项目demo
git:https://github.com/KawaLan/str-encry-plugin
插件是:str-encry-plugin
-
1.使用之前先将本模块提交到你本地maven仓库 具体步骤:点击Gradle,选择str-encry-plugin模块,之后打开Tasks->upload->点击uploadArchives上传
-
2.上传之后可以引入项目,引入步骤,在根目录的build.gradle引入
//本地仓库
maven { url uri('F:/maven/repo') }
classpath 'com.kawa.plugin:str-encry-plugin:1.0.0'
- 3.使用的模块
apply plugin: 'StringEncry'
stringEncry {
//加***,默认[1,2,3,4,5]
decryptKey = [1,2,6,4,5]
//需要加密指定目录
stringEncryDirList = [
"com/kawa/strencrydemo/url"
]
//需要加密指定文件
stringEncryList = [
"TestStr",
]
//解密路径
decryptFile = "com/kawa/strencrydemo/XorUtils"
//解密方法
decryptMethod = "xor_go"
}
上面用到的加密都是简简单单的异或加密
JNI版:
int key[] = {1, 2, 6, 4, 5};//加密字符**
//异或加密
void xor_go(char *pstr, int *pkey) {
int len = strlen(pstr);//获取长度
for (int i = 0; i < len; i++) {
*(pstr + i) = ((*(pstr + i)) ^ (pkey[i % 5]));
}
}
/**
* 执行异或加密
*/
JNIEXPORT jstring JNICALL str_go
(JNIEnv *env, jclass, jstring str) {
const char *buf_name = env->GetStringUTFChars(str, 0);
if (buf_name == NULL) {
return NULL;
}
int len = strlen(buf_name);
char value[len];
strcpy(value, buf_name);
//2. 释放内存
env->ReleaseStringUTFChars(str, buf_name);
char *p = value;
xor_go(value, key);
return env->NewStringUTF(p);
}
java版:
public static byte[] keyBytes = {1, 2, 3, 4, 5};
/**
* 异或加密解密
*
* @param enc
* @return
*/
public static String xor_go(String enc) {
byte[] b = enc.getBytes(Charset.forName("UTF-8"));
for (int i = 0; i < b.length; i++) {
b[i] = (byte) (b[i] ^ (keyBytes[i % keyBytes.length]));
}
return new String(b);
}
如需自定义加密方法,请修改NativeStringGuard这个文件
修改class文件代码是参考了四哥的 http://www.520monkey.com/archives/1313 此插件的目的是为了更方便的加密字符串
参考:
https://www.jianshu.com/p/882325acd200
https://www.jianshu.com/p/f43d48abfbf3
https://blog.****.net/huachao1001/article/details/51819972
https://www.jianshu.com/p/9039a3e46dbc