Python: How To Integrate Firebase with KIVY Android App By Using Pyrebase And Buildozer

pyrebase4_firebase_python_kivy_buildozer

Developers seeking to develop modern, touch-sensitive applications will love KIVY, the Python framework for cross-platform application development. By using Firebase, a platform for backend services akin to Pyrebase, you can provide your Kivy apps with robust functionalities such as real-time databases, authentication, and cloud storage.

Benefits of Kivy and Firebase?
  • Kivy:
    • Write once, run on any OS: Android, iOS, Windows, macOS, and Linux.
    • Supports touch gestures and dynamic UIs.
    • Python-based, making it accessible to developers familiar with Python.
  • Firebase:
    • Real-time database for instant synchronization.
    • Authentication services for login (email, Google, Facebook, etc.).
    • Cloud storage and functions for scalability.

Together, Kivy and Firebase create a powerful combination for mobile app development.

What Is Pyrebase4?

Pyrebase4 is a fork of the Pyrebase library designed to work seamlessly with Python 3.x and Firebase’s REST API. It provides an easy way to interact with Firebase services like:

  • Authentication
  • Database
  • Storage
  • Cloud Messaging

Setting Up Your Environment

1. Required Tools

Before starting, ensure you have right tools installed:

  • Python3.x.x installed.
  • Kivy installed:
  • Buildozer for creating Android APKs:
  • Pyrebase4 by using pip

2. Set Up Firebase

  1. Go to the Firebase Console.
  2. Create a new project.
  3. Add an Web or Android app to the project accordingly to your need and download the google-services.json file.
  4. Enable the desire services like Authentication and Realtime Database as needed.

Building a Kivy App with Firebase Integration

Project Structure

Your project directory should look something like this make sure you added correct paths in python code:

my_kivy_app/
├-- main.py
├-- buildozer.spec
├-─ google-services.json
Code Example

1. Configure Firebase with Pyrebase library:

Create a file firebase_config.py or you can add this code in your main.py file:

import pyrebase

firebase_config = {
    "apiKey": "YOUR_API_KEY",
    "authDomain": "YOUR_PROJECT_ID.firebaseapp.com",
    "databaseURL": "https://YOUR_PROJECT_ID.firebaseio.com",
    "storageBucket": "YOUR_PROJECT_ID.appspot.com",
    "serviceAccount": "google-services.json"
    "appId": "Your_Firebase_App_ID"
}

firebase = pyrebase.initialize_app(firebase_config)
auth = firebase.auth()
db = firebase.database()

Replace the “firebase_config” info with the values from your Firebase project.

With the above configurations if you need only for desktop app firebase services with python kivy you are good to go.

Add Firebase setting to Buildozer

Update the buildozer.spec file:

  1. Add dependencies:
requirements = kivy, pyrebase4, requests

Normally on systems “pip install pyrebase4” install all the required libraries that pyrebase need to work properly but on android with buildozer mostly developer face error such as missing Module  because somehow not installing correct libraries based on python3.x.x version or due some other issues. if you are face this issue, use these requirements in your buildozer.spec file.

pyrebase4_firebase_python_kivy_buildozer
requirements = python3,kivy,urllib3==1.26.20, requests-toolbelt==0.10.1,pyrebase4, gcloud, googleapis-common-protos, protobuf, httplib2, pyparsing, oauth2client, pyasn1, pyasn1-modules, rsa, pycryptodome, python-jwt, jws, requests, certifi, charset-normalizer==2.1.1, idna, jwcrypto, cryptography, deprecated, wrapt, typing-extensions

The above requirements will successfully install all the pyrebase4 supported libraries.

  1. Include google-services.json:
source.include_exts = py,png,jpg,kv,atlas,json
  1. Add permissions for internet access:

Add permissions that your app need also the internet permission.

android.permissions = INTERNET

Build the APK

Now build the app by using Buildozer command. If you need help with how to install Buildozer or use it click here.

After the build process completes, you’ll find the APK in the bin/ directory.


Running the App

Install the APK on your Android device and test the login functionality. If everything is set up correctly, you’ll be able to log in with Firebase Authentication and interact with the Realtime Database.


Extending the App

With Firebase integrated, you can expand your app to include features like:

  • User Registration: Add a sign-up screen to allow new users to register.
  • Database Operations: Use the db object to read and write real-time data.
  • Cloud Storage: Enable file uploads (e.g., images, PDFs).

Conclusion

Combining Kivy and Firebase using Pyrebase4 is a powerful way to create feature-rich Android apps using Python. Whether you’re building a personal project or a scalable mobile app, this integration provides flexibility and robust backend support.

Leave a Comment

Your email address will not be published. Required fields are marked *