Skip to content

Commit

Permalink
Merge pull request #21
Browse files Browse the repository at this point in the history
Allow applying expansion files from the current upload to multiple APKs
  • Loading branch information
orrc authored Dec 2, 2019
2 parents b9a0e1e + 130dcc7 commit 02b6960
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,17 @@ protected Boolean execute() throws IOException, InterruptedException {

/** Applies the appropriate expansion file to each given APK version. */
private void handleExpansionFiles(Collection<Long> uploadedVersionCodes) throws IOException {
for (long versionCode : uploadedVersionCodes) {
// Ensure that the version codes are sorted in ascending order, as this allows us to
// upload an expansion file with the lowest version, and re-use it for subsequent APKs
SortedSet<Long> sortedVersionCodes = new TreeSet<>(uploadedVersionCodes);

// If we want to re-use existing expansion files, figure out what the latest values are
if (usePreviousExpansionFilesIfMissing) {
fetchLatestExpansionFileVersionCodes();
}

// Upload or apply the expansion files for each APK we've uploaded
for (long versionCode : sortedVersionCodes) {
ExpansionFileSet fileSet = expansionFiles.get(versionCode);
FilePath mainFile = fileSet == null ? null : fileSet.getMainFile();
FilePath patchFile = fileSet == null ? null : fileSet.getPatchFile();
Expand All @@ -200,9 +210,6 @@ private void applyExpansionFile(long versionCode, String type, FilePath filePath

// Otherwise, check whether we should reuse an existing expansion file
if (usePreviousIfMissing) {
// Ensure we know what the latest expansion files versions are
fetchLatestExpansionFileVersionCodes();

// If there is no previous APK with this type of expansion file, there's nothing we can do
final long latestVersionCodeWithExpansion = type.equals(OBB_FILE_TYPE_MAIN) ?
latestMainExpansionFileVersionCode : latestPatchExpansionFileVersionCode;
Expand All @@ -226,19 +233,14 @@ private void applyExpansionFile(long versionCode, String type, FilePath filePath

/** Determines whether there are already-existing APKs for this app which have expansion files associated. */
private void fetchLatestExpansionFileVersionCodes() throws IOException {
// Don't do this again if we've already attempted to find the expansion files
if (latestMainExpansionFileVersionCode != 0 && latestPatchExpansionFileVersionCode != 0) {
return;
}

// Find the latest APK with a main expansion file, and the latest with a patch expansion file
latestMainExpansionFileVersionCode = fetchLatestExpansionFileVersionCode(OBB_FILE_TYPE_MAIN);
latestPatchExpansionFileVersionCode = fetchLatestExpansionFileVersionCode(OBB_FILE_TYPE_PATCH);
}

/** @return The version code of the newest APK which has an expansion file of this type, else {@code -1}. */
private long fetchLatestExpansionFileVersionCode(String type) throws IOException {
// Find the latest APK with a patch expansion file, i.e. sort version codes in descending order
// Find the latest APK with an expansion file, i.e. sort version codes in descending order
SortedSet<Long> newestVersionCodes = new TreeSet<>((a, b) -> ((int) (b - a)));
newestVersionCodes.addAll(existingVersionCodes);
for (long versionCode : newestVersionCodes) {
Expand Down Expand Up @@ -278,8 +280,20 @@ private ExpansionFile getExpansionFile(long versionCode, String type) throws IOE
*/
private ExpansionFilesUploadResponse uploadExpansionFile(long versionCode, String type, FilePath filePath)
throws IOException {
// Upload the file
FileContent file = new FileContent("application/octet-stream", new File(filePath.getRemote()));
return editService.expansionfiles().upload(applicationId, editId, Math.toIntExact(versionCode), type, file).execute();
ExpansionFilesUploadResponse response = editService.expansionfiles()
.upload(applicationId, editId, Math.toIntExact(versionCode), type, file).execute();

// Keep track of the now-latest APK with an expansion file, so we can associate the
// same expansion file with subsequent APKs that were uploaded in this session
if (type.equals(OBB_FILE_TYPE_MAIN)) {
latestMainExpansionFileVersionCode = versionCode;
} else {
latestPatchExpansionFileVersionCode = versionCode;
}

return response;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<h2>Naming</h2>
Files <b>must</b> be named in the format
<tt>[main|patch].&lt;expansion-version&gt;.&lt;application-id&gt.obb</tt>
<tt>[main|patch].&lt;apk-version&gt;.&lt;application-id&gt.obb</tt>
<p/>
For example:
<ul>
Expand All @@ -35,8 +35,9 @@ <h2>Re-using existing expansion files</h2>
<p/>
With this option enabled, if not enough expansion files are provided for all
of the APK(s) being uploaded, this plugin will search for the <i>newest</i>,
previously-uploaded APKs on Google Play with main or patch expansion files,
and will associate those files with the new APK(s) being uploaded here.
APKs on Google Play with main or patch expansion files — whether previously
uploaded, or uploaded via the current build — and will associate those files
with the new APK(s) being uploaded here.
<p/>
For example: If you want to upload a new APK, but the expansion files have not
changed at all, you should leave the "Expansion files" field blank, but enable
Expand All @@ -48,7 +49,12 @@ <h2>Re-using existing expansion files</h2>
then just provide the patch expansion file and make sure this option is
checked. The uploaded APK will have the existing main expansion file
associated with it, along with the newly-uploaded patch file.

<p/>
Or, if you have a new main or patch expansion file, and want to apply that
same file to multiple APKs being uploaded, name the expansion file according
to the <i>lowest</i> versionCode that you're uploading. That expansion file
will then be uploaded, and applied to the APKs with higher versionCodes that
were uploaded in the same build.
<hr/>
This field supports substituting environment variables in the form
<tt>${SOME_VARIABLE}</tt> or <tt>$SOME_VARIABLE</tt> at build time.
Expand Down

0 comments on commit 02b6960

Please sign in to comment.