Skip to content

Commit

Permalink
Add support for Google Services gradle plugin
Browse files Browse the repository at this point in the history
The Google Services gradle plugin[1] helps enable use of Google services
in Android apps. Since this requires a `google-services.json` file to be
copied into the build directory, it needs to be a separate option in
addition to enabling the plugin in the `build.gradle` file.

1. https://developers.google.com/android/guides/google-services-plugin
  • Loading branch information
dbnicholson committed Oct 21, 2022
1 parent 6cd170a commit b89734a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
9 changes: 9 additions & 0 deletions doc/source/buildoptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ options (this list may not be exhaustive):
- ``--add-source``: Add a source directory to the app's Java code.
- ``--no-compile-pyo``: Do not optimise .py files to .pyo.
- ``--enable-androidx``: Enable AndroidX support library.
- ``--enable-google-services``: Enable the Google Services Gradle plugin.
This option requires a ``google-services.json`` file in root of the
project directory.


webview
Expand Down Expand Up @@ -152,6 +155,9 @@ ready.
- ``add-source``: Add a source directory to the app's Java code.
- ``--port``: The port on localhost that the WebView will
access. Defaults to 5000.
- ``--enable-google-services``: Enable the Google Services Gradle plugin.
This option requires a ``google-services.json`` file in root of the
project directory.


service_library
Expand All @@ -177,6 +183,9 @@ systems and frameworks.
- ``--add-jar``: The path to a .jar file to include in the APK. To
include multiple jar files, pass this argument multiple times.
- ``add-source``: Add a source directory to the app's Java code.
- ``--enable-google-services``: Enable the Google Services Gradle plugin.
This option requires a ``google-services.json`` file in root of the
project directory.


Requirements blacklist (APK size optimization)
Expand Down
18 changes: 18 additions & 0 deletions pythonforandroid/bootstraps/common/build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,9 @@ def make_package(args):
if args.fileprovider_paths:
shutil.copy(args.fileprovider_paths, join(res_dir, "xml/file_paths.xml"))

if args.enable_google_services:
shutil.copy(args.google_services_json, 'src/google-services.json')

# gradle build templates
render(
'build.tmpl.gradle',
Expand Down Expand Up @@ -864,6 +867,15 @@ def parse_args_and_make_package(args=None):
ap.add_argument('--activity-class-name', dest='activity_class_name', default=DEFAULT_PYTHON_ACTIVITY_JAVA_CLASS,
help='The full java class name of the main activity')

ap.add_argument('--enable-google-services', dest='enable_google_services',
action='store_true',
help=('Enable the Google Services Gradle plugin. '
'This requires a google-services.json in the root '
'of the project.'))
ap.add_argument('--google-services-json', dest='google_services_json',
default='google-services.json',
help='Path to google-services.json file')

# Put together arguments, and add those from .p4a config file:
if args is None:
args = sys.argv[1:]
Expand Down Expand Up @@ -949,6 +961,12 @@ def _read_configuration():
'--launcher (SDL2 bootstrap only)' +
'to have something to launch inside the .apk!')
sys.exit(1)

if args.enable_google_services and not exists(args.google_services_json):
print('You must provide a google-services.json file for '
'--enable-google-services')
sys.exit(1)

make_package(args)

return args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
{% if args.enable_google_services %}
classpath 'com.google.gms:google-services:4.3.14'
{% endif %}
}
}

Expand All @@ -27,6 +30,9 @@ apply plugin: 'com.android.library'
{% else %}
apply plugin: 'com.android.application'
{% endif %}
{% if args.enable_google_services %}
apply plugin: 'com.google.gms.google-services'
{% endif %}

android {
compileSdkVersion {{ android_api }}
Expand Down
9 changes: 8 additions & 1 deletion pythonforandroid/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,10 @@ def add_parser(subparsers, *args, **kwargs):
parser_packaging.add_argument(
'--signkeypw', dest='signkeypw', action='store', default=None,
help='Password for key alias')
parser_packaging.add_argument(
'--google-services-json', dest='google_services_json',
default='google-services.json',
help='Path to google-services.json file')

add_parser(
subparsers,
Expand Down Expand Up @@ -627,6 +631,8 @@ def add_parser(subparsers, *args, **kwargs):
args.unknown_args += ["--activity-class-name", args.activity_class_name]
if hasattr(args, "service_class_name") and args.service_class_name != 'org.kivy.android.PythonService':
args.unknown_args += ["--service-class-name", args.service_class_name]
if hasattr(args, "google_services_json") and args.google_services_json:
args.unknown_args += ["--google-services-json", args.google_services_json]

self.args = args

Expand Down Expand Up @@ -990,7 +996,8 @@ def _fix_args(args):

fix_args = ('--dir', '--private', '--add-jar', '--add-source',
'--whitelist', '--blacklist', '--presplash', '--icon',
'--icon-bg', '--icon-fg', '--fileprovider-paths')
'--icon-bg', '--icon-fg', '--fileprovider-paths',
'--google-services-json')
unknown_args = args.unknown_args

for asset in args.assets:
Expand Down

0 comments on commit b89734a

Please sign in to comment.