Search code examples
pythonandroidcythoncythonizechaquopy

How to cythonize a python script for Android using Chaquopy?


My target is to cythonize/obfuscate a python script using Cython for Android platform.

What I have tried till now is: I have a setup.py

from setuptools import setup
from Cython.Build import cythonize
import os
print(os.getcwd())
os.chdir('/sdcard/Download')
print(os.getcwd())

setup(
    ext_modules=cythonize('test1.py')
)

Here I am changing the dir to /sdcard/Download/ since the python script I want to obfuscate is in that dir.

  • Case 1:

Inside MainActivity.java am calling the setup.py like:

Python py = Python.getInstance();
PyObject os= py.getModule("setup");

This way it throws an error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.styleapp, PID: 2422
    com.chaquo.python.PyException: SystemExit: usage:  [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
       or:  --help [cmd1 cmd2 ...]
       or:  --help-commands
       or:  cmd --help

    error: no commands supplied
        at <python>.distutils.core.setup(core.py:136)
        at <python>.setuptools.setup(__init__.py:161)
        at <python>.setup.<module>(setup.py:8)
        at <python>.importlib._bootstrap._call_with_frames_removed(<frozen importlib._bootstrap>:219)
        at <python>.importlib._bootstrap_external.exec_module(<frozen importlib._bootstrap_external>:783)
        at <python>.java.android.importer.exec_module(importer.py:503)
        at <python>.java.android.importer.exec_module(importer.py:546)
        at <python>.importlib._bootstrap._load_unlocked(<frozen importlib._bootstrap>:671)
        at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:975)
        at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:991)
        at <python>.importlib._bootstrap._gcd_import(<frozen importlib._bootstrap>:1014)
        at <python>.importlib.import_module(__init__.py:127)
        at <python>.chaquopy_java.Java_com_chaquo_python_Python_getModule(chaquopy_java.pyx:152)
        at com.chaquo.python.Python.getModule(Native Method)
        at com.example.styleapp.MainActivity.runthemodel(MainActivity.java:127)
        at com.example.styleapp.MainActivity.access$000(MainActivity.java:47)
        at com.example.styleapp.MainActivity$2.onMenuItemClick(MainActivity.java:103)
        at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:154)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:981)
        at androidx.appcompat.widget.ActionMenuView.invokeItem(ActionMenuView.java:625)
        at androidx.appcompat.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:151)
        at android.view.View.performClick(View.java:7140)
        at android.view.View.performClickInternal(View.java:7117)
        at android.view.View.access$3500(View.java:801)
        at android.view.View$PerformClick.run(View.java:27355)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7410)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)

I understand this is because am not passing any arguments like I should:

python setup.py build_ext -b /sdcard/Download/ -t .
  • Case 2:

If i use this:

PyObject os= py.getModule("setup.py build_ext -b /sdcard/Download/ -t .");

I get the same error.

I need help to understand how to pass on these args?

I tried to replicate:

import os
os.system('python3 setup.py build_ext -b "/sdcard/Download/" -t .')

as

PyObject os = py.getModule("os");
os.callAttr("system","python3 setup.py build_ext -b /sdcard/Download/ -t .");

But that is not even calling the setup.py script.


Solution

  • The reason I was not able to compile a Cython module on an actual Android device, because most devices don't come with a compiler. I guess we can compile py files into pyc since they are not machine dependant.