Skip to content

Commit b89734a

Browse files
committed
Add support for Google Services gradle plugin
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
1 parent 6cd170a commit b89734a

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

doc/source/buildoptions.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ options (this list may not be exhaustive):
9191
- ``--add-source``: Add a source directory to the app's Java code.
9292
- ``--no-compile-pyo``: Do not optimise .py files to .pyo.
9393
- ``--enable-androidx``: Enable AndroidX support library.
94+
- ``--enable-google-services``: Enable the Google Services Gradle plugin.
95+
This option requires a ``google-services.json`` file in root of the
96+
project directory.
9497

9598

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

156162

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

181190

182191
Requirements blacklist (APK size optimization)

pythonforandroid/bootstraps/common/build/build.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,9 @@ def make_package(args):
548548
if args.fileprovider_paths:
549549
shutil.copy(args.fileprovider_paths, join(res_dir, "xml/file_paths.xml"))
550550

551+
if args.enable_google_services:
552+
shutil.copy(args.google_services_json, 'src/google-services.json')
553+
551554
# gradle build templates
552555
render(
553556
'build.tmpl.gradle',
@@ -864,6 +867,15 @@ def parse_args_and_make_package(args=None):
864867
ap.add_argument('--activity-class-name', dest='activity_class_name', default=DEFAULT_PYTHON_ACTIVITY_JAVA_CLASS,
865868
help='The full java class name of the main activity')
866869

870+
ap.add_argument('--enable-google-services', dest='enable_google_services',
871+
action='store_true',
872+
help=('Enable the Google Services Gradle plugin. '
873+
'This requires a google-services.json in the root '
874+
'of the project.'))
875+
ap.add_argument('--google-services-json', dest='google_services_json',
876+
default='google-services.json',
877+
help='Path to google-services.json file')
878+
867879
# Put together arguments, and add those from .p4a config file:
868880
if args is None:
869881
args = sys.argv[1:]
@@ -949,6 +961,12 @@ def _read_configuration():
949961
'--launcher (SDL2 bootstrap only)' +
950962
'to have something to launch inside the .apk!')
951963
sys.exit(1)
964+
965+
if args.enable_google_services and not exists(args.google_services_json):
966+
print('You must provide a google-services.json file for '
967+
'--enable-google-services')
968+
sys.exit(1)
969+
952970
make_package(args)
953971

954972
return args

pythonforandroid/bootstraps/common/build/templates/build.tmpl.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ buildscript {
66
}
77
dependencies {
88
classpath 'com.android.tools.build:gradle:7.1.2'
9+
{% if args.enable_google_services %}
10+
classpath 'com.google.gms:google-services:4.3.14'
11+
{% endif %}
912
}
1013
}
1114

@@ -27,6 +30,9 @@ apply plugin: 'com.android.library'
2730
{% else %}
2831
apply plugin: 'com.android.application'
2932
{% endif %}
33+
{% if args.enable_google_services %}
34+
apply plugin: 'com.google.gms.google-services'
35+
{% endif %}
3036

3137
android {
3238
compileSdkVersion {{ android_api }}

pythonforandroid/toolchain.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,10 @@ def add_parser(subparsers, *args, **kwargs):
552552
parser_packaging.add_argument(
553553
'--signkeypw', dest='signkeypw', action='store', default=None,
554554
help='Password for key alias')
555+
parser_packaging.add_argument(
556+
'--google-services-json', dest='google_services_json',
557+
default='google-services.json',
558+
help='Path to google-services.json file')
555559

556560
add_parser(
557561
subparsers,
@@ -627,6 +631,8 @@ def add_parser(subparsers, *args, **kwargs):
627631
args.unknown_args += ["--activity-class-name", args.activity_class_name]
628632
if hasattr(args, "service_class_name") and args.service_class_name != 'org.kivy.android.PythonService':
629633
args.unknown_args += ["--service-class-name", args.service_class_name]
634+
if hasattr(args, "google_services_json") and args.google_services_json:
635+
args.unknown_args += ["--google-services-json", args.google_services_json]
630636

631637
self.args = args
632638

@@ -990,7 +996,8 @@ def _fix_args(args):
990996

991997
fix_args = ('--dir', '--private', '--add-jar', '--add-source',
992998
'--whitelist', '--blacklist', '--presplash', '--icon',
993-
'--icon-bg', '--icon-fg', '--fileprovider-paths')
999+
'--icon-bg', '--icon-fg', '--fileprovider-paths',
1000+
'--google-services-json')
9941001
unknown_args = args.unknown_args
9951002

9961003
for asset in args.assets:

0 commit comments

Comments
 (0)