Skip to content

Build FastAPI app as an Android Service #3033

Closed as not planned
Closed as not planned

Description

@fwwucn

Can I build Python FastAPI app as an Android Service?

e.g. I have a FastAPI app (see below app.py), which hosts a http server, and provides an endpoint which can be called by http request.
I want it to be run on Android device as a background Android Service, in which hosts a http server and provides an endpoint too. Thus another Android app on the same phone can call it by http request.

app.py

from fastapi import FastAPI
from fastapi.responses import Response

app = FastAPI()

@app.get('/health')
async def health() -> Response:
    '''Health check.'''
    return Response(status_code=200)

if __name__=='__main__':
    import uvicorn
    uvicorn.run(app, host='0.0.0.0', port=5000)

I can successfully build it as an AAR with p4a command:

p4a aar --private . --dist-name service --package=com.example.service --name "My Service" --version 1.0 --bootstrap=service_library --requirements=python3 --release --service=app:app.py --arch=arm64-v8a --arch=armeabi-v7a --ignore-setup-py --sdk-dir /home/user/.android/android-sdk --ndk-dir /home/user/.android/android-ndk --android-api=27

The service-release-1.0.aar was generated, and ServiceApp.class can be found in service-release-1.0.aar\classes.jar\com\example\service\. How to load ServiceApp.class and run as a HTTP server on Android device?

Or can I build it as an Android Service (APK?), which can be directly installed on Android device?

Activity

  1. github-actions commented on Dec 12, 2024

    @github-actions

    👋 @fwwucn,
    Sorry to hear you are having difficulties with Kivy's python-for-android; Kivy unites a number of different technologies, so building apps can be temperamental.
    We try to use GitHub issues only to track work for developers to do to fix bugs and add new features to python-for-android.
    However, this issue appears to be a support request. Please use our support channels to get help with the project.
    If you're having trouble installing python-for-android, please see our quickstart guide.
    If you're having trouble using python-for-android, please see our troubleshooting guide and FAQ.
    Let us know if this comment was made in error, and we'll be happy to reopen the issue.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions