Skip to content

Fixing service_library bootstrap + .aar build. #2612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on: ['push', 'pull_request']
env:
APK_ARTIFACT_FILENAME: bdist_unit_tests_app-debug-1.1-.apk
AAB_ARTIFACT_FILENAME: bdist_unit_tests_app-release-1.1-.aab
AAR_ARTIFACT_FILENAME: bdist_unit_tests_app-release-1.1-.aar
PYTHONFORANDROID_PREREQUISITES_INSTALL_INTERACTIVE: 0

jobs:
Expand Down Expand Up @@ -186,6 +187,47 @@ jobs:
name: ${{ matrix.runs_on }}-${{ matrix.bootstrap.name}}-${{ env.AAB_ARTIFACT_FILENAME }}
path: aabs/${{ matrix.runs_on }}-${{ matrix.bootstrap.name}}-${{ env.AAB_ARTIFACT_FILENAME }}


ubuntu_build_aar:
name: Unit test aar [ ${{ matrix.runs_on }} ]
needs: [flake8]
runs-on: ${{ matrix.runs_on }}
continue-on-error: true
strategy:
matrix:
runs_on: [ubuntu-latest]
bootstrap:
- name: service_library
target: testapps-service_library-aar
steps:
- name: Checkout python-for-android
uses: actions/checkout@v2
# helps with GitHub runner getting out of space
- name: Free disk space
run: |
df -h
sudo swapoff -a
sudo rm -f /swapfile
sudo apt -y clean
docker rmi $(docker image ls -aq)
df -h
- name: Pull docker image
run: |
make docker/pull
- name: Build Android AAR Python 3 (arm64-v8a)
run: |
mkdir -p aars
make docker/run/make/with-artifact/aar/${{ matrix.bootstrap.target }}
- name: Rename artifact to include the build platform name
run: |
mv aars/${{ env.AAR_ARTIFACT_FILENAME }} aars/${{ matrix.runs_on }}-${{ matrix.bootstrap.name}}-${{ env.AAR_ARTIFACT_FILENAME }}
- name: Upload aar artifact
uses: actions/upload-artifact@v1
with:
name: ${{ matrix.runs_on }}-${{ matrix.bootstrap.name}}-${{ env.AAR_ARTIFACT_FILENAME }}
path: aars/${{ matrix.runs_on }}-${{ matrix.bootstrap.name}}-${{ env.AAR_ARTIFACT_FILENAME }}


macos_build_aab:
name: Unit test aab [ ${{ matrix.runs_on }} | ${{ matrix.bootstrap.name }} ]
needs: [flake8]
Expand Down
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ testapps-with-numpy-aab: virtualenv
--requirements libffi,sdl2,pyjnius,kivy,python3,openssl,requests,urllib3,chardet,idna,sqlite3,setuptools,numpy \
--arch=armeabi-v7a --arch=arm64-v8a --arch=x86_64 --arch=x86 --release

testapps-service_library-aar: virtualenv
. $(ACTIVATE) && cd testapps/on_device_unit_tests/ && \
python setup.py aar --sdk-dir $(ANDROID_SDK_HOME) --ndk-dir $(ANDROID_NDK_HOME) \
--bootstrap service_library \
--requirements python3 \
--arch=arm64-v8a --arch=x86 --release

testapps-webview: virtualenv
. $(ACTIVATE) && cd testapps/on_device_unit_tests/ && \
python setup.py apk --sdk-dir $(ANDROID_SDK_HOME) --ndk-dir $(ANDROID_NDK_HOME) \
Expand Down Expand Up @@ -102,6 +109,11 @@ docker/run/make/with-artifact/apk/%: docker/build
docker cp p4a-latest:/home/user/app/testapps/on_device_unit_tests/bdist_unit_tests_app-debug-1.1-.apk ./apks
docker rm -fv p4a-latest

docker/run/make/with-artifact/aar/%: docker/build
docker run --name p4a-latest --env-file=.env $(DOCKER_IMAGE) make $*
docker cp p4a-latest:/home/user/app/testapps/on_device_unit_tests/bdist_unit_tests_app-release-1.1-.aar ./aars
docker rm -fv p4a-latest

docker/run/make/with-artifact/aab/%: docker/build
docker run --name p4a-latest --env-file=.env $(DOCKER_IMAGE) make $*
docker cp p4a-latest:/home/user/app/testapps/on_device_unit_tests/bdist_unit_tests_app-release-1.1-.aab ./aabs
Expand Down
1 change: 1 addition & 0 deletions pythonforandroid/bdistapk.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def _set_user_options():

BdistAPK.user_options = user_options
BdistAAB.user_options = user_options
BdistAAR.user_options = user_options


_set_user_options()
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected void doStartForeground(Bundle extras) {
Context context = getApplicationContext();
Intent contextIntent = new Intent(context, PythonActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
notification = new Notification(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.kivy.android;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;

public class GenericBroadcastReceiver extends BroadcastReceiver {

GenericBroadcastReceiverCallback listener;

public GenericBroadcastReceiver(GenericBroadcastReceiverCallback listener) {
super();
this.listener = listener;
}

public void onReceive(Context context, Intent intent) {
this.listener.onReceive(context, intent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.kivy.android;

import android.content.Intent;
import android.content.Context;

public interface GenericBroadcastReceiverCallback {
void onReceive(Context context, Intent intent);
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,42 @@ public class Service{{ name|capitalize }} extends PythonService {

private static final String TAG = "PythonService";

{% if sticky %}
@Override
public int startType() {
return START_STICKY;
}
{% endif %}

@Override
protected int getServiceId() {
return {{ service_id }};
}

public static void prepare(Context ctx) {
String appRoot = PythonUtil.getAppRoot(ctx);
Log.v(TAG, "Ready to unpack");
File app_root_file = new File(appRoot);
PythonUtil.unpackData(ctx, "private", app_root_file, false);
PythonUtil.unpackAsset(ctx, "private", app_root_file, true);
PythonUtil.unpackPyBundle(ctx, ctx.getApplicationInfo().nativeLibraryDir + "/" + "libpybundle", app_root_file, false);
}

public static void start(Context ctx, String pythonServiceArgument) {
Intent intent = getDefaultIntent(ctx, pythonServiceArgument);

//foreground: {{foreground}}
{% if foreground %}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ctx.startForegroundService(intent);
} else {
ctx.startService(intent);
}
{% else %}
ctx.startService(intent);
{% endif %}
}

static public Intent getDefaultIntent(Context ctx, String pythonServiceArgument) {
String appRoot = PythonUtil.getAppRoot(ctx);
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
intent.putExtra("androidPrivate", appRoot);
Expand All @@ -36,16 +64,19 @@ public static void start(Context ctx, String pythonServiceArgument) {
intent.putExtra("androidUnpack", appRoot);
intent.putExtra("pythonPath", appRoot + ":" + appRoot + "/lib");
intent.putExtra("pythonServiceArgument", pythonServiceArgument);
return intent;
}

//foreground: {{foreground}}
{% if foreground %}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ctx.startForegroundService(intent);
} else {
ctx.startService(intent);
}
{% else %}
ctx.startService(intent);
{% endif %}
@Override
protected Intent getThisDefaultIntent(Context ctx, String pythonServiceArgument) {
return Service{{ name|capitalize }}.getDefaultIntent(ctx, pythonServiceArgument);
}



static public void stop(Context ctx) {
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
ctx.stopService(intent);
}

}
25 changes: 11 additions & 14 deletions pythonforandroid/bootstraps/service_only/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,28 @@ def assemble_distribution(self):
with open('local.properties', 'w') as fileh:
fileh.write('sdk.dir={}'.format(self.ctx.sdk_dir))

arch = self.ctx.archs[0]
if len(self.ctx.archs) > 1:
raise ValueError('built for more than one arch, but bootstrap cannot handle that yet')
info('Bootstrap running with arch {}'.format(arch))

with current_directory(self.dist_dir):
info('Copying python distribution')

self.distribute_libs(arch, [self.ctx.get_libs_dir(arch.arch)])
self.distribute_aars(arch)
self.distribute_javaclasses(self.ctx.javaclass_dir,
dest_dir=join("src", "main", "java"))

python_bundle_dir = join(f'_python_bundle__{arch.arch}', '_python_bundle')
ensure_dir(python_bundle_dir)
site_packages_dir = self.ctx.python_recipe.create_python_bundle(
join(self.dist_dir, python_bundle_dir), arch)
for arch in self.ctx.archs:
self.distribute_libs(arch, [self.ctx.get_libs_dir(arch.arch)])
self.distribute_aars(arch)

python_bundle_dir = join(f'_python_bundle__{arch.arch}', '_python_bundle')
ensure_dir(python_bundle_dir)
site_packages_dir = self.ctx.python_recipe.create_python_bundle(
join(self.dist_dir, python_bundle_dir), arch)
if not self.ctx.with_debug_symbols:
self.strip_libraries(arch)
self.fry_eggs(site_packages_dir)

if 'sqlite3' not in self.ctx.recipe_build_order:
with open('blacklist.txt', 'a') as fileh:
fileh.write('\nsqlite3/*\nlib-dynload/_sqlite3.so\n')

if not self.ctx.with_debug_symbols:
self.strip_libraries(arch)
self.fry_eggs(site_packages_dir)
super().assemble_distribution()


Expand Down
2 changes: 1 addition & 1 deletion pythonforandroid/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ def _build_package(self, args, package_type):
)
gradle_task = "assembleDebug"
elif args.build_mode == "release":
if package_type == "apk":
if package_type in ["apk", "aar"]:
gradle_task = "assembleRelease"
elif package_type == "aab":
gradle_task = "bundleRelease"
Expand Down
11 changes: 11 additions & 0 deletions testapps/on_device_unit_tests/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@
'permissions': ['INTERNET', 'VIBRATE'],
'orientation': 'sensor',
'service': 'P4a_test_service:app_service.py',
},
'aar':
{
'requirements' : 'python3',
'android-api': 27,
'ndk-api': 21,
'dist-name': 'bdist_unit_tests_app',
'arch': 'arm64-v8a',
'bootstrap' : 'service_library',
'permissions': ['INTERNET', 'VIBRATE'],
'service': 'P4a_test_service:app_service.py',
}
}

Expand Down