如何用Google Play Publisher API上传App Bundle(.aab)?现有APK代码失效
How to Modify Your Code to Upload an App Bundle (.aab) via Google Play Publisher API
Got it! The core issue here is that uploading an App Bundle uses a different endpoint in the Google Play Publisher API compared to APKs. Let's adjust your code step by step to support .aab files:
Full Modified Code
"""Uploads an app bundle to the alpha track.""" import argparse import sys from apiclient import sample_tools from oauth2client import client TRACK = 'alpha' # Can be 'alpha', 'beta', 'production' or 'rollout' # Declare command-line flags. argparser = argparse.ArgumentParser(add_help=False) argparser.add_argument('package_name', help='The package name. Example: com.android.sample') argparser.add_argument('bundle_file', nargs='?', default='test.aab', help='The path to the App Bundle file to upload.') def main(argv): # Authenticate and construct service. service, flags = sample_tools.init( argv, 'androidpublisher', 'v3', __doc__, __file__, parents=[argparser], scope='https://www.googleapis.com/auth/androidpublisher') # Process flags and read their values. package_name = flags.package_name bundle_file = flags.bundle_file try: edit_request = service.edits().insert(body={}, packageName=package_name) result = edit_request.execute() edit_id = result['id'] # Upload the App Bundle instead of APK bundle_response = service.edits().bundles().upload( editId=edit_id, packageName=package_name, media_body=bundle_file).execute() print 'Version code %d has been uploaded' % bundle_response['versionCode'] track_response = service.edits().tracks().update( editId=edit_id, track=TRACK, packageName=package_name, body={u'releases': [{ u'name': u'My first App Bundle API release', u'versionCodes': [str(bundle_response['versionCode'])], u'status': u'completed', }]}).execute() print 'Track %s is set with releases: %s' % ( track_response['track'], str(track_response['releases'])) commit_request = service.edits().commit( editId=edit_id, packageName=package_name).execute() print 'Edit "%s" has been committed' % (commit_request['id']) except client.AccessTokenRefreshError: print ('The credentials have been revoked or expired, please re-run the ' 'application to re-authorize') if __name__ == '__main__': main(sys.argv)
Key Changes Explained
- API Endpoint Swap: Replaced
service.edits().apks().upload()withservice.edits().bundles().upload()— this is the critical change, as the API uses separate endpoints for APKs and App Bundles. - Parameter & Variable Updates: Renamed
apk_filetobundle_filein both the command-line arguments and internal variables to keep the code semantically clear, and updated the default value totest.aab. - Text Adjustments: Updated print messages and docstrings to reference App Bundles instead of APKs for clarity.
- Release Configuration: No changes needed here! The track update logic works the same way for both APKs and App Bundles, since we still reference the app's
versionCodein the release body.
Just run the modified script with your package name and .aab file path, and it should upload your App Bundle to the specified track successfully.
内容的提问来源于stack exchange,提问作者hard coder
相关产品推荐
相关产品推荐

