Skip to content

Instantly share code, notes, and snippets.

@espresso3389
Created June 24, 2017 19:43
Show Gist options
  • Save espresso3389/8dab405b8478d9ed78853b525bf49dd9 to your computer and use it in GitHub Desktop.
Save espresso3389/8dab405b8478d9ed78853b525bf49dd9 to your computer and use it in GitHub Desktop.
Automatic APK Uploader
// This code is based on the following article but updated for the latest system (June 2017):
// Automatic apk upload to Google Play (part 1)
// http://petr.logdown.com/posts/255755-automatic-apk-upload-1
using System;
using System.Collections.Generic;
using System.IO;
// Google.Apis.AndroidPublisher.v2
using Google.Apis.AndroidPublisher.v2;
using Google.Apis.AndroidPublisher.v2.Data;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
namespace ApkUploader
{
class ApkUploader
{
static void Main(string[] args)
{
var packageName = "jp.hogehoge.my-test-app"; // アプリのパッケージ名
var apkPath = @"c:\dist\apk\jp.hogehoge.my-test-app-Signed.apk"; // サイン済みのAPKのパス
var recentChanges = "This is the first automated upload!"; // アップロード時の更新情報
var recentChangesLang = "en-US"; // 更新情報の言語(ここでは英語に設定している)
// ダウンロードしたサービスアカウントの秘密鍵(JSON)を読み込む
var cred = GoogleCredential.FromJson(File.ReadAllText("Google Play Android Developer-XXXXXXX.json"));
cred = cred.CreateScoped(new[] { AndroidPublisherService.Scope.Androidpublisher });
// Create the AndroidPublisherService.
var androidPublisherService = new AndroidPublisherService(new BaseClientService.Initializer { HttpClientInitializer = cred });
// Create a new edit to make changes to your listing.
var edit = androidPublisherService.Edits
.Insert(null /** no content */, packageName)
.Execute();
Console.WriteLine("Created edit with id: " +
edit.Id +
" (valid for " + edit.ExpiryTimeSeconds + " seconds)");
// Upload new apk to developer console
Console.WriteLine("Upload started for apk: " + Path.GetFileName(apkPath));
var upload = androidPublisherService.Edits.Apks.Upload(
packageName,
edit.Id,
new FileStream(
apkPath,
FileMode.Open),
"application/vnd.android.package-archive"
);
upload.ResponseReceived += (apk) =>
{
if (apk == null)
return;
// Assign apk to beta track.
var apkVersionCodes = new List<int?> { apk.VersionCode };
var updatedTrack =
androidPublisherService.Edits.Tracks.Update(
new Track() { VersionCodes = apkVersionCodes },
packageName,
edit.Id,
EditsResource.TracksResource.UpdateRequest.TrackEnum.Beta)
.Execute();
Console.WriteLine("Track " + updatedTrack.TrackValue + " has been updated.");
// Update recent changes field in apk listing.
var newApkListing = new ApkListing { RecentChanges = recentChanges };
androidPublisherService.Edits.Apklistings
.Update(
newApkListing,
packageName,
edit.Id,
apk.VersionCode.GetValueOrDefault(),
recentChangesLang)
.Execute();
Console.WriteLine("Recent changes has been updated.");
// Commit changes for edit.
var commitRequest = androidPublisherService.Edits.Commit(packageName, edit.Id);
var appEdit = commitRequest.Execute();
Console.WriteLine("App edit with id " + appEdit.Id + " has been comitted");
};
var result = upload.Upload();
if (result.Exception != null)
{
Console.WriteLine("Error: " + result.Exception.Message);
return;
}
if (result.Status != UploadStatus.Completed)
{
Console.WriteLine("File upload failed.");
return;
}
Console.WriteLine("File uploaded, bytes sent: " + result.BytesSent);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment