Skip to content
This repository has been archived by the owner on Jul 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #11 from qiwi/develop
Browse files Browse the repository at this point in the history
Add last updates
  • Loading branch information
artemnikitin committed Apr 5, 2015
2 parents 94603a6 + 7e3475d commit 4c097c0
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 294 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Add plugin as .jar

- Build project via **gradlew clean fatJar**
- Build project via **./gradlew clean fatJar**
- Get .jar from **/build/libs/**
- Put .jar in your project
- Create file **client_secrets.json** with following format
Expand Down Expand Up @@ -39,7 +39,25 @@ publishToPlay {
keyPath = '/path/to/key.p12'
settingsPath = '/path/to/client_secrets.json'
track = 'alpha'
updateMessage = 'path/to/message.json' // optional parameter
}
```
If you want to add update message, then put it into JSON file and add `updateMessage` optional parameter to `publishToPlay` section. JSON must have format like this:
```
{
"messages": [
{
"locale": "en-US",
"text": "This is a message"
},
{
"locale": "ru-RU",
"text": "Текст"
}
]
}
```
Locale must be specified according to IETF BCP 47.

To publish your apk to Google Play use command ```./gradlew publishToPlay ```

3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dependencies {
repositories {
mavenCentral()
}

compile 'com.google.code.gson:gson:2.3'
compile 'commons-logging:commons-logging-api:1.1'
compile 'javax.jdo:jdo2-api:2.3-eb'
Expand Down Expand Up @@ -73,4 +72,4 @@ fatJar {
}

group = 'com.qiwi'
version = '0.0.1'
version = '0.2.0'
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.10-all.zip
distributionUrl=http\://services.gradle.org/distributions/gradle-1.12-all.zip
13 changes: 3 additions & 10 deletions src/main/groovy/com/qiwi/mobile/AndroidPublishPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@ class AndroidPublishPlugin implements Plugin<Project> {
@Override void apply(Project project) {
def extensions = project.extensions.create("publishToPlay", AndroidPublishPluginExtension)
project.task('publishToPlay') << {
Config.configApkPath = extensions.configApkPath
Config.configAppName = extensions.configAppName
Config.configEmail = extensions.configEmail
Config.configPackageName = extensions.configPackageName
Config.keyPath = extensions.keyPath
Config.settingsPath = extensions.settingsPath
Config.track = extensions.track
println("Start publish to Google Play ...")
def result = Worker.upload()
if(result) println("App was successfully published to Google Play!")
else throw new GradleException()
boolean result = Publisher.upload(extensions)
if(result) println("App was successfully published on Google Play!")
else throw new GradleException("Upload on Google Play failed!")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ class AndroidPublishPluginExtension {
String keyPath
String settingsPath
String track
String updateMessage
}
52 changes: 52 additions & 0 deletions src/main/groovy/com/qiwi/mobile/Publisher.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.qiwi.mobile
import com.google.api.services.androidpublisher.AndroidPublisher
import com.google.api.services.androidpublisher.model.Apk
import com.google.api.services.androidpublisher.model.ApkListing
import com.google.api.services.androidpublisher.model.AppEdit
import com.google.api.services.androidpublisher.model.Track

class Publisher {

static boolean upload(AndroidPublishPluginExtension params) {
try {
AndroidPublisher service = PublisherHelper.init(params)
final AndroidPublisher.Edits edits = service.edits()

AndroidPublisher.Edits.Insert editRequest = edits
.insert(params.configPackageName, null)
AppEdit edit = editRequest.execute()
final String editId = edit.getId()

AndroidPublisher.Edits.Apks.Upload uploadRequest = edits
.apks()
.upload(params.configPackageName, editId, PublisherHelper.getApk(params.configApkPath))
Apk apk = uploadRequest.execute()

List<Integer> apkVersionCodes = new ArrayList<>()
apkVersionCodes.add(apk.getVersionCode())
AndroidPublisher.Edits.Tracks.Update updateTrackRequest = edits
.tracks()
.update(params.configPackageName, editId, params.track, new Track().setVersionCodes(apkVersionCodes))
updateTrackRequest.execute()

if (params.updateMessage != "" && params.updateMessage != null) {
def json = PublisherHelper.parseJson(params.updateMessage)
json.messages.each { entry ->
AndroidPublisher.Edits.Apklistings.Update updateRecentChangesRequest = edits
.apklistings()
.update(params.configPackageName, editId, apk.getVersionCode(),
entry.locale, new ApkListing().setRecentChanges(entry.text))
updateRecentChangesRequest.execute()
}
}

AndroidPublisher.Edits.Commit commitRequest = edits.commit(params.configPackageName, editId)
commitRequest.execute()
true
} catch (Exception e) {
println("Exception was thrown while uploading apk: " + e)
false
}
}

}
80 changes: 80 additions & 0 deletions src/main/groovy/com/qiwi/mobile/PublisherHelper.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.qiwi.mobile

import com.google.api.client.auth.oauth2.Credential
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
import com.google.api.client.http.AbstractInputStreamContent
import com.google.api.client.http.FileContent
import com.google.api.client.http.HttpTransport
import com.google.api.client.json.JsonFactory
import com.google.api.client.json.jackson2.JacksonFactory
import com.google.api.client.util.store.FileDataStoreFactory
import com.google.api.services.androidpublisher.AndroidPublisher
import com.google.api.services.androidpublisher.AndroidPublisherScopes
import groovy.json.JsonSlurper

class PublisherHelper {

private static final String MIME_TYPE_APK = "application/vnd.android.package-archive"
private static final String DATA_STORE_SYSTEM_PROPERTY = "user.home"
private static final String DATA_STORE_FILE = ".store/android_publisher_api"
private static final File DATA_STORE_DIR =
new File(System.getProperty(DATA_STORE_SYSTEM_PROPERTY), DATA_STORE_FILE)
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance()
private static HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport()
private static final String INST_APP_USER_ID = "user"
private static FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR)

static AndroidPublisher init(AndroidPublishPluginExtension params) {
Credential credential
if (params.configEmail == null || params.configEmail.isEmpty()) {
credential = authorizeWithInstalledApplication(params)
} else {
credential = authorizeWithServiceAccount(params)
}

new AndroidPublisher.Builder(
HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(params.configAppName)
.build()
}

static Credential authorizeWithServiceAccount(AndroidPublishPluginExtension params) {
new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(params.configEmail)
.setServiceAccountScopes(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER))
.setServiceAccountPrivateKeyFromP12File(new File(params.keyPath))
.build()
}

static Credential authorizeWithInstalledApplication(AndroidPublishPluginExtension params) {
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
JSON_FACTORY, new InputStreamReader(PublisherHelper.class.getResourceAsStream(params.settingsPath)))

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets,
Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER))
.setDataStoreFactory(dataStoreFactory)
.build()

new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
.authorize(INST_APP_USER_ID)
}

static AbstractInputStreamContent getApk(String path) {
new FileContent(MIME_TYPE_APK, new File(path))
}

static Object parseJson(String path) {
File file = new File(path)
JsonSlurper parser = new JsonSlurper()
parser.parse(new FileReader(file))
}

}
Loading

0 comments on commit 4c097c0

Please sign in to comment.