Skip to content
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

Building an Android Library #1957

Closed
xuhcc opened this issue Aug 13, 2019 · 9 comments · May be fixed by #1963
Closed

Building an Android Library #1957

xuhcc opened this issue Aug 13, 2019 · 9 comments · May be fixed by #1963

Comments

@xuhcc
Copy link

xuhcc commented Aug 13, 2019

Is it possible to build an Android Library (AAR) with Python-For-Android?

There is a python package which I want to use as a library in existing Android application. I tried to integrate it in Kivy demo app and it is working. However I can't find a way to produce AAR instead of APK.

@xuhcc
Copy link
Author

xuhcc commented Aug 17, 2019

I managed to build an AAR containing a python service by using a custom bootstrap (based on service_only bootstrap). Maybe I'll publish my code later if it will work.

Similar issue: #1055

@inclement
Copy link
Member

No, this isn't currently supported. As you've found, it should be fairly straightforward though, and it's something I've been meaning to look into.

@gklasen
Copy link

gklasen commented Feb 17, 2020

@xuhcc I am having the same issue. How can i build this? Do you have a repo?

@xuhcc
Copy link
Author

xuhcc commented Feb 17, 2020

@gklasen You can use my fork of p4a, and there's also an open PR #1963. I didn't finished my work on it due to a shift of priorities in my project, but I think it is almost complete and ready to use.

I don't have a repo for my build environment, but here's the command that I used to build my library:

python3 -m pythonforandroid.toolchain apk \
    --debug \
    --bootstrap=library \
    --local-recipes=recipes \
    --requirements=python3,pyjnius,mypackage \
    --dist_name mypackage \
    --name "mypackage" \
    --version 0.1 \
    --package mypackage \
    --android_api 28 \
    --minsdk 21 \
    --ndk-api 21 \
    --private .buildozer/android/app \
    --service mypackage:service.py \
    --copy-libs \
    --arch x86 \
    --color=always \
    --storage-dir="/vagrant/.buildozer/android/platform/build" \
    --ndk-api=21 \
    --ndk-dir="/home/vagrant/.buildozer/android/platform/android-ndk-r17c/" \
    --sdk-dir="/home/vagrant/.buildozer/android/platform/android-sdk/" \
    --blacklist-requirements=sqlite3,libffi,android,openssl

@gklasen
Copy link

gklasen commented Feb 21, 2020

@xuhcc Thank you very much for your work on this! I managed to compile an .AAR-File, the only thing left is how can i refer functionality from it within Javacode. Is there an example, or a small hint?

Thank again, you already helped a lot.

@xuhcc
Copy link
Author

xuhcc commented Feb 21, 2020

In my case it worked like this:

service.py

import os
from jnius import autoclass

def send_broadcast(data):
    Intent = autoclass('android.content.Intent')
    intent = Intent('test')
    intent.putExtra('result', data)
    PythonService = autoclass('org.kivy.android.PythonService')
    pyService = PythonService.mService
    pyService.sendBroadcast(intent)

# Parse the input data at start
arg = os.environ['PYTHON_SERVICE_ARGUMENT']
# Do something with it...
# And then return result using broadcast
send_broadcast(result)

And on the app side (my app was written in JavaScript):

// Start service and provide input data
function startExampleService(arg) {
    const context = getApplicationContext();
    ExampleService.prepare(context);
    ExampleService.start(context, arg);
}

// Receive the result asynchronously
function registerReceiver() {
    application.registerBroadcastReceiver('test', (context, intent) => {
        const extras = intent.getExtras();
        const result = new java.lang.String(extras.getCharArray('result'));
    });
}

@gklasen
Copy link

gklasen commented Feb 26, 2020

@xuhcc , thanks, very close to finish. Last quick question, do i need a "main.py" as well or is "service.py" enough?

@xuhcc
Copy link
Author

xuhcc commented Feb 26, 2020

Yes, you only need the service.py file

@abhay6694
Copy link

@gklasen You can use my fork of p4a, and there's also an open PR #1963. I didn't finished my work on it due to a shift of priorities in my project, but I think it is almost complete and ready to use.

I don't have a repo for my build environment, but here's the command that I used to build my library:

python3 -m pythonforandroid.toolchain apk \
    --debug \
    --bootstrap=library \
    --local-recipes=recipes \
    --requirements=python3,pyjnius,mypackage \
    --dist_name mypackage \
    --name "mypackage" \
    --version 0.1 \
    --package mypackage \
    --android_api 28 \
    --minsdk 21 \
    --ndk-api 21 \
    --private .buildozer/android/app \
    --service mypackage:service.py \
    --copy-libs \
    --arch x86 \
    --color=always \
    --storage-dir="/vagrant/.buildozer/android/platform/build" \
    --ndk-api=21 \
    --ndk-dir="/home/vagrant/.buildozer/android/platform/android-ndk-r17c/" \
    --sdk-dir="/home/vagrant/.buildozer/android/platform/android-sdk/" \
    --blacklist-requirements=sqlite3,libffi,android,openssl

I tried to build .aar file using this but I am getting following error
[ERROR]: unsupported bootstrap for android recipe: library

Please help me solve this issue. I install p4a from your forked repo itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants