Skip to content

Building an Android Library #1957

Closed

Description

@xuhcc

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.

Activity

  1. xuhcc commented on Aug 17, 2019

    @xuhcc
    Author

    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

  2. inclement commented on Aug 18, 2019

    @inclement
    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.

  3. gklasen commented on Feb 17, 2020

    @gklasen

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

  4. xuhcc commented on Feb 17, 2020

    @xuhcc
    Author

    @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
  5. gklasen commented on Feb 21, 2020

    @gklasen

    @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.

  6. xuhcc commented on Feb 21, 2020

    @xuhcc
    Author

    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'));
        });
    }
  7. gklasen commented on Feb 26, 2020

    @gklasen

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

  8. xuhcc commented on Feb 26, 2020

    @xuhcc
    Author

    Yes, you only need the service.py file

  9. abhay6694 commented on May 18, 2021

    @abhay6694

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions