kivy / python-for-android

Turn your Python application into an Android APK
https://python-for-android.readthedocs.io
MIT License
8.33k stars 1.84k forks source link

Termux build fails #2585

Closed rayzchen closed 2 years ago

rayzchen commented 2 years ago

Checklist

Versions

Description

I am trying to use Termux to build a PySDL2 app, but python-for-android doesn't seem to find a certain toolchain.

buildozer.spec

not used

Spec file:

not used

Commands

Fresh copy of termux Use Andronix to install Ubuntu 20.04 Command: curl https://raw.githubusercontent.com/AndronixApp/AndronixOrigin/master/repo-fix.sh > repo.sh && chmod +x repo.sh && bash repo.sh && pkg update -y && pkg install wget curl proot tar -y && wget https://raw.githubusercontent.com/AndronixApp/AndronixOrigin/master/Installer/Ubuntu20/ubuntu20.sh -O ubuntu20.sh && chmod +x ubuntu20.sh && bash ubuntu20.sh

Run these commands inside proot:

apt-get update
apt-get install vim -y
vim main.py
# Add python code

apt-get install software-properties-common -y
add-apt-repository ppa:deadsnakes/ppa -y
apt-get install python3.9 python3-pip -y
pip install python-for-android pysdl2 pysdl2-dll cython

apt-get install wget curl unzip make openjdk-8-jdk -y
wget https://raw.githubusercontent.com/kivy/python-for-android/develop/ci/makefiles/android.mk
make -f android.mk

export ANDROIDSDK=/root/.android/android-sdk
export ANDROIDNDK=/root/.android/android-ndk
export ANDROIDAPI=27
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-arm64/

dpkg --add-architecture i386
apt-get update
apt-get install -y build-essential ccache git zlib1g-dev python3 python3-dev libncurses5:i386 libstdc++6:i386 zlib1g:i386 openjdk-8-jdk unzip ant ccache autoconf libtool libssl-dev

p4a apk --private . --package=org.example.test --name "My application" --version 0.1 --bootstrap=sdl2 --requirements=python3,pysdl2 --arch arm64-v8a

Some i386 installs don't work

In current directory I have main.py:

import sdl2.ext
sdl2.ext.init()
window = sdl2.ext.Window("Hello World!", size=(640, 480))
window.show()
processor = sdl2.ext.TestEventProcessor()
processor.run(window)
sdl2.ext.quit()

Logs

root@localhost:~/p4a# p4a apk --private . --package=org.example.test --name "My application" --version 0.1 --bootstrap=sdl2 --requirements=python3,pysdl2 --arch arm64-v8a
[INFO]:    Will compile for the following archs: arm64-v8a
[INFO]:    Found Android API target in $ANDROIDAPI: 27
[INFO]:    Available Android APIs are (27)
[INFO]:    Requested API target 27 is available, continuing.
[INFO]:    Found NDK dir in $ANDROIDNDK: /root/.android/android-ndk
[INFO]:    Found NDK version 23b
[WARNING]: Maximum recommended NDK version is 19c, but newer versions may work.
[WARNING]: Newer NDKs may not be fully supported by p4a.
[WARNING]: NDK API target was not set manually, using the default of 21 = min(android-api=27, default ndk-api=21)
[INFO]:    ccache is missing, the build will not be optimized in the future.
[WARNING]: sysroot doesn't exist: /root/.android/android-ndk/toolchains/llvm/prebuilt/linux-aarch64/sysroot
[WARNING]: Could not find any toolchain for aarch64-linux-android!
Traceback (most recent call last):
  File "/usr/local/bin/p4a", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.8/dist-packages/pythonforandroid/entrypoints.py", line 18, in main
    ToolchainCL()
  File "/usr/local/lib/python3.8/dist-packages/pythonforandroid/toolchain.py", line 728, in __init__
    getattr(self, command)(args)
  File "/usr/local/lib/python3.8/dist-packages/pythonforandroid/toolchain.py", line 141, in wrapper_func
    ctx.prepare_build_environment(user_sdk_dir=self.sdk_dir,
  File "/usr/local/lib/python3.8/dist-packages/pythonforandroid/build.py", line 446, in prepare_build_environment
    self.toolchain_version = select_and_check_toolchain_version(
  File "/usr/local/lib/python3.8/dist-packages/pythonforandroid/build.py", line 90, in select_and_check_toolchain_version
    toolchain_version=toolchain_version,
UnboundLocalError: local variable 'toolchain_version' referenced before assignment
kengoon commented 2 years ago

@rayzchen How did you get to install Android SDK and NDK

rayzchen commented 2 years ago
apt-get install wget curl unzip make openjdk-8-jdk -y
wget https://raw.githubusercontent.com/kivy/python-for-android/develop/ci/makefiles/android.mk
make -f android.mk

This downloads the makefile in ci/makefiles/android.mk and runs it, which downloads both the NDK and SDK and sets them up in /root/.android/

kengoon commented 2 years ago

ok, I will try that, this is really cool

On Sat, 23 Apr 2022 at 10:54, Ray Chen @.***> wrote:

apt-get install wget curl unzip make openjdk-8-jdk -y wget https://raw.githubusercontent.com/kivy/python-for-android/develop/ci/makefiles/android.mk make -f android.mk

This downloads the makefile in ci/makefiles/android.mk and runs it, which downloads both the NDK and SDK and sets them up in /root/.android/

— Reply to this email directly, view it on GitHub https://github.com/kivy/python-for-android/issues/2585#issuecomment-1107444350, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKB42IVFL5PLVSIE55FWRSLVGPCG7ANCNFSM5T4SZZTA . You are receiving this because you commented.Message ID: @.***>

kengoon commented 2 years ago

@rayzchen just read the content of the file and I discovered that the NDK and SDK version it installed is for x86_64 bit archs and not aarch64. Which simply means p4a is not supported on android phones with armeabi-v7a and aarch64

rayzchen commented 2 years ago

Ran same steps on a GitHub codespace yet it still tries to find toolchain for "aarch64-linux-android"

platform.platform() is 'Linux-5.4.0-1074-azure-x86_64-with-glibc2.31'

rayzchen commented 2 years ago

Why does p4a say I have to specify an arch when the quick start doesn't?

misl6 commented 2 years ago

python-for-android should run on a instance that also Google supports with the build tools for Android. (Linux x86_64, macOS x86_64, macOS Apple Silicon).

This statement doesn't mean that you can't build for aarch64-linux-android if you're on a x64_64 machine as all the builds for Android are cross-compiled (I'm referring to: "just read the content of the file and I discovered that the NDK and SDK version it installed is for x86_64 bit archs and not aarch64. Which simply means p4a is not supported on android phones with armeabi-v7a and aarch64") .

@rayzchen I've seen you're using the stable version of python-for-android (the latest one available on PyPi), but you're also taking advantage of the android.mk file from develop branch: https://raw.githubusercontent.com/kivy/python-for-android/develop/ci/makefiles/android.mk, which installs the NDK required by the develop branch, not the master one, which is incompatible.

Please let me know if using wget https://raw.githubusercontent.com/kivy/python-for-android/master/ci/makefiles/android.mk instead of wget https://raw.githubusercontent.com/kivy/python-for-android/develop/ci/makefiles/android.mk fixes your issue.

rayzchen commented 2 years ago

It seems to be fixed! Thanks for the help.