• unity打成aar上传到maven库的工具


    需求:

    把unity打成aar并上传到maven库

    其实就是把前两个博客整合了一下

    unity打aar包工具

    aar上传maven库工具

    这里先说eclipse版的

    package com.jinkejoy.build_aar;
    
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    
    public class EcAarBuildUpload {
        private static final String BUILD_PROJECT_PATH = "./aar-build";
        private static final String UPLOAD_PROJECT_PATH = "./aar-upload";
    
        private JFrame jFrame;
    
        private JTextField sourceText;
        private JButton sourceButton;
        private File sourceFile;
    
        private JTextField sdkText;
        private JButton sdkButton;
        private File sdkFile;
    
        private JTextField ndkText;
        private JButton ndkButton;
        private File ndkFile;
    
        private JTextField groupIdText;
        private JTextField aarNameText;
        private JTextField versionText;
    
        private JButton buildButton;
        private JButton uploadButton;
    
        public static void main(String[] args) {
            new EcAarBuildUpload();
        }
    
        public EcAarBuildUpload() {
            openFileWindow();
        }
    
        private void openFileWindow() {
            jFrame = new JFrame();
            jFrame.setTitle("将android工程打成aar并上传到maven库");
            jFrame.setBounds(500, 500, 700, 250);
            jFrame.setVisible(true);
            FlowLayout layout = new FlowLayout();
            layout.setAlignment(FlowLayout.LEFT);
            //选择文件
            JLabel sourceLabel = new JLabel("工程路径:");
            sourceText = new JTextField(50);
            sourceButton = new JButton("浏览");
            //输出路径
            JLabel sdkLabel = new JLabel("本地sdk路径:");
            sdkText = new JTextField(48);
            sdkButton = new JButton("浏览");
            //sdk
            JLabel ndkLabel = new JLabel("本地ndk路径:");
            ndkText = new JTextField(48);
            ndkButton = new JButton("浏览");
            //上传aar
            JLabel groupIdLabel = new JLabel("aar前缀包名:");
            groupIdText = new JTextField(54);
            JLabel aarNameLabel = new JLabel("aar名称:");
            aarNameText = new JTextField(56);
            JLabel versionLabel = new JLabel("aar版本号:");
            versionText = new JTextField(55);
            buildButton = new JButton("构建aar");
            uploadButton = new JButton("上传aar");
    
    
            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jFrame.setLayout(layout);
            jFrame.setResizable(false);
            jFrame.add(sourceLabel);
            jFrame.add(sourceText);
            jFrame.add(sourceButton);
            jFrame.add(sdkLabel);
            jFrame.add(sdkText);
            jFrame.add(sdkButton);
            jFrame.add(ndkLabel);
            jFrame.add(ndkText);
            jFrame.add(ndkButton);
            jFrame.add(groupIdLabel);
            jFrame.add(groupIdText);
            jFrame.add(aarNameLabel);
            jFrame.add(aarNameText);
            jFrame.add(versionLabel);
            jFrame.add(versionText);
            jFrame.add(buildButton);
            jFrame.add(uploadButton);
    
            chooseSourceFile();
            chooseSdkFile();
            chooseNdkFile();
    
            buildAarButton();
            uploadAarButton();
    
            getCacheInput();
        }
    
        private void getCacheInput() {
            sourceText.setText(CacheUtils.getCacheInput("sourcePath"));
            sdkText.setText(CacheUtils.getCacheInput("sdkPath"));
            ndkText.setText(CacheUtils.getCacheInput("ndkPath"));
            groupIdText.setText(CacheUtils.getCacheInput("groupId"));
            aarNameText.setText(CacheUtils.getCacheInput("aarName"));
            versionText.setText(CacheUtils.getCacheInput("version"));
        }
    
        private void buildAar() {
            if (checkInput()) return;
            CacheUtils.cacheInput(sourceText, sdkText, ndkText, groupIdText, aarNameText, versionText);
            createAsFile();
            findUpdateFile(BUILD_PROJECT_PATH);
            gradleBuildAar();
        }
    
    
        private void gradleBuildAar() {
            String command = "cmd /c start gradle clean assembleDebug";
            File cmdPath = new File(BUILD_PROJECT_PATH);
            Runtime runtime = Runtime.getRuntime();
            try {
                Process process = runtime.exec(command, null, cmdPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
        private void findUpdateFile(String filePath) {
            File file = new File(filePath);
            if (!file.exists()) {
                return;
            }
            File[] files = file.listFiles();
            for (File updateFile : files) {
                if (updateFile.isDirectory()) {
                    findUpdateFile(updateFile.getAbsolutePath());
                } else {
                    switch (updateFile.getName().toString()) {
                        case "build.gradle":
                            updateBuildGradle(updateFile.getAbsolutePath());
                            break;
                        case "AndroidManifest.xml":
                            updateManifestFile(updateFile.getAbsolutePath());
                            break;
                        case "local.properties":
                            updateSdkFile(updateFile.getAbsolutePath());
                            break;
                        case "UnityPlayerActivity.java":
                        case "UnityPlayerNativeActivity.java":
                        case "UnityPlayerProxyActivity.java":
                            updateFile.delete();
                            break;
                    }
                }
            }
        }
    
        private void updateSdkFile(String filePath) {
            try {
                RandomAccessFile sdkFile = new RandomAccessFile(filePath, "rw");
                String line;
                long lastPoint = 0;
                while ((line = sdkFile.readLine()) != null) {
                    final long point = sdkFile.getFilePointer();
                    if (line.contains("sdk.dir")) {
                        String s = line.substring(0);
                        String sdkStr = sdkText.getText().toString();
                        String sdkPan = sdkStr.substring(0, 1);
                        sdkStr = sdkStr.substring(1).replace("\", "\\");
                        String ndkStr = sdkText.getText().toString();
                        String ndkPan = ndkStr.substring(0, 1);
                        ndkStr = ndkStr.substring(1).replace("\", "\\");
                        String replaceStr = "sdk.dir=" + sdkPan + "\" + sdkStr + "
    " + "ndk.dir=" + ndkPan + "\" + ndkStr + "
                          ";
                        String str = line.replace(s, replaceStr);
                        sdkFile.seek(lastPoint);
                        sdkFile.writeBytes(str);
                    }
                    lastPoint = point;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private void updateManifestFile(String filePath) {
            try {
                RandomAccessFile manifestFile = new RandomAccessFile(filePath, "rw");
                String line;
                long lastPoint = 0;
                while ((line = manifestFile.readLine()) != null) {
                    final long ponit = manifestFile.getFilePointer();
                    if (line.contains("<activity") && line.contains("UnityPlayerActivity") && !line.contains("<!--<activity")) {
                        String str = line.replace("<activity", "<!--<activity");
                        manifestFile.seek(lastPoint);
                        manifestFile.writeBytes(str);
                    }
                    if (line.contains("</activity>") && !line.contains("</activity>-->")) {
                        String str = line.replace("</activity>", "</activity>-->
    ");
                        manifestFile.seek(lastPoint);
                        manifestFile.writeBytes(str);
                    }
                    lastPoint = ponit;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private void updateBuildGradle(String filePath) {
            try {
                RandomAccessFile buildGradleFile = new RandomAccessFile(filePath, "rw");
                String line;
                long lastPoint = 0;
                while ((line = buildGradleFile.readLine()) != null) {
                    final long ponit = buildGradleFile.getFilePointer();
                    if (line.contains("classpath 'com.android.tools.build:gradle")) {
                        String s = line.substring(line.indexOf("classpath"));
                        String str = line.replace(s, "classpath 'com.android.tools.build:gradle:2.3.0'  
    ");
                        buildGradleFile.seek(lastPoint);
                        buildGradleFile.writeBytes(str);
                    }
                    if (line.contains("com.android.application")) {
                        String str = line.replace("'com.android.application'", "'com.android.library'    
    ");
                        buildGradleFile.seek(lastPoint);
                        buildGradleFile.writeBytes(str);
                    }
                    if (line.contains("compileSdkVersion") && !line.contains("compileSdkVersion 25")) {
                        String s = line.substring(line.indexOf("compileSdkVersion")).toString();
                        String str = line.replace(s, "compileSdkVersion 25
    ");
                        buildGradleFile.seek(lastPoint);
                        buildGradleFile.writeBytes(str);
                    }
                    if (line.contains("buildToolsVersion") && !line.contains("buildToolsVersion '25.0.2'")) {
                        String s = line.substring(line.indexOf("buildToolsVersion")).toString();
                        String str = line.replace(s, "buildToolsVersion '25.0.2'
    ");
                        buildGradleFile.seek(lastPoint);
                        buildGradleFile.writeBytes(str);
                    }
                    if (line.contains("targetSdkVersion") && !line.contains("targetSdkVersion 25")) {
                        String s = line.substring(line.indexOf("targetSdkVersion")).toString();
                        String str = line.replace(s, "targetSdkVersion 25
    ");
                        buildGradleFile.seek(lastPoint);
                        buildGradleFile.writeBytes(str);
                    }
                    if (line.contains("applicationId") && !line.contains("//applicationId")) {
                        String s = line.substring(line.indexOf("applicationId")).toString();
                        String str = line.replace(s, "//" + s + "
    ");
                        buildGradleFile.seek(lastPoint);
                        buildGradleFile.writeBytes(str);
                    }
                    lastPoint = ponit;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private void createAsFile() {
            String sourcePath = sourceText.getText().toString();
            File buildFile = new File(BUILD_PROJECT_PATH);
            String buildProject = buildFile.getAbsolutePath();
            //delete history
            String deletePath = buildProject + "\app\src\main";
            FileUtils.delAllFile(deletePath);
            String buildPath1 = buildProject + "\build";
            FileUtils.delFolder(buildPath1);
            String buildPath2 = buildProject + "\app\build";
            FileUtils.delFolder(buildPath2);
            //assets
            String assets = sourcePath + "\assets";
            String newAssets = buildProject + "\app\src\main\assets";
            FileUtils.copyFolder(assets, newAssets);
            //unity-classes.jar
            String unity = sourcePath + "\libs\unity-classes.jar";
            String newUnity = buildProject + "\app\libs\unity-classes.jar";
            FileUtils.copyFile(new File(unity), new File(newUnity));
            //libs
            String libs = sourcePath + "\libs";
            String jniLibs = buildProject + "\app\src\main\jniLibs";
            FileUtils.copyFolder(libs, jniLibs);
            File jni_unity = new File(jniLibs + "\unity-classes.jar");
            jni_unity.delete();
            //res
            String res = sourcePath + "\res";
            String newRes = buildProject + "\app\src\main\res";
            FileUtils.copyFolder(res, newRes);
            //src
            String src = sourcePath + "\src";
            String java = buildProject + "\app\src\main\java";
            FileUtils.copyFolder(src, java);
            //AndroidManifest.xml
            String manifest = sourcePath + "\AndroidManifest.xml";
            String newManifest = buildProject + "\app\src\main\AndroidManifest.xml";
            FileUtils.copyFile(new File(manifest), new File(newManifest));
        }
    
    
        private void uploadAar() {
            findAarFile(BUILD_PROJECT_PATH);
            gradleUpload();
        }
    
        private void gradleUpload() {
            String command = "cmd /c start gradlew clean uploadArchives";
            File cmdPath = new File(UPLOAD_PROJECT_PATH);
            Runtime runtime = Runtime.getRuntime();
            try {
                Process process = runtime.exec(command, null, cmdPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private void findAarFile(String filePath) {
            File file = new File(filePath);
            if (!file.exists()) {
                return;
            }
            File[] files = file.listFiles();
            for (File outputFile : files) {
                if (outputFile.isDirectory()) {
                    findAarFile(outputFile.getAbsolutePath());
                } else {
                    String fileName = outputFile.getName().toString();
                    if (fileName.endsWith(".aar")) {
                        File aarFile = new File("./" + fileName);
                        FileUtils.copyFile(outputFile, aarFile);
                        CacheUtils.cacheAar(aarFile.getAbsolutePath());
                    }
                }
            }
        }
    
        private boolean checkInput() {
            if ("".equals(sourceText.getText().toString())) {
                JOptionPane.showMessageDialog(jFrame, "请输入源工程文件路径");
                return true;
            }
            if ("".equals(sdkText.getText().toString())) {
                JOptionPane.showMessageDialog(jFrame, "请输入本地sdk路径");
                return true;
            }
            if ("".equals(ndkText.getText().toString())) {
                JOptionPane.showMessageDialog(jFrame, "请输入本地ndk路径");
                return true;
            }
            if ("".equals(groupIdText.getText().toString())) {
                JOptionPane.showMessageDialog(jFrame, "请输入aar前缀包名");
                return true;
            }
            if ("".equals(aarNameText.getText().toString())) {
                JOptionPane.showMessageDialog(jFrame, "请输入aar名称");
                return true;
            }
            if ("".equals(versionText.getText().toString())) {
                JOptionPane.showMessageDialog(jFrame, "请输入aar版本号");
                return true;
            }
            return false;
        }
    
    
        private void buildAarButton() {
            buildButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    buildAar();
                }
            });
        }
    
        private void uploadAarButton() {
            uploadButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    uploadAar();
                }
            });
        }
    
        private void chooseSourceFile() {
            sourceButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    JFileChooser chooser = new JFileChooser();
                    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                    chooser.showDialog(new JLabel(), "选择");
                    sourceFile = chooser.getSelectedFile();
                    sourceText.setText(sourceFile.getAbsolutePath().toString());
                }
            });
        }
    
        private void chooseSdkFile() {
            sdkButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    JFileChooser chooser = new JFileChooser();
                    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                    chooser.showDialog(new JLabel(), "选择");
                    sdkFile = chooser.getSelectedFile();
                    sdkText.setText(sdkFile.getAbsolutePath().toString());
                }
            });
        }
    
        private void chooseNdkFile() {
            ndkButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    JFileChooser chooser = new JFileChooser();
                    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                    chooser.showDialog(new JLabel(), "选择");
                    ndkFile = chooser.getSelectedFile();
                    ndkText.setText(ndkFile.getAbsolutePath().toString());
                }
            });
        }
    }
    package com.jinkejoy.build_aar;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Properties;
    
    import javax.swing.JTextField;
    
    public class CacheUtils {
        private static final String CACHE_PATH = "./cache-input.properties";
        private static final String AAR_PATH = "./aar-path.properties";
    
        public static void cacheInput(JTextField sourceText, JTextField sdkText, JTextField ndkText,
                                      JTextField groupIdText, JTextField aarNameText, JTextField versionText) {
            String cache = "sourcePath=" + sourceText.getText().toString().replace("\", "\\") + "
    " +
                    "sdkPath=" + sdkText.getText().toString().replace("\", "\\") + "
    " +
                    "ndkPath=" + ndkText.getText().toString().replace("\", "\\") + "
    " +
                    "groupId=" + groupIdText.getText().toString().replace("\", "\\") + "
    " +
                    "aarName=" + aarNameText.getText().toString().replace("\", "\\") + "
    " +
                    "version=" + versionText.getText().toString().replace("\", "\\");
            File cacheFile = new File(CACHE_PATH);
            if (!cacheFile.exists()) {
                try {
                    cacheFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                FileOutputStream fop = new FileOutputStream(cacheFile);
                fop.write(cache.getBytes());
                fop.flush();
                fop.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static void cacheAar(String aarPath) {
            String cache = "aarPath=" + aarPath.replace("\", "\\");
            File cacheFile = new File(AAR_PATH);
            if (!cacheFile.exists()) {
                try {
                    cacheFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                FileOutputStream fop = new FileOutputStream(cacheFile);
                fop.write(cache.getBytes());
                fop.flush();
                fop.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static String getCacheInput(String key) {
            File cacheFile = new File(CACHE_PATH);
            if (cacheFile.exists()) {
                try {
                    FileInputStream fip = new FileInputStream(cacheFile);
                    Properties properties = new Properties();
                    properties.load(fip);
                    Iterator<Map.Entry<Object, Object>> iterator = properties.entrySet().iterator();
                    while (iterator.hasNext()) {
                        Map.Entry<Object, Object> entry = iterator.next();
                        if (key.equals(entry.getKey().toString())) {
                            return entry.getValue().toString();
                        }
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return "";
        }
    }
    package com.jinkejoy.build_aar;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.channels.FileChannel;
    
    public class FileUtils {
        public static boolean delAllFile(String path) {
            boolean flag = false;
            File file = new File(path);
            if (!file.exists()) {
                return flag;
            }
            if (!file.isDirectory()) {
                return flag;
            }
            String[] tempList = file.list();
            File temp = null;
            for (int i = 0; i < tempList.length; i++) {
                if (path.endsWith(File.separator)) {
                    temp = new File(path + tempList[i]);
                } else {
                    temp = new File(path + File.separator + tempList[i]);
                }
                if (temp.isFile()) {
                    temp.delete();
                }
                if (temp.isDirectory()) {
                    delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                    delFolder(path + "/" + tempList[i]);//再删除空文件夹
                    flag = true;
                }
            }
            return flag;
        }
    
        public static void delFolder(String folderPath) {
            try {
                delAllFile(folderPath); //删除完里面所有内容
                String filePath = folderPath;
                filePath = filePath.toString();
                java.io.File myFilePath = new java.io.File(filePath);
                myFilePath.delete(); //删除空文件夹
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void copyFile(File source, File dest) {
            FileChannel inputChannel = null;
            FileChannel outputChannel = null;
            try {
                inputChannel = new FileInputStream(source).getChannel();
                outputChannel = new FileOutputStream(dest).getChannel();
                outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    inputChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    outputChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static void copyFolder(String oldPath, String newPath) {
            try {
                // 如果文件夹不存在,则建立新文件夹
                (new File(newPath)).mkdirs();
                // 读取整个文件夹的内容到file字符串数组,下面设置一个游标i,不停地向下移开始读这个数组
                File filelist = new File(oldPath);
                String[] file = filelist.list();
                // 要注意,这个temp仅仅是一个临时文件指针
                // 整个程序并没有创建临时文件
                File temp = null;
                for (int i = 0; i < file.length; i++) {
                    // 如果oldPath以路径分隔符/或者结尾,那么则oldPath/文件名就可以了
                    // 否则要自己oldPath后面补个路径分隔符再加文件名
                    // 谁知道你传递过来的参数是f:/a还是f:/a/啊?
                    if (oldPath.endsWith(File.separator)) {
                        temp = new File(oldPath + file[i]);
                    } else {
                        temp = new File(oldPath + File.separator + file[i]);
                    }
    
                    // 如果游标遇到文件
                    if (temp.isFile()) {
                        FileInputStream input = new FileInputStream(temp);
                        // 复制并且改名
                        FileOutputStream output = new FileOutputStream(newPath
                                + "/" + (temp.getName()).toString());
                        byte[] bufferarray = new byte[1024 * 64];
                        int prereadlength;
                        while ((prereadlength = input.read(bufferarray)) != -1) {
                            output.write(bufferarray, 0, prereadlength);
                        }
                        output.flush();
                        output.close();
                        input.close();
                    }
                    // 如果游标遇到文件夹
                    if (temp.isDirectory()) {
                        copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
                    }
                }
            } catch (Exception e) {
                System.out.println("复制整个文件夹内容操作出错");
            }
        }
    }

    效果图

    备注:有时候会出现渲染加载不出来的问题,可以修改下布局的创建顺序,改为创建一个控件就马上加载

  • 相关阅读:
    scala简单的功能实现~weektwo
    linux中/dev/null与2>&1讲解
    scala简单的功能实现~weekone
    oracle之随机数
    oracle之percent_rank() over()函数和PERCENTILE_CONT() within group()over()函数详解
    awk编程的基本用法
    Django-路由系统
    selectors模块
    threading中定时器Timer方法
    HTML-W3school首页制作
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/9234411.html
Copyright © 2020-2023  润新知