利用python脚本预埋app信息
一常见的Python命令
1、sys.argv[]是用来获取命令行参数的
例如执行语句
python packer.py path a.apk b68b21a01dc536f5 10016180 path当前目录路径就OK
sys.argv[1] 为 path
sys.argv[2] a.apk
sys.argv[3] b68b21a01dc536f5 10016180
2、%s是格式化输出
pub = 'com.android.pid=%s' % youb
格式化后相当于java里的pub=“com.android.pid=youb[可用具体值代替]”
3、os.chdir(path) 修改当前工作目录到指定目录
os.getcwd() 获取当前工作目录
二下载地址
三使用方法
终端切换至Python所在的目录, 执行以下Python 命令
格式为:
Python+空格+python_packer.py(脚本名字)+空格 +当前目录+空格+要打包apk名字+空格 注入的第一个参数oID(不需要的话传-1)+空格+参数二bID(不需要传-1)+空格+参数三cID(不需要时传-1)+空格+参数四hid(不需要时传-1)
例如:
python python_packer.py /Users/xwb/PythonProject/beforetest/writePidAndChannelId/pyte my.apk XQWUWETQ0 -1 -1 -1
四、写入信息后在APP启动后获取
public class ZipUtils {
//解析注入信息的实体类
public static class CommentInfo {
public String Aid;
public String Bid;
public String Cid;
public String Did;
}
//假如解析String的格式为String comment ="com.android.Aid=77777,com.android.Bid=iiiii,
// com.android.Cid=ddd,com.android.DId=ddd"
public static CommentInfo getCommentInfoFromPy(String path) {
CommentInfo commentInfo = null;
String comment = extractZipComment(path);
Log.d("flag", "---------------------zipComment" + comment);
if (!TextUtils.isEmpty(comment)) {
String[] args = comment.split(",");
if (args.length >= 4) {
commentInfo = new CommentInfo();
commentInfo.Aid= args[0].substring(args[0].indexOf("=") + 1);
commentInfo.Bid = args[1].substring(args[1].indexOf("=") + 1);
commentInfo.Cid = args[2].substring(args[2].indexOf("=") + 1);
commentInfo.Did = args[3].substring(args[3].indexOf("=") + 1);
} else if (args.length == 3) {
commentInfo = new CommentInfo();
commentInfo.Aid = args[0].substring(args[0].indexOf("=") + 1);
commentInfo.Bid = args[1].substring(args[1].indexOf("=") + 1);
commentInfo.Cid = args[2].substring(args[2].indexOf("=") + 1);
} else if (args.length == 2) {
commentInfo = new CommentInfo();
commentInfo.Aid = args[0].substring(args[0].indexOf("=") + 1);
commentInfo.Bid = args[1].substring(args[1].indexOf("=") + 1);
}
}
return commentInfo;
}
private static final int SIZE_1K = 1024;
public static String extractZipComment(String filename) {
String retStr = null;
try {
File file = new File(filename);
int fileLen = (int) file.length();
FileInputStream in = new FileInputStream(file);
/*
* The whole ZIP comment (including the magic byte sequence)
* MUST fit in the buffer
* otherwise, the comment will not be recognized correctly
* You can safely increase the buffer size if you like
*/
byte[] buffer = new byte[Math.min(fileLen, SIZE_1K)];
int len;
in.skip(fileLen - buffer.length);
if ((len = in.read(buffer)) > 0) {
retStr = getZipCommentFromBuffer(buffer, len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return retStr;
}
private static String getZipCommentFromBuffer(byte[] buffer, int len) {
byte[] magicDirEnd = {0x50, 0x4b, 0x05, 0x06};
int buffLen = Math.min(buffer.length, len);
// Check the buffer from the end
for (int i = buffLen - magicDirEnd.length - 22; i >= 0; i--) {
boolean isMagicStart = true;
for (int k = 0; k < magicDirEnd.length; k++) {
if (buffer[i + k] != magicDirEnd[k]) {
isMagicStart = false;
break;
}
}
if (isMagicStart) {
// Magic Start found!
int commentLen = buffer[i + 20] + buffer[i + 22] * 256;
int realLen = buffLen - i - 22;
System.out.println("ZIP comment found at buffer position " + (i + 22) + " with len="
+ commentLen + ", good!");
if (commentLen != realLen) {
System.out.println("WARNING! ZIP comment size mismatch: directory says len is " +
commentLen + ", but file ends after " + realLen + " bytes!");
}
String comment = new String(buffer, i + 22, Math.min(commentLen, realLen));
return comment;
}
}
System.out.println("ERROR! ZIP comment NOT found!");
return null;
}
}
五、根据工具类获取信息
String path = context.getPackageResourcePath();
ZipUtils.CommentInfo info = ZipUtils.getCommentInfoFromPy(path);