uploadToPlayStore method
Uploads the built app (either appbundle or apk) to the playstore using Play Store Developer API
The user should create the correct credentials using GCP, downloads the JSON api key and place it in the path
Implementation
Future<void> uploadToPlayStore({
required BPConfig config,
}) async {
// Reading the user credentials from their JSON file
late google_auth.ServiceAccountCredentials credentials;
try {
credentials = google_auth.ServiceAccountCredentials.fromJson(
File(getCredentialPath()).readAsStringSync(),
);
} catch (e) {
Console.logError("Error while using the Play Store API json credentials");
Console.logError(e.toString());
return;
}
// Getting the client for the service account, using the credentials
// created via JSON file, with the publishing scope
//
// The client will have to be closed at the end of the function
google_auth.AutoRefreshingAuthClient client = await google_auth.clientViaServiceAccount(
credentials,
[play.AndroidPublisherApi.androidpublisherScope],
);
// Creating the api object to call the Google play API
play.AndroidPublisherApi api = play.AndroidPublisherApi(client);
// Calling the APIs
//
// 1. Create edit object for the app
Console.logInfo("Creating a '$releaseTrack' release for Android...");
late play.AppEdit edit;
try {
edit = await api.edits.insert(play.AppEdit(), bundleID);
if (edit.id == null) {
throw Exception("The created edit is invalid");
}
} catch (e) {
Console.logError("There was an error while creating an edit to the Play Store API");
Console.logError(e.toString());
client.close();
return;
}
// 2. Upload the bundle or apk file
Console.logInfo("Uploading the build file for Android...");
try {
await api.edits.bundles.upload(
bundleID,
edit.id!,
uploadMedia: play.Media(File(outputFilePath).openRead(), File(outputFilePath).lengthSync()),
);
} catch (e) {
Console.logError("There was an error while uploading the build file to the Play Store API");
Console.logError(e.toString());
client.close();
return;
}
// 3. Assign a track to the edit
try {
await api.edits.tracks.update(
play.Track(
track: releaseTrack,
releases: [
play.TrackRelease(
status: 'completed',
versionCodes: [config.buildVersion],
// releaseNotes: [
// play.LocalizedText(language: 'en-US', text: 'Automated release via flutter_build_pipe'),
// ],
),
],
),
bundleID,
edit.id!,
releaseTrack,
);
} catch (e) {
Console.logError("There was an error while creating the $releaseTrack track in Play Store API");
Console.logError(e.toString());
client.close();
return;
}
// 4. Commit the edit
try {
await api.edits.commit(bundleID, edit.id!);
Console.logSuccess('√ Android app is successfully published!');
} catch (e) {
Console.logError("There was an error while commiting the changes to the Play Store API");
Console.logError(e.toString());
}
// Closing the client
client.close();
}