Repository: kivy/python-for-android

Issue:
Incorrect SDK variable in build.xml with pygame bootstrap and direct p4a invocation
This issue is a follow-up to Issue #681 (opened and closed two days ago).  Please see that for additional context (ie, install environment, dependency resolution, etc). I'm attempting still to manually create an apk by directly invoking p4a as follows:

  p4a apk --sdk_dir /opt/android-sdk-linux \
          --ndk_dir /opt/android-ndk-r10e \
          --android_api 15 --ndk_ver r10e-rc4 \
          --private /root/HelloWorld \
          --package=org.example.helloworld \
          --name="HelloWorld" --version=0.5 \
          --bootstrap=pygame --requirements=sdl,python2 \
          --dist_name=helloworld

P4a (or, more specifically, python-sh) is now properly resolving "ant"; however, ant is failing due to an error in the build.xml file:

  Buildfile: /root/.local/share/python-for-android/dists/helloworld/build.xml   

  BUILD FAILED  
  /root/.local/share/python-for-android/dists/helloworld/build.xml:18: Cannot find /opt/android-sdk/tools/ant/build.xml imported from /root/.local/share/python-for-android/dists/helloworld/build.xml 

Specifically, my dist's build.xml seems to be trying to import the Android SDK's build.xml from the wrong fully canonicalized file location.  Above, note that it looks for it in:

  /opt/android-sdk/tools/ant/build.xml

However, the p4a invocation clearly passes the SDK directory parameter as:

  /opt/android-sdk-linux

Somehow, p4a is going from "/opt/android-sdk-linux" to "/opt/android-sdk" and thus failing to find the file.

After some digging, I note that this is really due to use of the pygame bootstrap; more specifically, "/root/.local/share/python-for-android/dists/helloworld/build.xml" includes "/root/.local/share/python-for-android/dists/helloworld/local.properties", which itself contains the incorrect SDK directory parameter ("/opt/android-sdk", rather than "/opt/android-sdk-linux").

If we look at "/usr/local/lib/python2.7/dist-packages/pythonforandroid/bootstraps/pygame/**init**.py", we see this code snippet:

  shprint(sh.cp, '-a', join(src_path, 'local.properties'), '.')

So, the pygame bootstrap is simply copying the default "local.properties" (as in the git repo for python-for-android) into my particular dist.  I'm assuming that the intention here is that the "local.properties" file is supposed to be a fallback default location for SDK directory, and that my command line parameter ("--sdk_dir /opt/android-sdk-linux" above) at p4a invocation should supersede this default.  Clearly, that's not happening.

In fact, though, if I manually change the "local.properties" file to match the correct SDK directory, everything works fine.  Also, this bug is NOT triggered with the SDL2 bootstrap (though that triggers yet another bug), and it also is NOT triggered when the toolchain is invoked by buildozer (with "android_new" target).  Still, there's a bug somewhere here (even if only a documentation / dependency bug), and I'd like to resolve it, so things work properly with p4a invoked directly.

Thanks!



Discussion:
I encountered the exact same issue when trying pygame when I was blocked by #686.

My workaround was to create a symlink from /opt/android-sdk to /opt/android-sdk-linux and that works.


Resolving patch:
diff --git a/pythonforandroid/bootstraps/pygame/__init__.py b/pythonforandroid/bootstraps/pygame/__init__.py
--- a/pythonforandroid/bootstraps/pygame/__init__.py
+++ b/pythonforandroid/bootstraps/pygame/__init__.py
@@ -33,7 +33,6 @@ def run_distribute(self):
 
             info('Copying default files')
             shprint(sh.cp, '-a', join(self.build_dir, 'project.properties'), '.')
-            shprint(sh.cp, '-a', join(src_path, 'local.properties'), '.')
             shprint(sh.cp, '-a', join(src_path, 'build.py'), '.')
             shprint(sh.cp, '-a', join(src_path, 'buildlib'), '.')
             shprint(sh.cp, '-a', join(src_path, 'src'), '.')
@@ -42,6 +41,9 @@ def run_distribute(self):
             shprint(sh.cp, '-a', join(src_path, 'blacklist.txt'), '.')
             shprint(sh.cp, '-a', join(src_path, 'whitelist.txt'), '.')
 
+            with open('local.properties', 'w') as fileh:
+                fileh.write('sdk.dir={}'.format(self.ctx.sdk_dir))
+
             info('Copying python distribution')
             hostpython = sh.Command(self.ctx.hostpython)
             # AND: This *doesn't* need to be in arm env?


Test patch: