Skip to content

Commit 5c1f607

Browse files
committed
added --network-security-config, --uses-cleartext-traffic, --activity-class-name and --service-class-name input parameters
1 parent 690dd18 commit 5c1f607

File tree

10 files changed

+68
-8
lines changed

10 files changed

+68
-8
lines changed

pythonforandroid/bootstraps/common/build/build.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ def make_package(args):
395395
if not args.activity_launch_mode:
396396
args.activity_launch_mode = ''
397397

398+
# if not args.service_class_name:
399+
# args.service_class_name = 'org.kivy.android.PythonService'
400+
398401
if args.extra_source_dirs:
399402
esd = []
400403
for spec in args.extra_source_dirs:
@@ -418,6 +421,7 @@ def make_package(args):
418421
service = True
419422

420423
service_names = []
424+
base_service_class = args.service_class_name.split('.')[-1]
421425
for sid, spec in enumerate(args.services):
422426
spec = spec.split(':')
423427
name = spec[0]
@@ -442,6 +446,7 @@ def make_package(args):
442446
foreground=foreground,
443447
sticky=sticky,
444448
service_id=sid + 1,
449+
base_service_class=base_service_class,
445450
)
446451

447452
# Find the SDK directory and target API
@@ -478,6 +483,7 @@ def make_package(args):
478483
args.backup_rules = split(args.backup_rules)[1][:-4]
479484

480485
# Render out android manifest:
486+
print("Rendering AndroidManifest.xml...", args)
481487
manifest_path = "src/main/AndroidManifest.xml"
482488
render_args = {
483489
"args": args,
@@ -633,6 +639,10 @@ def parse_args_and_make_package(args=None):
633639
help='Custom key=value to add in application metadata')
634640
ap.add_argument('--uses-library', dest='android_used_libs', action='append', default=[],
635641
help='Used shared libraries included using <uses-library> tag in AndroidManifest.xml')
642+
ap.add_argument('--network-security-config', dest='network_security_config',
643+
help='Add a Network Security Configuration file path to AndroidManifest.xml')
644+
ap.add_argument('--uses-cleartext-traffic', dest='uses_cleartext_traffic',
645+
help='Indicate that app intends to use cleartext network traffic in AndroidManifest.xml')
636646
ap.add_argument('--asset', dest='assets',
637647
action="append", default=[],
638648
metavar="/path/to/source:dest",
@@ -778,12 +788,22 @@ def parse_args_and_make_package(args=None):
778788
ap.add_argument('--manifest-placeholders', dest='manifest_placeholders',
779789
default='[:]', help=('Inject build variables into the manifest '
780790
'via the manifestPlaceholders property'))
791+
ap.add_argument('--network-security-config', dest='network_security_config', default='',
792+
help='Add a Network Security Configuration file path to AndroidManifest.xml')
793+
ap.add_argument('--uses-cleartext-traffic', dest='uses_cleartext_traffic', default='',
794+
help='Indicate that app intends to use cleartext network traffic in AndroidManifest.xml')
795+
ap.add_argument('--service-class-name', dest='service_class_name', default='org.kivy.android.PythonService',
796+
help='Use that parameter if you need to implement your own PythonServive Java class')
797+
ap.add_argument('--activity-class-name', dest='activity_class_name', default='org.kivy.android.PythonActivity',
798+
help='The full java class name of the main activity')
781799

782800
# Put together arguments, and add those from .p4a config file:
783801
if args is None:
784802
args = sys.argv[1:]
803+
print('args was empty, set from sys.argv')
785804

786805
def _read_configuration():
806+
print("Looking for p4a configuration file in %r" % os.path.abspath('.p4a'))
787807
if not exists(".p4a"):
788808
return
789809
print("Reading .p4a configuration")
@@ -793,10 +813,12 @@ def _read_configuration():
793813
for line in lines if not line.startswith("#")]
794814
for line in lines:
795815
for arg in line:
816+
print('Added option: %r' % arg)
796817
args.append(arg)
797818
_read_configuration()
798819

799820
args = ap.parse_args(args)
821+
800822
args.ignore_path = []
801823

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

pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ protected int getServiceId() {
9292
}
9393

9494
protected void doStartForeground(Bundle extras) {
95+
Log.v("python service", "doStartForeground");
9596
String serviceTitle = extras.getString("serviceTitle");
9697
String serviceDescription = extras.getString("serviceDescription");
9798
Notification notification;
@@ -137,6 +138,7 @@ protected void doStartForeground(Bundle extras) {
137138

138139
@Override
139140
public void onDestroy() {
141+
Log.v("python service", "onDestroy");
140142
super.onDestroy();
141143
pythonThread = null;
142144
if (autoRestartService && startIntent != null) {
@@ -152,17 +154,20 @@ public void onDestroy() {
152154
*/
153155
@Override
154156
public void onTaskRemoved(Intent rootIntent) {
157+
Log.v("python service", "onTaskRemoved");
155158
super.onTaskRemoved(rootIntent);
156159
stopSelf();
157160
}
158161

159162
@Override
160163
public void run(){
164+
Log.v("python service", "run");
161165
String app_root = getFilesDir().getAbsolutePath() + "/app";
162166
File app_root_file = new File(app_root);
163167
PythonUtil.loadLibraries(app_root_file,
164168
new File(getApplicationInfo().nativeLibraryDir));
165169
this.mService = this;
170+
Log.v("python service", "run before nativeStart");
166171
nativeStart(
167172
androidPrivate, androidArgument,
168173
serviceEntrypoint, pythonName,

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 JAVA_NAMESPACE, 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ 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',
6465
]
6566
with patch_sys_argv(argv), mock.patch(
6667
'pythonforandroid.build.get_available_apis'
@@ -80,6 +81,7 @@ def test_create(self):
8081
'/tmp/android-ndk/platforms/android-21/arch-arm', True)
8182
tchain = ToolchainCL()
8283
assert tchain.ctx.activity_class_name == 'abc.myapp.android.CustomPythonActivity'
84+
assert tchain.ctx.service_class_name == 'xyz.myapp.android.CustomPythonService'
8385
assert m_get_available_apis.call_args_list in [
8486
[mock.call('/tmp/android-sdk')], # linux case
8587
[mock.call('/private/tmp/android-sdk')] # macos case

0 commit comments

Comments
 (0)