Skip to content

Commit 030b57e

Browse files
committed
added --network-security-config, --uses-cleartext-traffic, --activity-class-name and --service-class-name input parameters
1 parent 1518d9c commit 030b57e

File tree

9 files changed

+56
-8
lines changed

9 files changed

+56
-8
lines changed

pythonforandroid/bootstraps/common/build/build.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ def make_package(args):
430430
service = True
431431

432432
service_names = []
433+
base_service_class = args.service_class_name.split('.')[-1]
433434
for sid, spec in enumerate(args.services):
434435
spec = spec.split(':')
435436
name = spec[0]
@@ -454,6 +455,7 @@ def make_package(args):
454455
foreground=foreground,
455456
sticky=sticky,
456457
service_id=sid + 1,
458+
base_service_class=base_service_class,
457459
)
458460

459461
# Find the SDK directory and target API
@@ -803,6 +805,14 @@ def parse_args_and_make_package(args=None):
803805
ap.add_argument('--manifest-placeholders', dest='manifest_placeholders',
804806
default='[:]', help=('Inject build variables into the manifest '
805807
'via the manifestPlaceholders property'))
808+
ap.add_argument('--network-security-config', dest='network_security_config', default='',
809+
help='Add a Network Security Configuration file path to AndroidManifest.xml')
810+
ap.add_argument('--uses-cleartext-traffic', dest='uses_cleartext_traffic', default='',
811+
help='Indicate that app intends to use cleartext network traffic in AndroidManifest.xml')
812+
ap.add_argument('--service-class-name', dest='service_class_name', default='org.kivy.android.PythonService',
813+
help='Use that parameter if you need to implement your own PythonServive Java class')
814+
ap.add_argument('--activity-class-name', dest='activity_class_name', default='org.kivy.android.PythonActivity',
815+
help='The full java class name of the main activity')
806816

807817
# Put together arguments, and add those from .p4a config file:
808818
if args is None:
@@ -822,6 +832,7 @@ def _read_configuration():
822832
_read_configuration()
823833

824834
args = ap.parse_args(args)
835+
825836
args.ignore_path = []
826837

827838
if args.name and args.name[0] == '"' and args.name[-1] == '"':

pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import android.content.Intent;
44
import android.content.Context;
5-
import org.kivy.android.PythonService;
5+
import {{ args.service_class_name }};
66

77

8-
public class Service{{ name|capitalize }} extends PythonService {
8+
public class Service{{ name|capitalize }} extends {{ base_service_class }} {
99
{% if sticky %}
1010
@Override
1111
public int startType() {

pythonforandroid/bootstraps/sdl2/build/templates/AndroidManifest.tmpl.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@
5959
android:icon="@drawable/icon"
6060
android:allowBackup="{{ args.allow_backup }}"
6161
{% if args.backup_rules %}android:fullBackupContent="@xml/{{ args.backup_rules }}"{% endif %}
62+
{% if args.network_security_config %}android:networkSecurityConfig="{{ args.network_security_config }}"{% endif %}
63+
{% if args.uses_cleartext_traffic %}android:usesCleartextTraffic="{{ args.uses_cleartext_traffic }}"{% endif %}
6264
android:theme="{{args.android_apptheme}}{% if not args.window %}.Fullscreen{% endif %}"
63-
android:hardwareAccelerated="true" >
65+
android:hardwareAccelerated="true"
66+
>
6467
{% for l in args.android_used_libs %}
6568
<uses-library android:name="{{ l }}" />
6669
{% endfor %}
@@ -110,7 +113,7 @@
110113
{% endif %}
111114

112115
{% if service or args.launcher %}
113-
<service android:name="org.kivy.android.PythonService"
116+
<service android:name="{{ args.service_class_name }}"
114117
android:process=":pythonservice" />
115118
{% endif %}
116119
{% for name in service_names %}

pythonforandroid/build.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ def __init__(self):
448448
self.copy_libs = False
449449

450450
self.activity_class_name = u'org.kivy.android.PythonActivity'
451+
self.service_class_name = u'org.kivy.android.PythonService'
451452

452453
# this list should contain all Archs, it is pruned later
453454
self.archs = (

pythonforandroid/recipes/android/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def prebuild_arch(self, arch):
5353
'JNI_NAMESPACE': jni_ns,
5454
'ACTIVITY_CLASS_NAME': self.ctx.activity_class_name,
5555
'ACTIVITY_CLASS_NAMESPACE': self.ctx.activity_class_name.replace('.', '/'),
56+
'SERVICE_CLASS_NAME': self.ctx.service_class_name,
5657
}
5758

5859
# create config files for Cython, C and Python

pythonforandroid/recipes/android/src/android/broadcast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Broadcast receiver bridge
33

44
from jnius import autoclass, PythonJavaClass, java_method
5-
from android.config import JAVA_NAMESPACE, JNI_NAMESPACE, ACTIVITY_CLASS_NAME
5+
from android.config import JAVA_NAMESPACE, JNI_NAMESPACE, ACTIVITY_CLASS_NAME, SERVICE_CLASS_NAME
66

77

88
class BroadcastReceiver(object):
@@ -72,7 +72,7 @@ def stop(self):
7272
def context(self):
7373
from os import environ
7474
if 'PYTHON_SERVICE_ARGUMENT' in environ:
75-
PythonService = autoclass(JAVA_NAMESPACE + '.PythonService')
75+
PythonService = autoclass(SERVICE_CLASS_NAME)
7676
return PythonService.mService
7777
PythonActivity = autoclass(ACTIVITY_CLASS_NAME)
7878
return PythonActivity.mActivity

pythonforandroid/recipes/android/src/android/storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from jnius import autoclass, cast
22
import os
33

4-
from android.config import JAVA_NAMESPACE, ACTIVITY_CLASS_NAME
4+
from android.config import ACTIVITY_CLASS_NAME, SERVICE_CLASS_NAME
55

66

77
Environment = autoclass('android.os.Environment')
@@ -34,7 +34,7 @@ def _get_activity():
3434
activity = PythonActivity.mActivity
3535
if activity is None:
3636
# assume we're running from the background service
37-
PythonService = autoclass(JAVA_NAMESPACE + '.' + 'PythonService')
37+
PythonService = autoclass(SERVICE_CLASS_NAME)
3838
activity = PythonService.mService
3939
return activity
4040

pythonforandroid/toolchain.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,21 @@ def __init__(self):
380380
dest='activity_class_name', default='org.kivy.android.PythonActivity',
381381
help='The full java class name of the main activity')
382382

383+
generic_parser.add_argument(
384+
'--service-class-name',
385+
dest='service_class_name', default='org.kivy.android.PythonService',
386+
help='Full java package name of the PythonService class')
387+
388+
generic_parser.add_argument(
389+
'--network-security-config',
390+
dest='network_security_config', default=None,
391+
help='Add a Network Security Configuration file path to AndroidManifest.xml')
392+
393+
generic_parser.add_argument(
394+
'--uses-cleartext-traffic',
395+
dest='uses_cleartext_traffic', default=None,
396+
help='Indicate that app intends to use cleartext network traffic in AndroidManifest.xml')
397+
383398
generic_parser.add_argument(
384399
'--java-build-tool',
385400
dest='java_build_tool', default='auto',
@@ -613,6 +628,14 @@ def add_parser(subparsers, *args, **kwargs):
613628
args.unknown_args += ["--with-debug-symbols"]
614629
if hasattr(args, "ignore_setup_py") and args.ignore_setup_py:
615630
args.use_setup_py = False
631+
if hasattr(args, "activity_class_name") and args.activity_class_name != 'org.kivy.android.PythonActivity':
632+
args.unknown_args += ["--activity-class-name", args.activity_class_name]
633+
if hasattr(args, "service_class_name") and args.service_class_name != 'org.kivy.android.PythonService':
634+
args.unknown_args += ["--service-class-name", args.service_class_name]
635+
if hasattr(args, "network_security_config") and args.network_security_config is not None:
636+
args.unknown_args += ["--network-security-config", args.network_security_config]
637+
if hasattr(args, "uses_cleartext_traffic") and args.uses_cleartext_traffic is not None:
638+
args.unknown_args += ["--uses-cleartext-traffic", args.uses_cleartext_traffic]
616639

617640
self.args = args
618641

@@ -709,6 +732,9 @@ def add_parser(subparsers, *args, **kwargs):
709732
self.ctx.copy_libs = args.copy_libs
710733

711734
self.ctx.activity_class_name = args.activity_class_name
735+
self.ctx.network_security_config = args.network_security_config
736+
self.ctx.uses_cleartext_traffic = args.uses_cleartext_traffic
737+
self.ctx.service_class_name = args.service_class_name
712738

713739
# Each subparser corresponds to a method
714740
command = args.subparser_name.replace('-', '_')

tests/test_toolchain.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ def test_create(self):
6161
'--requirements=python3',
6262
'--dist-name=test_toolchain',
6363
'--activity-class-name=abc.myapp.android.CustomPythonActivity',
64+
'--service-class-name=xyz.myapp.android.CustomPythonService',
65+
'--network-security-config=1',
66+
'--uses-cleartext-traffic=1',
6467
]
6568
with patch_sys_argv(argv), mock.patch(
6669
'pythonforandroid.build.get_available_apis'
@@ -80,6 +83,9 @@ def test_create(self):
8083
'/tmp/android-ndk/platforms/android-21/arch-arm', True)
8184
tchain = ToolchainCL()
8285
assert tchain.ctx.activity_class_name == 'abc.myapp.android.CustomPythonActivity'
86+
assert tchain.ctx.service_class_name == 'xyz.myapp.android.CustomPythonService'
87+
assert tchain.ctx.network_security_config == '1'
88+
assert tchain.ctx.uses_cleartext_traffic == '1'
8389
assert m_get_available_apis.call_args_list in [
8490
[mock.call('/tmp/android-sdk')], # linux case
8591
[mock.call('/private/tmp/android-sdk')] # macos case

0 commit comments

Comments
 (0)