Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
JENKINS-38731: Added upload of deobfuscation-files.
  • Loading branch information
christopherfrieler committed Sep 6, 2017
1 parent 7cdf0b2 commit 11b97cb
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
Expand Up @@ -59,6 +59,9 @@ public class ApkPublisher extends GooglePlayPublisher {
@DataBoundSetter
private String apkFilesPattern;

@DataBoundSetter
private String deobfuscationFilesPattern;

@DataBoundSetter
private String expansionFilesPattern;

Expand All @@ -85,6 +88,14 @@ private String getExpandedApkFilesPattern() throws IOException, InterruptedExcep
return expand(getApkFilesPattern());
}

public String getDeobfuscationFilesPattern() {
return fixEmptyAndTrim(deobfuscationFilesPattern);
}

private String getExpandedDeobfuscationFilesPattern() throws IOException, InterruptedException {
return expand(getDeobfuscationFilesPattern());
}

public String getExpansionFilesPattern() {
return fixEmptyAndTrim(expansionFilesPattern);
}
Expand Down Expand Up @@ -311,8 +322,9 @@ private boolean publishApk(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace,
try {
GoogleRobotCredentials credentials = getCredentialsHandler().getServiceAccountCredentials();
return workspace.act(new ApkUploadTask(listener, credentials, applicationId, workspace, apkFiles,
expansionFiles, usePreviousExpansionFilesIfMissing, fromConfigValue(getCanonicalTrackName()),
getRolloutPercentageValue(), getExpandedRecentChangesList()));
getExpandedDeobfuscationFilesPattern(), expansionFiles, usePreviousExpansionFilesIfMissing,
fromConfigValue(getCanonicalTrackName()), getRolloutPercentageValue(),
getExpandedRecentChangesList()));
} catch (UploadException e) {
logger.println(String.format("Upload failed: %s", getPublisherErrorMessage(e)));
logger.println("- No changes have been applied to the Google Play account");
Expand Down
Expand Up @@ -7,13 +7,10 @@
import com.google.api.services.androidpublisher.model.ApkListing;
import com.google.api.services.androidpublisher.model.ExpansionFile;
import com.google.api.services.androidpublisher.model.ExpansionFilesUploadResponse;
import com.google.common.base.Strings;
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotCredentials;
import hudson.FilePath;
import hudson.model.BuildListener;
import hudson.model.TaskListener;
import net.dongliu.apk.parser.bean.ApkMeta;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -25,9 +22,11 @@
import java.util.Locale;
import java.util.Map;
import java.util.TreeSet;

import net.dongliu.apk.parser.bean.ApkMeta;
import org.apache.commons.codec.digest.DigestUtils;
import static org.jenkinsci.plugins.googleplayandroidpublisher.ApkPublisher.ExpansionFileSet;
import static org.jenkinsci.plugins.googleplayandroidpublisher.ApkPublisher.RecentChanges;
import static org.jenkinsci.plugins.googleplayandroidpublisher.Constants.DEOBFUSCATION_FILE_TYPE_PROGUARD;
import static org.jenkinsci.plugins.googleplayandroidpublisher.Constants.OBB_FILE_TYPE_MAIN;
import static org.jenkinsci.plugins.googleplayandroidpublisher.Constants.OBB_FILE_TYPE_PATCH;
import static org.jenkinsci.plugins.googleplayandroidpublisher.Util.getApkMetadata;
Expand All @@ -36,6 +35,7 @@ class ApkUploadTask extends TrackPublisherTask<Boolean> {

private final FilePath workspace;
private final List<FilePath> apkFiles;
private final String deobfuscationFilesPattern;
private final Map<Integer, ExpansionFileSet> expansionFiles;
private final boolean usePreviousExpansionFilesIfMissing;
private final RecentChanges[] recentChangeList;
Expand All @@ -44,12 +44,13 @@ class ApkUploadTask extends TrackPublisherTask<Boolean> {
private int latestPatchExpansionFileVersionCode;

ApkUploadTask(TaskListener listener, GoogleRobotCredentials credentials, String applicationId,
FilePath workspace, List<FilePath> apkFiles, Map<Integer, ExpansionFileSet> expansionFiles,
FilePath workspace, List<FilePath> apkFiles, String deobfuscationFilesPattern, Map<Integer, ExpansionFileSet> expansionFiles,
boolean usePreviousExpansionFilesIfMissing, ReleaseTrack track, double rolloutPercentage,
ApkPublisher.RecentChanges[] recentChangeList) {
super(listener, credentials, applicationId, track, rolloutPercentage);
this.workspace = workspace;
this.apkFiles = apkFiles;
this.deobfuscationFilesPattern = deobfuscationFilesPattern;
this.expansionFiles = expansionFiles;
this.usePreviousExpansionFilesIfMissing = usePreviousExpansionFilesIfMissing;
this.recentChangeList = recentChangeList;
Expand Down Expand Up @@ -109,6 +110,9 @@ protected Boolean execute() throws IOException, InterruptedException, UploadExce
new FileContent("application/vnd.android.package-archive", new File(apkFile.getRemote()));
Apk uploadedApk = editService.apks().upload(applicationId, editId, apk).execute();
uploadedVersionCodes.add(uploadedApk.getVersionCode());
if (!Strings.isNullOrEmpty(deobfuscationFilesPattern)) {
uploadDeobfuscationFileForApk(apkFile, uploadedApk.getVersionCode());
}
}

// Upload the expansion files, or associate the previous ones, if configured
Expand Down Expand Up @@ -162,6 +166,37 @@ protected Boolean execute() throws IOException, InterruptedException, UploadExce
return true;
}

private void uploadDeobfuscationFileForApk(FilePath apkFile, Integer versionCode)
throws IOException, InterruptedException {
logger.println("Searching for deobfuscation-file...");

FilePath basePath = apkFile.getParent();
String deobfuscationFilesPatternWithoutGoingUp = this.deobfuscationFilesPattern;
while (deobfuscationFilesPatternWithoutGoingUp.startsWith("../")) {
if (workspace.equals(basePath)) {
logger.println("Stopped search, because it leads out of the workspace.");
return;
}
basePath = basePath.getParent();
deobfuscationFilesPatternWithoutGoingUp = deobfuscationFilesPatternWithoutGoingUp.substring("../".length());
}

List<String> deobfuscationFiles = basePath.act(new FindFilesTask(deobfuscationFilesPatternWithoutGoingUp));
if (!deobfuscationFiles.isEmpty()) {
String deobfuscationFilePath = basePath.child(deobfuscationFiles.get(0)).getRemote();
logger.println(String.format("Uploading deobfuscation-file: %s", deobfuscationFilePath));
FileContent deobfuscationFileContent
= new FileContent("application/octet-stream", new File(deobfuscationFilePath));
editService.deobfuscationfiles().upload(applicationId, editId, versionCode,
DEOBFUSCATION_FILE_TYPE_PROGUARD, deobfuscationFileContent).execute();
if (deobfuscationFiles.size() > 1) {
logger.println("Ignoring further deobfuscation-files.");
}
} else {
logger.println("No deobfuscation-file found.");
}
}

/** Applies an expansion file to an APK, whether from a given file, or by using previously-uploaded file. */
private void applyExpansionFile(int versionCode, String type, FilePath filePath, boolean usePreviousIfMissing)
throws IOException {
Expand Down
Expand Up @@ -5,6 +5,9 @@

public class Constants {

/** Deobfuscation file type: proguard */
public static final String DEOBFUSCATION_FILE_TYPE_PROGUARD = "proguard";

/** File name pattern which expansion files must match. */
public static final Pattern OBB_FILE_REGEX =
Pattern.compile("^(main|patch)\\.([0-9]+)\\.([._a-z0-9]+)\\.obb$", Pattern.CASE_INSENSITIVE);
Expand Down
Expand Up @@ -9,6 +9,10 @@
<f:textbox />
</f:entry>

<f:entry title="${%Deobfuscation files}" field="deobfuscationFilesPattern" description="${%Filepath or -pattern relative to each apk}">
<f:textbox />
</f:entry>

<f:entry title="${%Expansion files}" field="expansionFilesPattern">
<f:textbox />
<f:checkbox title="${%Re-use expansion files from existing APKs where necessary}" field="usePreviousExpansionFilesIfMissing" />
Expand Down
@@ -0,0 +1,16 @@
<div>
Specifies a filepath or -pattern matching one deobfuscation-file per APK that gets uploaded to Google Play,
e.g. "<tt>../mapping/*/mapping.txt</tt>".
<p/>
The path to the deobfuscation-file is resolved relative to the directory of each APK.<br/>
Missing deobfuscation-files or multiple matches are ignored.
<p/>
At the beginning of the path you can use "<tt>../</tt>" (also more than once) to search relative to an
ancestor-directory. However, you cannot leave the workspace.<br/>
You can also use wildcards like "<tt>**/mapping/*/*-mapping.txt</tt>". See
<a href='https://ant.apache.org/manual/Types/fileset.html'>the 'includes' attribute of Ant's FileSet</a>
for the exact format.
<hr/>
This field supports substituting environment variables in the form
<tt>${SOME_VARIABLE}</tt> or <tt>$SOME_VARIABLE</tt> at build time.
</div>

0 comments on commit 11b97cb

Please sign in to comment.