Exports an unsigned version of the application created by the given project.
Shell shell = Display.getCurrent().getActiveShell();
// get the java project to get the output directory
IFolder outputFolder = BaseProjectHelper.getOutputFolder(project);
if (outputFolder != null) {
IPath binLocation = outputFolder.getLocation();
// make the full path to the package
String fileName = project.getName() + AndroidConstants.DOT_ANDROID_PACKAGE;
File file = new File(binLocation.toOSString() + File.separator + fileName);
if (file.exists() == false || file.isFile() == false) {
MessageDialog.openInformation(Display.getCurrent().getActiveShell(),
"Android IDE Plug-in",
String.format("Failed to export %1$s: %2$s doesn't exist!",
project.getName(), file.getPath()));
return;
}
// ok now pop up the file save window
FileDialog fileDialog = new FileDialog(shell, SWT.SAVE);
fileDialog.setText("Export Project");
fileDialog.setFileName(fileName);
String saveLocation = fileDialog.open();
if (saveLocation != null) {
// get the stream from the original file
ZipInputStream zis = null;
ZipOutputStream zos = null;
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream(file);
zis = new ZipInputStream(input);
// get an output stream into the new file
File saveFile = new File(saveLocation);
output = new FileOutputStream(saveFile);
zos = new ZipOutputStream(output);
} catch (FileNotFoundException e) {
// only the input/output stream are throwing this exception.
// so we only have to close zis if output is the one that threw.
if (zis != null) {
try {
zis.close();
} catch (IOException e1) {
// pass
}
}
MessageDialog.openInformation(shell, "Android IDE Plug-in",
String.format("Failed to export %1$s: %2$s doesn't exist!",
project.getName(), file.getPath()));
return;
}
try {
ZipEntry entry;
byte[] buffer = new byte[4096];
while ((entry = zis.getNextEntry()) != null) {
String name = entry.getName();
// do not take directories or anything inside the META-INF folder since
// we want to strip the signature.
if (entry.isDirectory() || name.startsWith("META-INF/")) { //$NON-NL1$
continue;
}
ZipEntry newEntry;
// Preserve the STORED method of the input entry.
if (entry.getMethod() == JarEntry.STORED) {
newEntry = new JarEntry(entry);
} else {
// Create a new entry so that the compressed len is recomputed.
newEntry = new JarEntry(name);
}
// add the entry to the jar archive
zos.putNextEntry(newEntry);
// read the content of the entry from the input stream, and write it into the archive.
int count;
while ((count = zis.read(buffer)) != -1) {
zos.write(buffer, 0, count);
}
// close the entry for this file
zos.closeEntry();
zis.closeEntry();
}
} catch (IOException e) {
MessageDialog.openInformation(shell, "Android IDE Plug-in",
String.format("Failed to export %1$s: %2$s",
project.getName(), e.getMessage()));
} finally {
try {
zos.close();
} catch (IOException e) {
// pass
}
try {
zis.close();
} catch (IOException e) {
// pass
}
}
}
} else {
MessageDialog.openInformation(shell, "Android IDE Plug-in",
String.format("Failed to export %1$s: Could not get project output location",
project.getName()));
}