Skip to content

Commit

Permalink
Merge pull request #11 from endlessm/google-services
Browse files Browse the repository at this point in the history
Support Google Services and extra Gradle plugins
  • Loading branch information
manuq authored Oct 24, 2022
2 parents 6cd170a + 8a506ef commit 934cf66
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
15 changes: 15 additions & 0 deletions doc/source/buildoptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ 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.
- ``--add-gradle-plugins``: Add a plugin for gradle. The format of the option
is ``<plugin-id>:<classpath>``. The option can be specified multiple times.


webview
Expand Down Expand Up @@ -152,6 +157,11 @@ 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.
- ``--add-gradle-plugins``: Add a plugin for gradle. The format of the option
is ``<plugin-id>:<classpath>``. The option can be specified multiple times.


service_library
Expand All @@ -177,6 +187,11 @@ 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.
- ``--add-gradle-plugin``: Add a plugin for gradle. The format of the option
is ``<plugin-id>:<classpath>``. The option can be specified multiple times.


Requirements blacklist (APK size optimization)
Expand Down
32 changes: 32 additions & 0 deletions pythonforandroid/bootstraps/common/build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,16 @@ 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')

# Convert the gradle_plugins args to a list of dicts with id and
# classpath keys.
gradle_plugins = [
dict(zip(('id', 'classpath'), spec.split(':', maxsplit=1)))
for spec in args.gradle_plugins
]

# gradle build templates
render(
'build.tmpl.gradle',
Expand All @@ -559,6 +569,7 @@ def make_package(args):
build_tools_version=build_tools_version,
debug_build="debug" in args.build_mode,
is_library=(get_bootstrap_name() == 'service_library'),
gradle_plugins=gradle_plugins,
)

# gradle properties
Expand Down Expand Up @@ -761,6 +772,12 @@ def parse_args_and_make_package(args=None):
default=[],
action='append',
help='Ddd a repository for gradle')
ap.add_argument('--add-gradle-plugin', dest='gradle_plugins',
default=[],
action='append',
help=('Add a plugin for gradle. The format of the option '
'is <plugin-id>:<classpath>. The option can be '
'specified multiple times.'))
ap.add_argument('--add-packaging-option', dest='packaging_options',
default=[],
action='append',
Expand Down Expand Up @@ -864,6 +881,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 +975,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,12 @@ 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 %}
{% for plugin in gradle_plugins %}
classpath '{{ plugin.classpath }}'
{% endfor %}
}
}

Expand All @@ -27,6 +33,12 @@ 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 %}
{% for plugin in gradle_plugins %}
apply plugin: '{{ plugin.id }}'
{% endfor %}

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 934cf66

Please sign in to comment.