Kode Java Software Backup Project Android Studio
Salah satu kewajiban menjadi developer Android adalah diperlukannya backup source code app berkala.
Hal ini penting dilakukan, sebagai cadangan, jika terjadi hal-hal yang tidak diinginkan, seperti terjadinya rusak atau bad sector pada hardisk device yang digunakan untuk membuat aplikasi.
Masalahnya, file-file project app Android dengan Android Studio, biasanya jumlahnya banyak, meskipun ukurannya kecil-kecil.
Dan menurut penulis, di dalamnya lebih banyak file yang hanya akan memakan space memory, jika penyimpanannya secara satu project penuh.
Mungkin pada sebagian developer tidak akan mempermasalahkan keadaan ini.
Tetapi jika jumlah app-nya sudah banyak, seperti edugameapp penulis, akan lebih baik untuk menyimpan source code dan file-file yang diperlukan saja, yang memungkinkan untuk menyimpan di Cloud Service seperti di Google Drive, sebagai cadangan yang lain.
Oleh karena itu, kode Java di bawah ini, penulis menawarkan software sederhana berbasis dekstop, yang berguna untuk mengambil source code dan file-file penting dari suatu project app dengan Android Studio, untuk disimpan ke folder yang lain.
Dengan kode ini pula, penulis telah berhasil meminimalisasi penyimpanan hingga 10% dari ukuran semua file app dan game edugameapp sebenarnya. Sehingga memungkinkan penulis untuk menyimpannya di akun Google Drive, yang berukuran maksimal 15Gb.
package save.android.app; import java.awt.Component; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; /** * * @author Edugameapp */ public class SaveAndroidApp extends JFrame { JTextField pathsource=new JTextField(); JTextField pathtarget=new JTextField(); JTextArea keterangan=new JTextArea(10, 50); JButton mainbtn=new JButton(); String[] dirnamecheck=new String[]{".gradle",".idea","app","gradle","build.gradle"}; String[] filecheck=new String[]{"gradle","jks","pepk"}; private String getExtension(String pathname) { String extension = ""; int i = pathname.lastIndexOf('.'); int p = Math.max(pathname.lastIndexOf('/'), pathname.lastIndexOf('\\')); if (i > p) { extension = pathname.substring(i+1); } return extension; } public boolean checkIsAndroid(File f) { String[] pathnames = f.list(); int num=0; if(pathnames!=null)for(String pathname : pathnames){ for(int i=0;i<dirnamecheck.length;i++){ if(pathname.equals(dirnamecheck[i])){ num++; break; } } } if(dirnamecheck.length==num)return true; return false; } public static void copyFile(File srcFile,File dstFile) throws IOException { dstFile.getParentFile().mkdirs(); Files.copy( srcFile.toPath(), dstFile.toPath(), StandardCopyOption.REPLACE_EXISTING ); } public void subworking(int depth,String src,String wrk,boolean oncopy) { File f = new File(src); File[] pathnames = f.listFiles(); for (File pathname : pathnames){ if(!pathname.isDirectory()){ if(depth==0){ for(int i=0;i<filecheck.length;i++){ if(getExtension(pathname.getName()).equals(filecheck[i])){ try{ copyFile(pathname,new File(wrk+"\\"+pathname.getName())); }catch(IOException e){ keterangan.append("Failed copy:"+pathname.getName()+"\n"); } } } }else{ if(oncopy){ try{ copyFile(pathname,new File(wrk+"\\"+pathname.getName())); }catch(IOException e){ keterangan.append("Failed copy:"+pathname.getName()+"\n"); } } } }else{ if(oncopy || pathname.getName().equals("app") || pathname.getName().equals("src") || pathname.getName().equals("main")){ String m=wrk+"\\"+pathname.getName(); if(!(new File(m)).exists()){ new File(m).mkdirs(); } boolean cp=oncopy; if(!oncopy && pathname.getName().equals("main"))cp=true; subworking(depth+1,src+"\\"+pathname.getName(),m,cp); } } } } public void working() { if(pathsource.getText().isEmpty()||pathtarget.getText().isEmpty()){ keterangan.append("Source and target must be not empty!"); return; } keterangan.setText(""); File f = new File(pathsource.getText()); File[] files = f.listFiles(); for (File ff : files){ if(ff.isDirectory()){ if(checkIsAndroid(ff)){ String m=pathtarget.getText()+"\\"+ff.getName(); if(!(new File(m)).exists()){ new File(m).mkdirs(); } keterangan.append(ff.getName()+"\n"); subworking(0,ff.getAbsolutePath(),m,false); } } } keterangan.append("done.."); } public void init() { setTitle("Save My Android"); JPanel panel = new JPanel(); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; panel.setLayout(new GridBagLayout()); JLabel label = new JLabel("source"); c.weightx = 0.0; c.weighty = 0.0; c.gridx = 0; c.gridy = 0; panel.add(label,c); c.weightx = 1.0; c.weighty = 0.0; c.gridx = 1; c.gridy = 0; panel.add(pathsource,c); JButton button = new JButton(); button.setText("Set"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { new Thread(new Runnable() { @Override public void run() { pilihan(0); } }).start(); } }); c.weightx = 0.0; c.weighty = 0.0; c.gridx = 2; c.gridy = 0; panel.add(button,c); label = new JLabel("target"); c.weightx = 0.0; c.weighty = 0.0; c.gridx = 0; c.gridy = 1; panel.add(label,c); c.weightx = 1.0; c.weighty = 0.0; c.gridx = 1; c.gridy = 1; panel.add(pathtarget,c); button = new JButton(); button.setText("Set"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { new Thread(new Runnable() { @Override public void run() { pilihan(1); } }).start(); } }); c.weightx = 0.0; c.weighty = 0.0; c.gridx = 2; c.gridy = 1; panel.add(button,c); mainbtn.setText("Copy"); mainbtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { new Thread(new Runnable() { @Override public void run() { working(); } }).start(); } }); c.weightx = 0.0; c.weighty = 0.0; c.gridx = 0; c.gridy = 2; c.gridwidth=3; panel.add(mainbtn,c); keterangan.setLineWrap(true); keterangan.setWrapStyleWord(true); JScrollPane scroll = new JScrollPane(keterangan,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); c.weightx = 1.0; c.weighty = 1.0; c.gridx = 0; c.gridy = 3; c.gridwidth=3; panel.add(scroll,c); add(panel); addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent evt) { Component c = (Component)evt.getSource(); validate(); } }); setSize(400, 200); panel.setMinimumSize(new Dimension(400, 200)); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } public void pilihan(int mode) { String a=(mode==1)?pathtarget.getText():pathsource.getText(); JFileChooser j = new JFileChooser((a.isEmpty())?System.getProperty("user.dir"):a); j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int r = j.showSaveDialog(null); if (mode==1) { if (r == JFileChooser.APPROVE_OPTION){ pathtarget.setText(j.getSelectedFile().getAbsolutePath()); }else pathtarget.setText("the user cancelled the operation"); }else{ if (r == JFileChooser.APPROVE_OPTION){ pathsource.setText(j.getSelectedFile().getAbsolutePath()); }else pathsource.setText("the user cancelled the operation"); } } public static void main(String[] args) { SaveAndroidApp main=new SaveAndroidApp(); main.init(); } }
Penulis menggunakan Netbeans 8.2 dan JDK 8 sebagai compiler.
Komentar
Posting Komentar