Sunday, November 24, 2013

Pygame on Android App Crashes on Start: FIXED

A good tileset in the wrong place at the wrong time
After catching Jack smelt in Capitola and seeing an amazing display of seals, dolphins, and school of tens of thousands of fish, my son worked on his 2D tile map game, Maze.  The Android app consistently crashed on start.  It would display the initial splash screen and then die, leaving no error message.

This has happened in the past with other games.  We usually solve the problem with adb, the Android debugger.  Today, the fix was easy.  However, since I know that it can be daunting to wade through the adb logcat, I'll provide a few tips specific to Python / Pygame Subset for Android debugging.

My son's game uses Python and pgs4a.  He also uses Tiled to export JSON map files. 

First, find adb on your system.  It's not something that python developers normally use from the command line.  adb is in the platform-tools sub-directory of the android-sdk main SDK directory.

 
 From the Linux command line, you can either just run adb logcat or you can run it through grep and look for references to python.

 

 A larger snippet of the log file is shown below.


 In this case, the error was an incorrect reference to the tileset graphic.
  

The error is caused by a relative reference to a non-existent file.  Although the program works on the desktop, it doesn't work on the phone.  The fix is to place the tileset files in the same directory or sub-directory of your main.py program and create a new map and tileset with tiled.  If you've already created an extensive map, you can also edit the json file with a text editor to have it reference the correct location.

In this example, the file is called grass_water_terrain.png.

In a previous game, my son had a similar problem with the app crashing when it tried to load a font file that was in the wrong location.  Fonts and graphics are the two most common problems.  If you have all your fonts and graphic files in the same directory and your program still doesn't work on Android, but works on your desktop, you may be trying to import a library another directory.  Everything needs to be in the directory where main.py is or in a sub-directory.

If the application works on your desktop and it builds and then loads onto your phone fine, but crashes on start, think through what files the application is trying to load on the Android phone.  Then, work backwards and think through the differences between the phone directory structure and your desktop.  Also, think about the obvious things.  Check that you imported the android library and you've initialized it android.init().

If the problem is not immediately obvious, fire up adb and look for references to python.

Checklist:

  • import android library
  • initialize android library
  • main game file is called, main.py
  • graphic files are in the main or sub-directory
  • font files are in the main or sub-directory
  • custom libraries that you wrote are in the main or sub-directory

Update: March 3, 2014
One of the viewers of my pychildren YouTube channel had a problem with sound.  The important section is to map pygame.mixer and android.mixer to mixer.


try:    import pygame.mixer as mixer except ImportError:    import android.mixer as mixer

The other problem with sound is that there may be problems with different types of sound formats.  I suggest you use .wav files.  Since sound is handled differently on Android and the desktop, a few problems occur.



3 comments:

  1. Hello,

    My app is developed using pygame subset for android and its crashing(at startup only with a black screen) on android emulator and I tried to debug it using adb logcat and output is as follow. Could please me what should be the problem...Thankyou :)

    I/ActivityManager( 59): Starting activity: Intent { act=android.intent.action.
    MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.ScintillatorG
    ames.blogspot/org.renpy.android.PythonActivity }
    I/ActivityManager( 59): Start proc com.ScintillatorGames.blogspot:python for a
    ctivity com.ScintillatorGames.blogspot/org.renpy.android.PythonActivity: pid=359
    uid=10036 gids={1015}
    W/ResourceType( 359): No package identifier when getting value for resource num
    ber 0x00000000
    V/python ( 359): Extracting private assets.
    D/dalvikvm( 359): GC_FOR_MALLOC freed 846 objects / 61760 bytes in 93ms
    I/dalvikvm-heap( 359): Grow heap (frag case) to 4.132MB for 1048592-byte alloca
    tion
    D/dalvikvm( 359): GC_FOR_MALLOC freed 203 objects / 10744 bytes in 42ms
    I/python ( 359): extracting private.mp3 to /data/data/com.ScintillatorGames.bl
    ogspot/files
    I/SDLSurface( 359): surfaceCreated() is not handled :|
    I/ActivityManager( 59): Displayed activity com.ScintillatorGames.blogspot/org.
    renpy.android.PythonActivity: 618 ms (total 618 ms)
    D/libEGL ( 359): egl.cfg not found, using default config
    D/libEGL ( 359): loaded /system/lib/egl/libGLES_android.so
    W/SDLSurface( 359): Choose egl configuration
    I/SDLSurface( 359): Try to use graphics config R8G8B8A8S8
    E/SDLSurface( 359): Unable to find a correct surface for this device !
    I/AndroidRuntime( 359): AndroidRuntime onExit calling exit(0)
    I/WindowManager( 59): WIN DEATH: Window{4a245848 SurfaceView paused=false}
    I/WindowManager( 59): WIN DEATH: Window{4a210018 com.ScintillatorGames.blogspo
    t/org.renpy.android.PythonActivity paused=false}
    I/ActivityManager( 59): Process com.ScintillatorGames.blogspot:python (pid 359
    ) has died.
    I/UsageStats( 59): Unexpected resume of com.android.launcher while already res
    umed in com.ScintillatorGames.blogspot
    W/InputManagerService( 59): Got RemoteException sending setActive(false) notif
    ication to pid 359 uid 10036

    ReplyDelete
  2. hi i am having problem pygame.font
    whenever i use it the app crashes at brgining but works fine on pc
    can u tell me how to add fonts in detail
    and which files to load in game folder for fonts

    ReplyDelete
  3. I’m really impressed with your blog article, such great & useful knowledge you mentioned here
    mobile game developers

    ReplyDelete