由于公司项目的组件化的方式,各个组件之间需要不同的资源前缀,上网google半天都没有较好的工具的(ps:对于android的prefix 只是lint检测的一个项,并不能直接改代码),根据需求,要考虑,资源引用解决的问题,(java,xml中引用了你的重命名文件需要改掉等等。。。),看下效果

android moudle 资源文件重命名_第1张图片

 

,于是自己撸了一个直接上代码

package com.yingzi.common.test;import java.io.BufferedReader;import java.io.CharArrayWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.RandomAccessFile;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * @author yich  2016928148@qq.com * @Description:android moudle资源重命名的工具类 * @date 2018/9/13  下午2:03 */public class MoudleRenameResMain {    public  static  final String FILE_JAVA=".java";    public  static  final String FILE_START_DOT=".";    private static final String FILE_XML=".xml";    private static boolean IS_TEST=false;    /**     * 重命名资源的名字task     * @param renameModulePath     * @param modulePrefix     */    private static void doRenameTask(String renameModulePath, String modulePrefix) {        File fileDir = new File(renameModulePath);        if (fileDir!=null)        {            String renameResPath=renameModulePath+"/src/main/res/";            File renameFileDir=new File(renameResPath);            String workSpaceDir=renameModulePath+"/src/main/";            File[] readyFiles=renameFileDir.listFiles();            renameResFiles(readyFiles,modulePrefix,workSpaceDir);        }    }    private static void renameResFiles(File[] files, String modulePrefix,String workSpacePath) {        if (files==null){            System.out.println("进行重命名的文件下没有任何文件");            return;        }        for (File fileItem:files                ) {            if (fileItem!=null&&fileItem.isDirectory()&&filterReplaceResFile(fileItem.getName())){                File[] readyFiles=fileItem.listFiles();                renameResFiles(readyFiles,modulePrefix,workSpacePath);            }else{//                if (IS_TEST){//                    if (!fileItem.getName().contains("scan_")){//                        continue;//                    }//                }                renameSingleFile(fileItem,modulePrefix,workSpacePath);            }        }    }    private static boolean filterReplaceResFile(String fileItem) {        return fileItem!=null&&(!fileItem.startsWith(FILE_START_DOT));    }    /**     * 重命名单个文件     * @param fileItem     * @param modulePrefix     * @param workSpacePath     */    private static void renameSingleFile(File fileItem, String modulePrefix,String workSpacePath) {        System.out.println("重命名的文件:"+fileItem.getParent()+"/"+fileItem.getName());        String path =fileItem.getParent();        String fileName =fileItem.getName();        if (!fileName.startsWith(modulePrefix)){            String newFileName=modulePrefix+"_"+fileName;            if (fileItem.renameTo(new File(path+"/"+newFileName))) {                System.out.println("重命名为"+newFileName+"成功!准备解决引用");                resolveLinks(path,fileName.substring(0, fileName.indexOf(".")),newFileName.substring(0, newFileName.indexOf(".")),workSpacePath);            }            else{                System.out.println(path+fileName+"修改失败");            }        }    }    /**     * 解决重命名之后的引用问题     * @param fileName 老文件名     * @param newFileName 新文件名     * @param workSpacePath  工作的文件路径     */    private static void resolveLinks(String path,String fileName, String newFileName, String workSpacePath) {        File workSpaceDir=new File(workSpacePath);        File[] workFiles=workSpaceDir.listFiles();        if (workFiles!=null){            for (File fileItem:                    workFiles) {                if (fileItem!=null&& fileItem.isDirectory()){                    resolveLinks(path,fileName,newFileName,fileItem.getPath());                }else{                    doCheckFile(path,fileName,newFileName,fileItem);                }            }        }    }    /**     * 对单个文件进行读取和检查     * @param fileName 不包含文件后缀     * @param newFileName 不包含文件后缀     * @param fileItem 要检查的java或者xml文件     */    private static void doCheckFile(String path,String fileName, String newFileName, File fileItem) {        if (filterFile(fileItem.getName())){//            if (IS_TEST){//                if (!fileItem.getName().contains("SecondaryChildbirthFragment")){//                    return;//                }//            }            modifyFileContent1(path,fileItem,fileName,newFileName,fileItem.getName().endsWith(FILE_JAVA));        }    }    /**     * 过滤了相关文件     * @param name     * @return     */    private static boolean filterFile(String name) {        return name!=null&&(name.endsWith(FILE_JAVA)||name.endsWith(FILE_XML));    }    /**     * 修改文件     * @param file     * @param oldstr     * @param newStr     * @return     */    private static boolean modifyFileContent1(String path,File file, String oldstr, String newStr,Boolean isJavaFile) {        boolean hasReturn=hasReturnInEndOfFile(file);        FileReader in = null;        try {            in = new FileReader(file);            BufferedReader bufIn = new BufferedReader(in);            // 内存流, 作为临时流            CharArrayWriter tempStream = new CharArrayWriter();            // 替换            String line = null;            String nextLine=null;            int count =0;            line = bufIn.readLine();            while ( line!= null) {                count++;                line=doReplaceString(isJavaFile,oldstr,newStr,line,getPathSuffix(path));            /*){                System.out.println("【资源修改】:"+line+",第"+count+"行开始");                // 替换每行中, 符合条件的字符串                line = line.replaceAll(oldstr, newStr);                System.out.println("【资源修改】:"+line+",第"+count+"行完成");            }*/                // 将该行写入内存                tempStream.write(line);//                if (IS_TEST&&file.getName().contains("MainAc")){//                    System.out.println(line);//                }                nextLine=bufIn.readLine();                if (nextLine==null){                    // 添加换行符                    if (hasReturn){                        tempStream.append(System.getProperty("line.separator"));                    }                    else{                    }                    break;                }else{                    // 添加换行符                    tempStream.append(System.getProperty("line.separator"));                    line=nextLine;                }            }            // 关闭 输入流            bufIn.close();            // 将内存中的流 写入 文件            FileWriter out = new FileWriter(file);            tempStream.writeTo(out);            out.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return true;    }    /**     * 根据资源文件的path来的后缀来决定要改字符串的前缀,R.drawable.xxx 要修改drawable类型的所有符合条件的字符     * @param path     * @return     */    private static String getPathSuffix(String path) {        String sufix=path.substring(path.lastIndexOf("/")+1,path.length());        if (sufix.contains("-")){//针对drawable-hdpi类型的文件夹            return  sufix.substring(0,sufix.indexOf("-"));        }        if (IS_TEST){            System.out.println(path+"haha:"+sufix);        }        return  sufix;    }    /**     * 替换字符串里面匹配的字符串     * @param isJavaFile     * @param oldstr     * @param newStr     * @param line     * @param endParentPath     * @return     */    private static String doReplaceString(Boolean isJavaFile,String oldstr,String newStr, String line,String endParentPath) {        if (endParentPath.equals("values")){//values资源文件夹下的东西直接重命名            return  line;        }else {            endParentPath=endParentPath;        }        String wrappLine=";"+line+";";//进行前后加字符来匹配所需要的字符(如果前后位空行,readline出来的字符串前面后都没有字符的话如果匹配字符的非字符和数字和下划线)        String  noNumNolatterReg="([^_|a-z|A-Z|0-9])";//非字母非数字或者为空的字符        if (isJavaFile){//前后都不是数字文字下划线之外的字符或者是后面或者前面什么字符都没有//             System.out.println("替换前:"+line);            String reg=noNumNolatterReg+"(R."+endParentPath+"."+oldstr+")"+noNumNolatterReg;            Pattern p = Pattern.compile(reg);            Matcher matcher=p.matcher(wrappLine);            if (matcher.find()){                System.out.println("java文件中匹配到"+wrappLine);                line=  wrappLine.replaceAll(reg,"$1R."+endParentPath+"."+newStr+"$3");//只替换前面的字符串                line= line.replaceFirst(";","");                line=line.substring(0,line.length()-1);            }//             System.out.println("替换后:"+line);        }else{//如果是xml的文件匹配 前后都不是数字文字下划线之外的字符或者是后面或者前面什么字符都没有//             System.out.println("替换前:"+line);            String reg=noNumNolatterReg+"(@"+endParentPath+"/"+oldstr+")"+noNumNolatterReg;//            System.out.println(reg);            Pattern p = Pattern.compile(reg);            Matcher matcher=p.matcher(wrappLine);//             return  line.matches("R."+endParentPath+"."+oldstr+"[^a-zA-Z0-9]*");            if (matcher.find()){                System.out.println("xml文件中匹配到"+wrappLine);                line= wrappLine.replaceAll(reg,"$1@"+endParentPath+"/"+newStr+"$3");                line= line.replaceFirst(";","");                line=line.substring(0,line.length()-1);            }else{//如果没有匹配到就直接返回就好了            }//             System.out.println("替换后:"+line);        }        return line;    }    public static void main(String[] args) {        //todo 相关参数设置        String renameModulePath="/Users/qiyu/Desktop/yingzi/mainshow-all/yingzi-android-futurefarm-breed/breed_app";//要重命名的moudle的绝对路径        String modulePrefix="breed";//要添加的moudle的前缀        doRenameTask(renameModulePath,modulePrefix);    }    public static boolean hasReturnInEndOfFile(File file){        try (RandomAccessFile raFile = new RandomAccessFile(file, "rw");) {            raFile.seek(file.length() - 1);            int flag=raFile.read();            Boolean hasReturn=10 == flag;//            System.out.println(file.getName()+"有换行"+hasReturn+flag);            raFile.close();            return hasReturn;        } catch (Exception e) {        }        return false;    }}

如上代码所示:在main函数里面修改自己的moudle和需要的前缀和跑main函数即可

 

更多相关文章

  1. CrossWalk - android 动态加载so库文件实践
  2. Android中使用pull解析器操作xml文件的解决办法
  3. rdp文件和vnc软件
  4. Android so 文件进阶 从dlsym()源码看android 动态链接过程
  5. Android使用JNI生成.so文件并调用(使用传统生成.h的方法)
  6. Android本地视频播放器开发--ffmpeg解码视频文件中的音频(1)
  7. android之文件上传

随机推荐

  1. h5页面点击按钮,触发手机自带的发短信
  2. Android(安卓)ScrollView的使用
  3. 【Android-tips】 Unable to execute dex
  4. Android(安卓)高德地图API学习笔记
  5. android中GridView关于间距的属性值介绍
  6. FIDO框架分析3(FIDO UAF Android客户端)
  7. Android如何获取视频预览图(或首帧)和获取
  8. Android(安卓)AppCompatActivity的Action
  9. Android计算地图上两点距离
  10. android10.0连接wifi后提示“已连接,但无