如何通过Android Publisher Java API v2将obb文件上传到Google Play

时间:2016-02-25 14:13:03

标签: java upload google-play-developer-api

我以GitHub为例。 它运行正常,但我需要用我的apk上传obb文件。

我的部分代码:

        AndroidPublisher.Edits.Insert editRequest = edits.insert(bundleID, null);
        AppEdit edit = editRequest.execute();
        final String editId = edit.getId();
        final AbstractInputStreamContent apkFile = new FileContent("application/vnd.android.package-archive", new File(apkPath));

        AndroidPublisher.Edits.Apks.Upload uploadRequest = edits.apks().upload(bundleID, editId, apkFile);

        Apk apk = uploadRequest.execute();

        List<Integer> apkVersionCodes = new ArrayList<>();
        apkVersionCodes.add(apk.getVersionCode());
        AndroidPublisher.Edits.Tracks.Update updateTrackRequest = edits.tracks().update(bundleID, editId, TRACK_ALPHA, new Track().setVersionCodes(apkVersionCodes));
        updateTrackRequest.execute();
        // Commit changes for edit.
        AndroidPublisher.Edits.Commit commitRequest = edits.commit(bundleID, editId);
        commitRequest.execute();

我该怎么做?

2 个答案:

答案 0 :(得分:2)

上传新的扩展文件并将其与APK相关联:

String expansionFileType = "main"; // either "main" or "patch"
FileContent file = new FileContent("application/octet-stream", new File(path));
edits.expansionfiles().upload(appId, editId, versionCode, expansionFileType, file).execute();

如果要将现有扩展文件与APK关联:

String expansionFileType = "main";
int previousVersionCode = // find the version code here
ExpansionFile obbFile = new ExpansionFile().setReferencesVersion(previousVersionCode);
edits.expansionfiles().update(appId, editId, versionCode, expansionFileType, obbFile).execute();

答案 1 :(得分:0)

感谢Zynger和开发此https://github.com/googlesamples/android-play-publisher-api/tree/master/v2/java

的原始人
package com.google.play.developerapi.samples;

/**
 * Contains global application configuration, which is required by all samples.
 */
public final class ApplicationConfig {

    private ApplicationConfig() {
        // no instance
    }

    /**
     * Specify the name of your application. If the application name is
     * {@code null} or blank, the application will log a warning. Suggested
     * format is "MyCompany-Application/1.0".
     */
    static final String APPLICATION_NAME = "Test 1234";

    /**
     * Specify the package name of the app.
     */
    static final String PACKAGE_NAME = "my.app";

    /**
     * Authentication.
     * <p>
     * Installed application: Leave this string empty and copy or
     * edit resources/client_secrets.json.
     * </p>
     * <p>
     * Service accounts: Enter the service
     * account email and add your key.p12 file to the resources directory.
     * </p>
     */
    static final String SERVICE_ACCOUNT_EMAIL = "newaccount@YOUR_SERIVE_ACCOUNT.gserviceaccount.com";

    /**
     * Specify the apk file path of the apk to upload, i.e. /resources/your_apk.apk
     * <p>
     * This needs to be set for running {@link BasicUploadApk} and {@link UploadApkWithListing}
     * samples.
     * </p>
     */
    public static final String APK_FILE_PATH = "/resources/x7.apk";

    public static final String OBB_FILE_PATH = "/resources/x7.main.obb";
}






 public static void main(String[] args) {
        try {
            Preconditions.checkArgument(!Strings.isNullOrEmpty(ApplicationConfig.PACKAGE_NAME),
                    "ApplicationConfig.PACKAGE_NAME cannot be null or empty!");
        // Create the API service.
        AndroidPublisher service = AndroidPublisherHelper.init(ApplicationConfig.APPLICATION_NAME, ApplicationConfig.SERVICE_ACCOUNT_EMAIL);
        final Edits edits = service.edits();

        // Create a new edit to make changes to your listing.
        Insert editRequest = edits.insert(ApplicationConfig.PACKAGE_NAME, null /** no content */);
        AppEdit edit = editRequest.execute();
        final String editId = edit.getId();
        log.info(String.format("Created edit with id: %s", editId));


        File f = new File(ApplicationConfig.APK_FILE_PATH);
        if(f.exists() && !f.isDirectory()) { 
           System.out.println("Exist ");
        }


        // Upload new apk to developer console
        final String apkPath = BasicUploadApk.class.getResource(ApplicationConfig.APK_FILE_PATH).toURI().getPath();

        final String obbPath = BasicUploadApk.class.getResource(ApplicationConfig.OBB_FILE_PATH).toURI().getPath();

        final AbstractInputStreamContent apkFile = new FileContent(AndroidPublisherHelper.MIME_TYPE_APK, new File(apkPath));
        Upload uploadRequest = edits.apks().upload(ApplicationConfig.PACKAGE_NAME,editId,apkFile);

        final AbstractInputStreamContent obbFile = new FileContent(AndroidPublisherHelper.MIME_TYPE_OBB, new File(obbPath));

        Apk apk = uploadRequest.execute(); 
        log.info(String.format("Version code %d has been uploaded",apk.getVersionCode())); 
        List<Integer> apkVersionCodes = new ArrayList<>();
        apkVersionCodes.add(apk.getVersionCode()); 

        Update updateTrackRequest = edits.tracks().update(ApplicationConfig.PACKAGE_NAME,editId,TRACK_ALPHA, new Track().setVersionCodes(apkVersionCodes));


        String expansionFileType = "main"; // either "main" or "patch"
        FileContent file = new FileContent("application/octet-stream", new File(ApplicationConfig.OBB_FILE_PATH)); 
        edits.expansionfiles().upload(ApplicationConfig.PACKAGE_NAME, editId, apk.getVersionCode(), expansionFileType, obbFile).execute();



        Track updatedTrack = updateTrackRequest.execute();
        log.info(String.format("Track %s has been updated.", updatedTrack.getTrack()));

        // Commit changes for edit.
        Commit commitRequest = edits.commit(ApplicationConfig.PACKAGE_NAME, editId);
        AppEdit appEdit = commitRequest.execute();

        log.info(String.format("App edit with id %s has been comitted", appEdit.getId()));

    } catch (IOException | URISyntaxException | GeneralSecurityException ex) {
        log.error("Excpetion was thrown while uploading apk to rollout track", ex);
    }
}