Python-for-android: Multiple issues with latest Android SDK

Created on 29 Dec 2017  路  14Comments  路  Source: kivy/python-for-android

I tried to compile an APK using python-for-android for the first time, using the Kivy Virtual Machine.

I have updated python-for-android to the latest github version (commit 8829312fd187121384939627ec09354657821ae9) to avoid the "Invalid or unsupported command list" error.

Since then, I faced two additional issues:

  1. Build fail at ant call because android-sdk/tools/ant/build.xml does not exist anymore in latest releases of Android SDK. The only workaround I have found was to install an older SDK version prior to this change, such as android-sdk-r25.2.5. See here, here and here.

Example error:

BUILD FAILED    
/home/kivy/.local/share/python-for-android/dists/unnamed_dist_2/build.xml:93: Cannot find /home/kivy/android/android-sdk/tools/ant/build.xml imported from /home/kivy/.local/share/python-for-android/dists/unnamed_dist_2/build.xml    
  1. The build of Java classes is done with enforcement of v1.5 (-source 1.5), but kivy/android/PythonService.java includes a multi-catch statement that works only from v1.7 onwards.

Example error:

[INFO]:    STDOUT (last 20 lines of 79):                                       

-pre-compile:   

-compile:   
    [javac] Compiling 22 source files to /home/kivy/.local/share/python-for-android/dists/unnamed_dist_1/bin/classes    
    [javac] warning: [options] source value 1.5 is obsolete and will be removed in a future release 
    [javac] warning: [options] target value 1.5 is obsolete and will be removed in a future release 
    [javac] warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.    
    [javac] /home/kivy/.local/share/python-for-android/dists/unnamed_dist_1/tmp-src/org/kivy/android/PythonService.java:107: error: multi-catch statement is not supported in -source 1.5   
    [javac]             } catch (NoSuchMethodException | IllegalAccessException |   
    [javac]                                            ^    
    [javac]   (use -source 7 or higher to enable multi-catch statement) 
    [javac] 1 error 
    [javac] 3 warnings  

BUILD FAILED    
/home/kivy/android/android-sdk-r25.2.5/tools/ant/build.xml:716: The following error occurred while executing this line: 
/home/kivy/android/android-sdk-r25.2.5/tools/ant/build.xml:730: Compile failed; see the compiler error output for details.  

I'm stuck at this point, as I could not locate where the v1.5 enforcement takes place.

Most helpful comment

@defacto133 Thanks a lot! I did not think to look inside ant/build.xml !

I fixed it by replacing:

<property name="java.target" value="1.5" />
<property name="java.source" value="1.5" />

by:

<property name="java.target" value="7" />
<property name="java.source" value="7" />

And now it works! I got no additional error, the APK was compiled alright!

However, it works only with --bootstrap=webview, if I try to use pygame or sdl2 I get the errors described here: https://github.com/kivy/python-for-android/issues/1191#issuecomment-354580017

Would it be possible to fix these? Why are these assets required even though I don't use them?

All 14 comments

Also when trying to compile a simple Hello World application using Kivy and python-for-android, here is the error I get (whenever I use SDL2 as a backend):

...
Compiling /home/kivy/.local/share/python-for-android/dists/unnamed_dist_3/private/lib/python2.7/xml/sax/xmlreader.py ...
Traceback (most recent call last):
  File "/usr/local/bin/p4a", line 11, in <module>
    load_entry_point('python-for-android==0.6.0', 'console_scripts', 'p4a')()
  File "/usr/local/lib/python2.7/dist-packages/pythonforandroid/toolchain.py", line 975, in main
    ToolchainCL()
  File "/usr/local/lib/python2.7/dist-packages/pythonforandroid/toolchain.py", line 512, in __init__
    getattr(self, args.subparser_name.replace('-', '_'))(args)
  File "/usr/local/lib/python2.7/dist-packages/pythonforandroid/toolchain.py", line 150, in wrapper_func
    func(self, args)
  File "/usr/local/lib/python2.7/dist-packages/pythonforandroid/toolchain.py", line 754, in apk
    build_args = build.parse_args(args.unknown_args)
  File "/home/kivy/.local/share/python-for-android/dists/unnamed_dist_3/build.py", line 567, in parse_args
    make_package(args)
  File "/home/kivy/.local/share/python-for-android/dists/unnamed_dist_3/build.py", line 248, in make_package
    make_tar('src/main/assets/private.mp3', tar_dirs, args.ignore_path)
  File "/home/kivy/.local/share/python-for-android/dists/unnamed_dist_3/build.py", line 187, in make_tar
    tf = tarfile.open(tfn, 'w:gz', format=tarfile.USTAR_FORMAT)
  File "/usr/lib/python2.7/tarfile.py", line 1693, in open
    return func(name, filemode, fileobj, **kwargs)
  File "/usr/lib/python2.7/tarfile.py", line 1740, in gzopen
    fileobj = gzip.GzipFile(name, mode, compresslevel, fileobj)
  File "/usr/lib/python2.7/gzip.py", line 94, in __init__
    fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
IOError: [Errno 2] No such file or directory: 'src/main/assets/private.mp3'

For the 2nd problem, just look in <android-sdk>\tools\ant\build.xml, see this.

For your last problem, as a workaround, just create the directory and the file with touch, then there's should be another problem with a icon.png that you can solve the same way.

@defacto133 Thanks a lot! I did not think to look inside ant/build.xml !

I fixed it by replacing:

<property name="java.target" value="1.5" />
<property name="java.source" value="1.5" />

by:

<property name="java.target" value="7" />
<property name="java.source" value="7" />

And now it works! I got no additional error, the APK was compiled alright!

However, it works only with --bootstrap=webview, if I try to use pygame or sdl2 I get the errors described here: https://github.com/kivy/python-for-android/issues/1191#issuecomment-354580017

Would it be possible to fix these? Why are these assets required even though I don't use them?

I'm not sure what's goign on with this assets directory error, I haven't been able to reproduce it. I did just push a partial fix for the java version issue with gradle, though, and I'll look to add your ant fix as well - thanks for that.

I have also had the issue with the assets directory and the icon file. Supposedly this was fixed by a recent pull request 1204, but following the instruction to install directly from here (pip install git+https://github.com/kivy/python-for-android.git) did not fix the problem for me.

@Rybec I did not fix these issues, I only compiled with webview, which does not require these assets. Apparently it should be fixed by #1204 and #1205 but I did not test them out yet.

It's weird it's difficult to reproduce as I simply used the Kivy Virtual Machine using the latest version of VirtualBox, normally this should be a pretty reproducible environment.

No, according to #1204 QuadTriangle fixed it and inclement merged the fix commit into master.

I'll give some background that I probably should have included before, since this is so hard to reproduce.

The command I used to compile is this:
p4a apk --private ./ --package=org.blg.pytest --name pytest --version 0.1 --bootstrap=sdl2 --requirements=pysdl2 --sdk_dir $(ANDROIDSDK) --ndk_dir $(ANDROIDNDK) --android_api $(ANDROIDAPI) --ndk_version $(ANDROIDNDKVER) --arch=$(TARGET)

My program is in main.py, in the project root where this command is executed. I also had a graphic file in the project, but it does not get included in the APK (see #1233).

ANDROIDAPI is 21, ANDROIDNDKVER is r16b, and TARGET is armeabi-v7a.

I hope this helps.

Ok, one more critical piece of information: I just got past another issue that was causing build problems, but now my APK files have a totally different name. I just noticed that it is using Gradle now. Before, it was using Ant. I did not have Gradle installed (and to be honest, I am not sure what I did that installed it, unless it is included with texlive and I just didn't notice it in massive list of dependencies I installed).

Anyhow, now that it is using Gradle, this issue is not happening anymore. It _did_ happen when Ant was used during the build. Perhaps this is why it is hard to reproduce!

@Rybec how did you fix this private.mp3 problem?

@feelfreelinux It wasn't a problem. It was just unclear how it worked. I will explain:

private.mp3 is actually a zip file, but for some reason Android does not like to package or allow access to certain file types (I forget the details...). Renaming it to .mp3 makes Android think it is just a media asset, so it plays nice with it. This is why p4a uses private.mp3.

This zip file is where all assets, some bits of the Python interpreter, and other things are stored, by python-for-android. This means that assets for your projects won't be found where you normally expect them. Instead, you will find them in this zip file. If you want to verify that a particular file is there, extract private.mp3 from the APK, rename it to private.zip, and open it. You should be able to browse it freely from there.

@rybec I do not understand. I get exactly the same error about the missing "private.mp3". I've already asked on https://groups.google.com/forum/#!forum/kivy-users. There are two or three other people affected by the issue. What are we doing wrong? Where should we look for what, and why?

That's a separate issue from what I have been having. I did end up with it too though. I forget exactly what the solution was, but I think you just have to put an empty private.mp3 file somewhere. I believe it needs to go in the assets folder of the directory Kivy makes to compile your project into, but I don't recall what that directory was called. Basically, when you tell python-for-android to compile your project, it will create a directory where everything goes, and then it generates the APK from that. For some reason, when python-for-android tries to start putting stuff in private.mp3 (I think...), it just assumes the file is already there, and if it is not, it fails. The solution is to create the file, but leave it empty. I don't know where this directory is anymore though (and I was doing this on Debian, so if you are using OSX or Windows, it is probably in a different location anyway).

Sorry I cannot provide more information, but maybe this will be enough to get you headed in the right direction.

Thank you for trying to help. Is there anyone who had success in using kivy with python-for-android. And when so which way did you do it? Which operationg system, which version of installed packages, which command to build/install the "hello world" py program?

Closing as stale, everything seems to be working fine right now.

Was this page helpful?
0 / 5 - 0 ratings