获取apk的包名然后重命名apk 需要appt.exe

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 + '\'' +
                    '}';
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值