how to apply the private recipe in python-for-android project

328 views
Skip to first unread message

JUAN CHEN

unread,
Nov 8, 2021, 10:28:32 PM11/8/21
to Kivy users support
Hi, I am new to python for android tool and kivy. Right now I have a project to try to run python and c++ code on android via p4a tool. After reading the doc, I still don't understand how to apply p4a tool especially not know how to apply private recipe. 
For example, I have a folder ~/test. The structure inside is like the following:
~/test
     main.py
     p4a-recipes
          rectangle
               __init__.py
              src
                  setup.py
                  rect
                     Rectangle.cpp
                     Rectangle.h
                     rect.pyx

My main.py is very simple, the snippet is : 
import rectangle as r
r.PyRectangle(0,0,10,10)
print("Hello, World!")

The code inside __init__.py is :

from pythonforandroid.recipe import CythonRecipe, IncludedFilesBehaviour
from pythonforandroid.util import current_directory
from pythonforandroid import logger

from os.path import join


class RectangleRecipe(IncludedFilesBehaviour, CythonRecipe):
    name = 'rectangle'
    version = None
    url = None

    src_filename = 'src'
    depends = ['sdl2', 'pyjnius']

    def get_recipe_env(self, arch):
        env = super().get_recipe_env(arch)
        # Manipulate the env here if you want
        return env

    def should_build(self, arch):
        # Add a check for whether the recipe is already built if you
        # want, and return False if it is.
        return True

    def prebuild_arch(self, arch):
        super().prebuild_arch(self)

    def build_arch(self, arch):
        super().build_arch(self)
       

    def postbuild_arch(self, arch):
        super().prebuild_arch(self)
        # Do anything you want after the build, e.g. deleting
        # unnecessary files such as documentation

recipe = RectangleRecipe()

The setup.py is :
from distutils.core import setup
from distutils.extension import Extension

extensions = [Extension('rectangle', ['./rect/Rectangle.cpp'])]

setup(name = "rectangle",packages=['rect'],
      package_dir={'rect': 'rect'},
      ext_modules= extensions)

The rect.pyx is :

# distutils: language = c++
# distutils: sources = Rectangle.cpp

cdef extern from "Rectangle.h" namespace "shapes":
    cdef cppclass Rectangle:
        Rectangle(int, int, int, int) except +
        int x0, y0, x1, y1
        int getLength()
        int getHeight()
        int getArea()
        void move(int, int)

cdef class PyRectangle:
    cdef Rectangle *thisptr      # hold a C++ instance which we're wrapping
    def __cinit__(self, int x0, int y0, int x1, int y1):
        self.thisptr = new Rectangle(x0, y0, x1, y1)
    def __dealloc__(self):
        del self.thisptr
    def getLength(self):
        return self.thisptr.getLength()
    def getHeight(self):
        return self.thisptr.getHeight()
    def getArea(self):
        return self.thisptr.getArea()
    def move(self, dx, dy):
        self.thisptr.move(dx, dy)


So if I run the command:
p4a apk --private $HOME/test --package=org.example.myapp --name "MyFirstKiviApplication" --version 0.1 --bootstrap=sdl2 --requirements=python3,kivy,rectangle

there is the error:
ERROR: Could not find a version that satisfies the requirement rectangle (from versions: none)
ERROR: No matching distribution found for rectangle

So I am so confused how to apply this private recipe to the apk. Would anyone help me on this issue? Thank you very much in advance. 







Robert

unread,
Nov 9, 2021, 1:30:58 AM11/9/21
to Kivy users support
1) Your Kivy app is not a Python script, it needs to look like this https://kivy.org/doc/stable/guide/basic.html#kivy-app-life-cycle
Build and run Hello World first before trying anything else.
Really, it helps.

2) Recipes are build time scripts executed by p4a and separate from your app. They are specified with a build option.
The result of that execution ends up in site-packages on the Android device, and is imported by your app at run time like any package.
Start by building an existing recipe locally, again you'll get to see how the parts fit together.

John Brown

unread,
Nov 14, 2021, 6:55:33 AM11/14/21
to Kivy users support
i tested and it works
Reply all
Reply to author
Forward
0 new messages