So, we have a complete, working game. We want to port it over to Android. But How? Well, I could rewrite it explicitly in Java, but that seems rather senseless. Instead, I can use a subset of Pygame, get it running, and install it on my phone (an HTC myTouch 4G). Luckily, such a thing already exist - dubbed psg4a - and seems have an active community. The homepage can be found here and the documentation here.
The documentation includes a good deal of information about what to install to get started. For instance, I used:
Ref: https://sites.google.com/site/dustinprinehart/current-projects/float-i-pop/proof-of-concept
The documentation includes a good deal of information about what to install to get started. For instance, I used:
- The current Pygame Subset: pgs4a-0.9.4.zip and unzipped it to an arbitrary directory (Drive leter:\pgs4a for example).
- The Java Development Kit: link to Download page here. I used the newest (at the time), version 7 Update 10.
- Python, of course (installed prior).
- Android OEM USB Drivers for Android phone (in order to connect your mobile phone and develop/test directly without using an emulator). Link here.
- Optionally, I installed the HTC-specific drivers for my phone. I found the package here.
Now, to get down to business.
- Check your pgs4a install by running
android.py test
via the command line (reference here). If successful, you should see the following:
- Now follow along with the steps and begin constructing the sample game. Doing so aided me in fixing my installation as I ran into numerous issues (admittedly, some were my fault while others were just hiccups).
- Notice that once you create the .py file and launch it, the "game" we run successfully.
- Once you get here, and type in either the release or install commands (an aside: typing in
android.py build mygame release
builds the signed and unsigned .apks whileandroid.py build mygame release install
does the former and installs it on the connected device or emulator) you will run into a dreaded error. It will differ, but will end with:Buildfile: build.xml does not exist!
Build failed
- Luckily, all of these problems you will encounter are resolved via a couple of fairly easy fixes. It just takes forever and involves a little finagling with the System Properties.
To get started, first some background: or what is a path variable? When working with Windows there exist an editable/configurable data element known as a PATH environment variable. To familiarize yourself with it, open another command line window and type inecho %PATH%
. You will see a long string of directories separated by semicolons. What does this mean? This is where and how we specify/define the paths in which the executables we want to run, do so within our Windows environment. For instance, for other programs to use Apache Ant (used to build the Java applications associated with Android) - or just ant via the command line, they need to know where it is. We can edit the path to specify that information (do not fret, the procedure below will walk you through the process). But, where is our executable to add to our path?Well...when we installed the Android SDK via android.py it was installed in the pgs4a directory. Specifically, the executables would be found in the bin folder (for instance, D:\pgs4a-0.9.4\apache-ant\bin for me). The executable we need is ant.bat. However, if you were to run ant (double-clicking or command line), you would get a message similar to this:D:\pgs4a-0.9.4\apache-ant\bin>ant
Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre7\lib\tools.jar
Buildfile: build.xml does not exist!
Build failed
So, what can we do? We can fix our PATH variable! To do it, we will need to follow a few steps:- Find the directory you installed the JDK in. We need the path to the java.exe. This is usually found Drive Letter:\Program Files\Java\jdk1.7.0_10\bin.
- Navigate to My Computer -> Click the Advanced Tab -> Click Environment Variables -> Under System variables look for Path -> Click Edit -> At the end of the string insert a semicolon.
- Now paste your directory found in Step #1 directly after the ; (so my Path would be longlistofstring;C:\Program Files\Java\jdk1.7.0_10\bin). To test, open up another command line window and type in javac. This is the Java compiler. You should see a list of options if successful. If you are not you will see:
'javac' is not recognized as an internal or external command,
operable program or batch file.
- Now again follow Step #2 and add the directory for ant remembering to add the semicolon before the directory (again my Path would look likelonglistofstrings;C:\Program Files\Java\jdk1.7.0_10\bin;D:\pgs4a-0.9.4\apache-ant\bin).
- Note, if you have more than one Java instance installed, you may need to uninstall them as usually the SE java executes by default. This the root cause of the 1stmessage stated in the error above (Unable to locate tools.jar...). Yet, we still get the build.xml error! What gives!?!
- When we created our new directory we intended it to be an Android project. However, we actually have not setup the Android environment yet. Fortunately, we downloaded the tools when we unpacked the sdk (see here). To test the android tool, in the command prompt simply type in android and press Enter. If you see:
'android' is not recognized as an internal or external command,
operable program or batch file.
Then, we need to...you guessed it! Append it to our Path variable (the last time, I promise). So, my final Path variable would be: longlistofstrings;C:\Program Files\Java\jdk1.7.0_10\bin;D:\pgs4a-0.9.4\apache-ant\bin;D:\pgs4a-0.9.4\android-sdk\tools. Again, Click OK to close both the Environment Variableswindow and the System Properties window, respectively. Lastly, close your command line window and open a new one. At this point we are done editing our Path variable, but, we have a few steps to finish before we have a working build.
- Try the android command again. This will open the Android SDK Manager. Launching said manager will search and populate all available updates for the SDK tool set (Under the Tools folder). It is highly recommended you download them (rev. 20 as of this tutorial). I installed the Android SDK Platform-tools additionally (but it is purely optional). Click Install x (x = number of package(s)) package(s)... Accept the License and click Install. Wait patiently. Once the install concludes you may be prompted with a prompt; click OK. Close window.
- Instantly, rerun the android command yet again in the command prompt. You should now see all the available Android APIs and subsequent SDK updates (further revisions or iterations on top of the major releases; I initially had SDK rev. 16 installed for an earlier project so your experience may differ). Choose your API of choice. My (older) phone runs API 10 (2.3.3) so naturally, I chose it. A larger majority of retail Android phones run API 8 (2.2) so keep this in mind as Android applications are not backward compatible. They are, however, forward compatible. Note aside, you should see something like this:
Follow the previous step's instructions to install the packages (this time clicking Accept All). I did not catch it at the time, but under the Extras folder the manager had defaulted to selecting an update for the Google USB Driver. Indeed, in hindsight, it may be possible to avoid manually installing the OEM driver yourself. - Now to deal with our build.xml error...surely. Open a new command line window and type in
android list
targets
. This will list all the available devices, dubbed targets, we want to build our application for. Amongst them (likely to be several), you will see the Intel Atom image you just installed. Choose the target that represents your device. In my case, my target was 1. I then typed inandroid update project --target x --path D:\pgs4a-0.9.4\
. Note: x is 1 for me, but may differ for you (of course, path will likely differ too; I included my path just for reference).
Error: D:\pgs4a-0.9.4\mygame is not a valid project (AndroidManifest.xml not found).
- Seriously!?! No big deal. The config file you made (if you do not recall, here) produced the Android Manifest file (albeit outside your mygame directory, in the ps4a parent folder). Simply move that file inside your mygame directory and rerun the command. You will see:
Updated and renamed default.properties to project.properties
Updated local.properties
No project name specified, using Activity name 'PythonActivity'.
If you wish to change it, edit the first line of build.xml.
Added file D:\pgs4a-0.9.4\build.xml
Added file D:\pgs4a-0.9.4\proguard-project.txt
Hey! That is the build.xml file we have been looking for. Now we can run our build command.
Updating build files.
Error: Target id 'android-8' is not valid. Use 'android.bat list targets' to get the target ids.
Nope! We need to run the android command and specify that this specific Android API is installed (just for giggles navigate to the android-sdk\platforms sub-folder in the pgs4a directory and verify that android-8 is not there). Run the android command one more time. Select Android 2.2 (API 8) and start the install process; once finished, close the window, and type the build command (android.py build mygame release
) one more time. You will see walls of commands and text, ending with:
release:
install:
[echo] Installing D:\pgs4a-0.9.4\bin\mygame-1-release.apk onto default emulator or device...
[exec] pkg: /data/local/tmp/mygame-1-release.apk
[exec] Success
[exec] 1663 KB/s (2661077 bytes in 1.562s)
BUILD SUCCESSFUL
Total time: 20 seconds
It looks like the build succeeded.
You did it! The game has been built and installed on your device successfully. Now to test it! Bring up your applications and find the mygame application (it will be the default Pygame icon). Tap it and watch the Pygame splash screen appear and feel your excitement brew. Now watch as the screen flicker and the game crash! Disappointment sets in. Before you lose your patience, know that it is a simple fix. Very simple, I promise and it is one I missed when reading the sample tutorial. In other words, my bad!.- So, what the heck happened!?! Well, luckily (and very much so I might add), the Pygame Subset grants you access to the Android debug output in the form of the adb logcat command. Furthermore, it filters python output showing you exactly what happens when you run your application. To take advantage of this tool, open another command line window from the pgs4a directory and type in
android.py logcat
. Now, run the installed application again. Analyzing the output I see a mistake that took me a while to catch:I/python (27690): Private directory is /data/data/com.dr.mygame/files
I/python (27690): Argument is /data/data/com.dr.mygame/files
I/python (27690): Handing off to main.
I/python (27690): Traceback (most recent call last):
I/python (27690): File "start.pyx", line 52, in start (jni/../jni/application/src//start.c:1313)
I/python (27690): ImportError: No module named main
Ah, main! That is right! Nothing in my directory is specifically named main.py. So, when the start function tries to hand off the functionality to my game, it has no main function to call. Such a simple mistake that could have been fixed if I had carefully reading the first line here. - To fix this, I simply rename the mygame.py to main.py, right? Well, yes and no. I had an instance (as of this typing, I could not reproduce) where I had to also run configure again to update the AndroidManifest.xml, which updated the build.xml file when I ran the build command again. I also ran into one instance where building would fail to create a signed apk. To remedy this I deleted the keystore and ran configure again. This seemed to solve the issue.
- Run the application again. It works!!! Congratulations for sticking with it and uploading a "game" to your Android device. My work, however, has just begun.
- Few asides or notes:
- leave logcat running as you test your application. It is an invaluable tool.
- reference the Pygame Subset forum here. It is a great resource.
- ignore the cdrom Import error...pgs4a does.
Ref: https://sites.google.com/site/dustinprinehart/current-projects/float-i-pop/proof-of-concept
沒有留言:
張貼留言