Skip to content

Commit 934cf66

Browse files
authored
Merge pull request #11 from endlessm/google-services
Support Google Services and extra Gradle plugins
2 parents 6cd170a + 8a506ef commit 934cf66

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

doc/source/buildoptions.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ 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.
97+
- ``--add-gradle-plugins``: Add a plugin for gradle. The format of the option
98+
is ``<plugin-id>:<classpath>``. The option can be specified multiple times.
9499

95100

96101
webview
@@ -152,6 +157,11 @@ ready.
152157
- ``add-source``: Add a source directory to the app's Java code.
153158
- ``--port``: The port on localhost that the WebView will
154159
access. Defaults to 5000.
160+
- ``--enable-google-services``: Enable the Google Services Gradle plugin.
161+
This option requires a ``google-services.json`` file in root of the
162+
project directory.
163+
- ``--add-gradle-plugins``: Add a plugin for gradle. The format of the option
164+
is ``<plugin-id>:<classpath>``. The option can be specified multiple times.
155165

156166

157167
service_library
@@ -177,6 +187,11 @@ systems and frameworks.
177187
- ``--add-jar``: The path to a .jar file to include in the APK. To
178188
include multiple jar files, pass this argument multiple times.
179189
- ``add-source``: Add a source directory to the app's Java code.
190+
- ``--enable-google-services``: Enable the Google Services Gradle plugin.
191+
This option requires a ``google-services.json`` file in root of the
192+
project directory.
193+
- ``--add-gradle-plugin``: Add a plugin for gradle. The format of the option
194+
is ``<plugin-id>:<classpath>``. The option can be specified multiple times.
180195

181196

182197
Requirements blacklist (APK size optimization)

pythonforandroid/bootstraps/common/build/build.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,16 @@ 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+
554+
# Convert the gradle_plugins args to a list of dicts with id and
555+
# classpath keys.
556+
gradle_plugins = [
557+
dict(zip(('id', 'classpath'), spec.split(':', maxsplit=1)))
558+
for spec in args.gradle_plugins
559+
]
560+
551561
# gradle build templates
552562
render(
553563
'build.tmpl.gradle',
@@ -559,6 +569,7 @@ def make_package(args):
559569
build_tools_version=build_tools_version,
560570
debug_build="debug" in args.build_mode,
561571
is_library=(get_bootstrap_name() == 'service_library'),
572+
gradle_plugins=gradle_plugins,
562573
)
563574

564575
# gradle properties
@@ -761,6 +772,12 @@ def parse_args_and_make_package(args=None):
761772
default=[],
762773
action='append',
763774
help='Ddd a repository for gradle')
775+
ap.add_argument('--add-gradle-plugin', dest='gradle_plugins',
776+
default=[],
777+
action='append',
778+
help=('Add a plugin for gradle. The format of the option '
779+
'is <plugin-id>:<classpath>. The option can be '
780+
'specified multiple times.'))
764781
ap.add_argument('--add-packaging-option', dest='packaging_options',
765782
default=[],
766783
action='append',
@@ -864,6 +881,15 @@ def parse_args_and_make_package(args=None):
864881
ap.add_argument('--activity-class-name', dest='activity_class_name', default=DEFAULT_PYTHON_ACTIVITY_JAVA_CLASS,
865882
help='The full java class name of the main activity')
866883

884+
ap.add_argument('--enable-google-services', dest='enable_google_services',
885+
action='store_true',
886+
help=('Enable the Google Services Gradle plugin. '
887+
'This requires a google-services.json in the root '
888+
'of the project.'))
889+
ap.add_argument('--google-services-json', dest='google_services_json',
890+
default='google-services.json',
891+
help='Path to google-services.json file')
892+
867893
# Put together arguments, and add those from .p4a config file:
868894
if args is None:
869895
args = sys.argv[1:]
@@ -949,6 +975,12 @@ def _read_configuration():
949975
'--launcher (SDL2 bootstrap only)' +
950976
'to have something to launch inside the .apk!')
951977
sys.exit(1)
978+
979+
if args.enable_google_services and not exists(args.google_services_json):
980+
print('You must provide a google-services.json file for '
981+
'--enable-google-services')
982+
sys.exit(1)
983+
952984
make_package(args)
953985

954986
return args

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ 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 %}
12+
{% for plugin in gradle_plugins %}
13+
classpath '{{ plugin.classpath }}'
14+
{% endfor %}
915
}
1016
}
1117

@@ -27,6 +33,12 @@ apply plugin: 'com.android.library'
2733
{% else %}
2834
apply plugin: 'com.android.application'
2935
{% endif %}
36+
{% if args.enable_google_services %}
37+
apply plugin: 'com.google.gms.google-services'
38+
{% endif %}
39+
{% for plugin in gradle_plugins %}
40+
apply plugin: '{{ plugin.id }}'
41+
{% endfor %}
3042

3143
android {
3244
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)