Skip to content

Commit

Permalink
ARQ-1605 APK export/import does not work correctly on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Miklosovic committed Jan 2, 2014
1 parent 8913b99 commit f607b34
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@ public void onAndroidDeployArchive(@Observes AndroidDeploy event, DeploymentDesc

Archive<?> archive = event.getArchive();

File deployApk = DroidiumFileUtils.export(archive,
new File(DroidiumFileUtils.getTmpDir(), DroidiumFileUtils.getRandomAPKFileName()));

File resignedApk = signer.get().resign(deployApk,
new File(DroidiumFileUtils.getTmpDir(), DroidiumFileUtils.getRandomAPKFileName()));
File deployApk = new File(DroidiumFileUtils.getTmpDir(), DroidiumFileUtils.getRandomAPKFileName());
DroidiumFileUtils.export(archive, deployApk);
File resignedApk = signer.get().resign(deployApk);

AndroidDeployment deployment = new AndroidDeployment();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ public File sign(File toSign, File signed) {
/**
*
* @param toResign file to resign
* @param resigned
* @return {@code resigned} file
* @return resigned file
*/
public File resign(File toResign, File resigned) {
public File resign(File toResign) {
Validate.notNull(toResign, "File to resign can not be a null object!");
Archive<?> apk = ShrinkWrap.createFromZipFile(JavaArchive.class, toResign);
apk.delete("META-INF");
DroidiumFileUtils.export(apk, toResign);
return sign(toResign, resigned);
File toSign = new File(DroidiumFileUtils.getTmpDir(), DroidiumFileUtils.getRandomAPKFileName());
DroidiumFileUtils.export(apk, toSign);
return sign(toSign, new File(DroidiumFileUtils.getTmpDir(), DroidiumFileUtils.getRandomAPKFileName()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@
*/
package org.arquillian.droidium.container.utils;

import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -136,59 +131,41 @@ public static String getRandomAPKFileName() {
}

/**
* Exports archive to file
* Exports archive to file. If {@code toFile} exists, it will be deleted. If deletion fails, we try to overwrite it during
* exporting on behalf of ShrinkWrap.
*
* @param fromArchive archive to export its content from
* @param toFile file to export the content from archive to
* @return {@code toFile} or null if exporting fails
* @return {@code toFile}
*/
public static File export(Archive<?> fromArchive, File toFile) {
Validate.notNull(fromArchive, "Archive to export from can not be a null object!");
Validate.notNull(toFile, "File to export archive to can not be a null object!");
if (toFile.exists() && toFile.isFile()) {
if (!toFile.delete()) {
logger.fine("File to export the archive to exists and it can not be removed.");
}
}

final OutputStream out;
final InputStream in;

try {
out = new FileOutputStream(toFile);
in = fromArchive.as(ZipExporter.class).exportAsInputStream();
write(in, out);
closeStream(in);
closeStream(out);
return toFile;
} catch (final FileNotFoundException ex) {
} catch (final IOException ex) {
}
return null;
fromArchive.as(ZipExporter.class).exportTo(toFile, true);
return toFile;
}

private static void write(InputStream input, OutputStream output) throws IOException {
Validate.notNull(input, "InputStream to read from can not be null object!");
Validate.notNull(output, "OutputStream to write to can not be null object!");

int read = 0;
byte[] bytes = new byte[1024];

while ((read = input.read(bytes)) != -1) {
output.write(bytes, 0, read);
/**
* Exports archive to file. Archive will be exported to file placed in temporary directory. This method has
* to be called after {@link #createTmpDir(File)}.
*
* @param fromArchive archive to export
* @return exported archive as file
* @throws IllegalStateException if temporary directory to export archive by default is null (not set)
*/
public static File export(Archive<?> fromArchive) {
Validate.notNull(fromArchive, "Archive to export from can not be a null object!");
if (DroidiumFileUtils.getTmpDir() == null) {
throw new IllegalStateException("Please call DroidiumFileUtils.createTmpDir(File) before calling this method.");
}

output.flush();
File toFile = new File(DroidiumFileUtils.getTmpDir(), DroidiumFileUtils.getRandomAPKFileName());
return export(fromArchive, toFile);
}

private static void closeStream(Closeable stream) {
if (stream == null) {
return;
}
try {
stream.close();
} catch (final IOException ignore) {
// ignore
} finally {
stream = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ public void performInstrumentation(@Observes PerformInstrumentation event) {
}

private File getSelendroidResigned(File selendroidRebuilt) {
return signer.get().resign(selendroidRebuilt,
new File(DroidiumFileUtils.getTmpDir(), DroidiumFileUtils.getRandomAPKFileName()));
return signer.get().resign(selendroidRebuilt);
}

private File getSelendroidWorkingCopy() {
Expand Down

0 comments on commit f607b34

Please sign in to comment.