获取apk的包名然后重命名apk 需要appt.exe
2016-03-09 17:50
453 查看
package com.yws; import java.io.File; import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; /** * Created by Administrator on 2016/3/9. * 获取apk的包名然后重命名apk 需要appt.exe */ public class GetPackageNameAndRenameAPKFile { static boolean isShowAppName=true; public static void main(String[] args) throws IOException { Files.walkFileTree(Paths.get("D:\\apk\\"),new SimpleFileVisitor<Path>(){ @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { try { if(file.toFile().getAbsolutePath().endsWith(".apk")) { dosomething(file.toFile()); } } catch (Exception e) { e.printStackTrace(); } return super.visitFile(file, attrs); } }); } private static void dosomething(File file) { //aapt d badging xxx.apk String path=file.getAbsolutePath(); /* if(JavaShellUtil.isWindow) { path=path.replaceAll("\\\\","\\\\\\\\"); }else{ path=path.replaceAll("/","//"); }*/ System.out.println(path); //在c盘桌面目录需要权限执行aapt 放在其他盘目录即可 JavaShellUtil.CommandResult commandResult=JavaShellUtil.execCommand("aapt d badging \""+path+"\""); if(commandResult.result!=0) { System.out.println("ret="+commandResult); } String content=commandResult.responseMsg; String appName=null; String packageName=null; int index=content.indexOf("package: name="); if(index!=-1) { String data=content.substring(index,content.length()-1); String[] lines=data.split("'"); if(lines.length>1) { packageName=lines[1]; } } index=content.indexOf("application-label"); if(index!=-1) { String data=content.substring(index,content.length()-1); String[] lines=data.split("'"); if(lines.length>1) { appName=lines[1]; } } System.out.println(appName); System.out.println(packageName); if(appName!=null && packageName!=null) { String fileName=file.getName(); String prefix=fileName.substring(fileName.lastIndexOf(".")+1); String newname=packageName+"_"+appName+"."+prefix; if(isShowAppName) { newname=packageName+"_"+appName+"."+prefix; }else{ newname=packageName+"."+prefix; } System.out.println(newname); File newfile=new File(file.getParentFile(),newname); if(newfile.exists()) { System.err.println(file.getAbsolutePath()+"=>"+newfile.getAbsolutePath()+" 已经存在!"); } file.renameTo(newfile); System.out.println(file.getAbsolutePath()); } } }
package com.yws; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; /** java shell命令工具类 by yunshouhu javac JavaShellUtil.java -encoding utf-8 */ public class JavaShellUtil { public static String lineseparator=System.getProperty("line.separator"); public static String COMMAND_SH = "sh"; public static String COMMAND_EXIT = "exit\n"; public static String COMMAND_LINE_END = "\n"; public static boolean isWindow = true; static { if(System.getProperty("os.name").toUpperCase().indexOf("WINDOWS")!=-1) { System.out.println("window"); isWindow=true; COMMAND_SH="cmd"; }else{ System.out.println("unix"); isWindow=false; } } public static void main(String[] args) { System.out.println(JavaShellUtil.execCommand("dir").toString()); System.out.println(JavaShellUtil.execCommand("ls -l").toString()); //System.out.println(JavaShellUtil.execCommand("ping www.baidu.com").toString()); System.out.println(JavaShellUtil.execCommand("aapt v").toString()); System.out.println(JavaShellUtil.execCommand("aapt.exe").toString()); } public static CommandResult execCommand(String command) { return execCommand(new String[] {command}, true); } public static CommandResult execCommand(String command, boolean isNeedResultMsg) { return execCommand(new String[]{command}, isNeedResultMsg); } public static CommandResult execCommand(List<String> commands, boolean isNeedResultMsg) { return execCommand(commands == null ? null : commands.toArray(new String[]{}), isNeedResultMsg); } /** * execute shell commands * {@link CommandResult#result} is -1, there maybe some excepiton. * * @param commands command array * @param needResponse whether need result msg */ public static CommandResult execCommand(String[] commands, final boolean needResponse) { int result = -1; if (commands == null || commands.length == 0) { return new CommandResult(result, null, "空命令"); } Process process = null; final StringBuilder successMsg = new StringBuilder(); final StringBuilder errorMsg = new StringBuilder(); DataOutputStream os = null; try { process = Runtime.getRuntime().exec(COMMAND_SH); os = new DataOutputStream(process.getOutputStream()); for (String command : commands) { if (command == null) { continue; } // donnot use os.writeBytes(commmand), avoid chinese charset error os.write(command.getBytes()); os.writeBytes(COMMAND_LINE_END); os.flush(); } os.writeBytes(COMMAND_EXIT); os.flush(); final BufferedReader successResult = new BufferedReader(new InputStreamReader(process.getInputStream())); final BufferedReader errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream())); //http://249wangmang.blog.163.com/blog/static/52630765201261334351635/ new Thread(new Runnable() { public void run() { try { if (needResponse) { String s; while ((s = successResult.readLine()) != null) { successMsg.append(s); successMsg.append(lineseparator); } } } catch (IOException e) { e.printStackTrace(); } } }).start(); //启动两个线程,解决process.waitFor()阻塞问题 new Thread(new Runnable() { public void run() { try { if (needResponse) { String s; while ((s = errorResult.readLine()) != null) { errorMsg.append(s); errorMsg.append(lineseparator); } } } catch (IOException e) { e.printStackTrace(); } } }).start(); result = process.waitFor(); if (errorResult != null) { errorResult.close(); } if (successResult != null) { successResult.close(); } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } finally { if (process != null) { process.destroy(); } } } return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null : errorMsg.toString()); } public static class CommandResult { public int result; public String responseMsg; public String errorMsg; public CommandResult(int result) { this.result = result; } public CommandResult(int result, String responseMsg, String errorMsg) { this.result = result; this.responseMsg = responseMsg; this.errorMsg = errorMsg; } @Override public String toString() { return "CommandResult{" + "errorMsg='" + errorMsg + '\'' + ", result=" + result + ", responseMsg='" + responseMsg + '\'' + '}'; } } }
相关文章推荐
- 自定闹钟改良版
- iOS NSPredicate用法详解
- Android应用性能优化之使用SparseArray替代HashMap
- Hammer.js 实现移动端元素的拖拽库
- iOS自定义TabBarViewController
- Android布局:简单是所有布局开始最重要的原则
- Android开发中,关于心跳包的简要介绍
- Swift的初步学习第一步——变量的声明和set,get,?,!,别名
- 在android中,关于去掉标题栏的问题
- Notes about VectorDrawable
- 升级IOS9,提示滑动升级,卡在password锁屏界面,无反应了
- Android应用读取Excel文件的方法
- appium使用ID进行定位click点击事件
- 解决iOS的Safari浏览器解析绑定了onclick事件闪烁的问题
- 过滤Android系统设置中Wifi
- 微信朋友圈的推广广告怎么做好?
- Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用
- Android databinding 开发参考
- iOS开发--3D Touch使用代码实例
- Android 四大组件学习之 Service 服务