Skip to content

Commit 9ca09dd

Browse files
author
Sergei Surovtsev
committed
Merge branch 'master' of github.com:cwiz/python-for-android
2 parents b8dc5dc + c9023a3 commit 9ca09dd

File tree

15 files changed

+455
-6
lines changed

15 files changed

+455
-6
lines changed

README.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ Python for android is a project to create your own Python distribution
55
including the modules you want, and create an apk including python, libs, and
66
your application.
77

8-
Website: http://python-for-android.rtfd.org/
8+
- Website: http://python-for-android.rtfd.org/
9+
- Forum: https://groups.google.com/forum/?hl=fr#!forum/python-android
10+
- Mailing list: python-android@googlegroups.com
911

1012

1113
Global overview
@@ -40,3 +42,15 @@ Global overview
4042
adb install bin/touchtracer-1.0-debug.apk
4143

4244
#. Enjoy.
45+
46+
47+
Troubleshooting
48+
---------------
49+
50+
if you get the following message:
51+
52+
Android NDK: Host 'awk' tool is outdated. Please define HOST_AWK to point to Gawk or Nawk !
53+
54+
a solution is to remove the "awk" binary in the android ndk distribution
55+
56+
rm $ANDROIDNDK/prebuilt/linux-x86/bin/awk

distribute.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ SRC_PATH="$ROOT_PATH/src"
1919
JNI_PATH="$SRC_PATH/jni"
2020
DIST_PATH="$ROOT_PATH/dist/default"
2121

22+
# Tools
23+
export LIBLINK_PATH="$BUILD_PATH/objects"
24+
export LIBLINK="$ROOT_PATH/src/tools/liblink"
25+
export BIGLINK="$ROOT_PATH/src/tools/biglink"
26+
2227
# Internals
2328
CRED="\x1b[31;01m"
2429
CBLUE="\x1b[34;01m"
@@ -80,6 +85,7 @@ function push_arm() {
8085
export OLD_RANLIB=$RANLIB
8186
export OLD_STRIP=$STRIP
8287
export OLD_MAKE=$MAKE
88+
export OLD_LD=$LD
8389

8490
# to override the default optimization, set OFLAG
8591
#export OFLAG="-Os"
@@ -121,6 +127,7 @@ function push_arm() {
121127
export CXX="$TOOLCHAIN_PREFIX-g++ $CXXFLAGS"
122128
export AR="$TOOLCHAIN_PREFIX-ar"
123129
export RANLIB="$TOOLCHAIN_PREFIX-ranlib"
130+
export LD="$TOOLCHAIN_PREFIX-ld"
124131
export STRIP="$TOOLCHAIN_PREFIX-strip --strip-unneeded"
125132
export MAKE="make -j5"
126133

@@ -140,6 +147,7 @@ function pop_arm() {
140147
export CC=$OLD_CC
141148
export CXX=$OLD_CXX
142149
export AR=$OLD_AR
150+
export LD=$OLD_LD
143151
export RANLIB=$OLD_RANLIB
144152
export STRIP=$OLD_STRIP
145153
export MAKE=$OLD_MAKE
@@ -239,6 +247,7 @@ function run_prepare() {
239247
test -d $PACKAGES_PATH || mkdir -p $PACKAGES_PATH
240248
test -d $BUILD_PATH || mkdir -p $BUILD_PATH
241249
test -d $LIBS_PATH || mkdir -p $LIBS_PATH
250+
test -d $LIBLINK_PATH || mkdir -p $LIBLINK_PATH
242251

243252
# create initial files
244253
echo "target=android-$ANDROIDAPI" > $SRC_PATH/default.properties
@@ -486,6 +495,7 @@ function run_distribute() {
486495
debug "Fill private directory"
487496
try cp -a python-install/lib private/
488497
try mkdir -p private/include/python2.7
498+
try mv libs/$ARCH/libpymodules.so private/
489499
try cp python-install/include/python2.7/pyconfig.h private/include/python2.7/
490500

491501
debug "Reduce private directory from unwanted files"
@@ -519,12 +529,19 @@ function run_distribute() {
519529

520530
}
521531

532+
function run_biglink() {
533+
push_arm
534+
try $BIGLINK $LIBS_PATH/libpymodules.so $LIBLINK_PATH
535+
pop_arm
536+
}
537+
522538
function run() {
523539
run_prepare
524540
run_source_modules
525541
run_get_packages
526542
run_prebuild
527543
run_build
544+
run_biglink
528545
run_postbuild
529546
run_distribute
530547
info "All done !"
@@ -536,6 +553,23 @@ function list_modules() {
536553
exit 0
537554
}
538555

556+
# one method to deduplicate some symbol in libraries
557+
function arm_deduplicate() {
558+
fn=$(basename $1)
559+
echo "== Trying to remove duplicate symbol in $1"
560+
push_arm
561+
try mkdir ddp
562+
try cd ddp
563+
try $AR x $1
564+
try $AR rc $fn *.o
565+
try $RANLIB $fn
566+
try mv -f $fn $1
567+
try cd ..
568+
try rm -rf ddp
569+
pop_arm
570+
}
571+
572+
539573
# Do the build
540574
while getopts ":hvlfm:d:" opt; do
541575
case $opt in

docs/source/android.rst

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
Android Python module
2+
=====================
3+
4+
Python for android project include a python module named "android". This module is designed to give you an access to the Android API. As for today, the module is very limited, and waiting for contribution to wrap more Android API.
5+
6+
Example
7+
-------
8+
9+
::
10+
11+
import android
12+
13+
# activate the vibrator
14+
android.vibrate(1)
15+
16+
# read screen dpi
17+
print android.get_dpi()
18+
19+
How it's working
20+
----------------
21+
22+
The whole Android API is accessible in Java. Their is no native or extensible way to access it from Python. The schema for accessing to their API is::
23+
24+
[1] Cython -> [2] C JNI -> [3] Java
25+
26+
#. ``android.pyx`` is written in `Cython <http://cython.org/>`_: a language
27+
with typed informations, very close to Python, that generate Python
28+
extension. It's easier to write in Cython than CPython, and it's linked
29+
directly to the part 2.
30+
#. ``android_jni.c`` is defining simple c methods that access to Java
31+
interfaces using JNI layer.
32+
#. The last part contain the Java code that will be called from the JNI stuff.
33+
34+
All the source code is available at:
35+
36+
https://github.com/kivy/python-for-android/tree/master/recipes/android/src
37+
38+
39+
API
40+
---
41+
42+
android
43+
~~~~~~~
44+
45+
.. module:: android
46+
47+
.. function:: check_pause()
48+
49+
This should be called on a regular basis to check to see if Android
50+
expects the game to pause. If it return true, the game should
51+
call :func:`android.wait_for_resume()`, after persisting its state
52+
as necessary.
53+
54+
.. function:: wait_for_resume()
55+
56+
This function should be called after :func:`android.check_pause()`
57+
returns true. It does not return until Android has resumed from
58+
the pause. While in this function, Android may kill a game
59+
without further notice.
60+
61+
.. function:: map_key(keycode, keysym)
62+
63+
This maps between an android keycode and a python keysym. The android
64+
keycodes are available as constants in the android module.
65+
66+
.. function:: vibrate(s)
67+
68+
Causes the phone to vibrate for `s` seconds. This requires that your
69+
application have the VIBRATE permission.
70+
71+
.. function:: accelerometer_enable(enable)
72+
73+
Enables (if `enable` is true) or disables the device's accelerometer.
74+
75+
.. function:: accelerometer_reading()
76+
77+
Returns an (x, y, z) tuple of floats that gives the accelerometer
78+
reading, in meters per second squared. See `this page
79+
<http://developer.android.com/reference/android/hardware/SensorEvent.html>`_
80+
for a description of the coordinate system. The accelerometer must
81+
be enabled before this function is called. If the tuple contains
82+
three zero values, the accelerometer is not enabled, not available,
83+
defective, has not returned a reading, or the device is in
84+
free-fall.
85+
86+
.. function:: get_dpi()
87+
88+
Returns the screen density in dots per inch.
89+
90+
.. function:: show_keyboard()
91+
92+
Shows the soft keyboard.
93+
94+
.. function:: hide_keyboard()
95+
96+
Hides the soft keyboard.
97+
98+
android_mixer
99+
~~~~~~~~~~~~~
100+
101+
.. module:: android_mixer
102+
103+
The android_mixer module contains a subset of the functionality in found
104+
in the `pygame.mixer <http://www.pygame.org/docs/ref/mixer.html>`_ module. It's
105+
intended to be imported as an alternative to pygame.mixer, using code like: ::
106+
107+
try:
108+
import pygame.mixer as mixer
109+
except ImportError:
110+
import android_mixer as mixer
111+
112+
Note that if you're using `kivy.core.audio
113+
<http://kivy.org/docs/api-kivy.core.audio.html>`_ module, you don't have to do
114+
anything, all is automatic.
115+
116+
The android_mixer module is a wrapper around the Android MediaPlayer
117+
class. This allows it to take advantage of any hardware acceleration
118+
present, and also eliminates the need to ship codecs as part of an
119+
application.
120+
121+
It has several differences from the pygame mixer:
122+
123+
* The init and pre_init methods work, but are ignored - Android chooses
124+
appropriate setting automatically.
125+
126+
* Only filenames and true file objects can be used - file-like objects
127+
will probably not work.
128+
129+
* Fadeout does not work - it causes a stop to occur.
130+
131+
* Looping is all or nothing, there's no way to choose the number of
132+
loops that occur. For looping to work, the
133+
:func:`android_mixer.periodic` function should be called on a
134+
regular basis.
135+
136+
* Volume control is ignored.
137+
138+
* End events are not implemented.
139+
140+
* The mixer.music object is a class (with static methods on it),
141+
rather than a module. Calling methods like :func:`mixer.music.play`
142+
should work.
143+
144+
.. note::
145+
146+
The android_mixer module hasn't been tested much, and so bugs may be
147+
present.

docs/source/helloworld.rst

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
Hello world example
2+
===================
3+
4+
If you don't know how to start with Python for Android, here is a simple
5+
tutorial for creating an UI using `Kivy <http://kivy.org/>`_, and make an APK
6+
with this project.
7+
8+
.. note::
9+
10+
Don't forget that Python for android is not Kivy only, and you might want
11+
to use other toolkit libraries. When other toolkits will be available, this
12+
documentation will be enhanced.
13+
14+
Let's create a simple Hello world application, with one Label and one Button.
15+
16+
#. Ensure you've correctly installed and configured the project as said in the
17+
:doc:`prerequisites`
18+
19+
#. Create a directory named ``helloworld``::
20+
21+
mkdir helloworld
22+
cd helloworld
23+
24+
#. Create a file named ``main.py``, with this content::
25+
26+
import kivy
27+
kivy.require('1.0.9')
28+
from kivy.lang import Builder
29+
from kivy.uix.gridlayout import GridLayout
30+
from kivy.properties import NumericProperty
31+
from kivy.app import App
32+
33+
Builder.load_string('''
34+
<HelloWorldScreen>:
35+
cols: 1
36+
Label:
37+
text: 'Welcome to the Hello world'
38+
Button:
39+
text: 'Click me! %d' % root.counter
40+
on_release: root.my_callback()
41+
''')
42+
43+
class HelloWorldScreen(GridLayout):
44+
counter = NumericProperty(0)
45+
def my_callback(self):
46+
print 'The button have been pushed'
47+
self.counter += 1
48+
49+
class HelloWorldApp(App):
50+
def build(self):
51+
return HelloWorldScreen()
52+
53+
if __name__ == '__main__':
54+
HelloWorldApp().run()
55+
56+
#. Go to the ``python-for-android`` directory
57+
58+
#. Create a distribute with kivy::
59+
60+
./distribute.sh -m kivy -d 'dist-kivy'
61+
62+
#. Go to the newly created ``dist-kivy`` distribution::
63+
64+
cd dist/dist-kivy
65+
66+
#. Plug your android device, and ensure you can install development application
67+
68+
#. Build your hello world application in debug mode::
69+
70+
./build.py --package org.hello.world --name "Hello world" \
71+
--version 1.0 --dir /PATH/TO/helloworld debug installd
72+
73+
#. Take your device, and start the application!
74+
75+
#. If it's goes wrong, open the logcat by doing::
76+
77+
adb logcat
78+
79+
The final debug APK will be located in ``bin/hello-world-1.0-debug.apk``.
80+
81+
If you want to release your application instead of just making a debug APK, you must:
82+
83+
#. Generate a non-signed APK::
84+
85+
./build.py --package org.hello.world --name "Hello world" \
86+
--version 1.0 --dir /PATH/TO/helloworld release
87+
88+
#. Continue by reading http://developer.android.com/guide/publishing/app-signing.html
89+
90+
91+
.. seealso::
92+
93+
`Kivy demos <https://github.com/kivy/kivy/tree/master/examples/demo>`_
94+
You can use them for creating APK too.
95+

docs/source/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@ Python for android is a project to create your own Python distribution
1010
including the modules you want, and create an apk including python, libs, and
1111
your application.
1212

13+
- Forum: https://groups.google.com/forum/?hl=fr#!forum/python-android
14+
- Mailing list: python-android@googlegroups.com
15+
16+
1317
.. toctree::
1418
:maxdepth: 2
1519

1620
introduction.rst
1721
prerequisites.rst
1822
usage.rst
23+
helloworld.rst
1924
howitworks.rst
2025
customize.rst
2126
recipes.rst
27+
android.rst
2228
related.rst
2329

2430
Indices and tables

docs/source/prerequisites.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@ After installing them, export both installation path, NDK version and API to use
4242
export ANDROIDNDKVER=r7
4343
export ANDROIDAPI=14
4444

45+
Also, you must configure you're PATH to add the ``android`` binary::
46+
47+
export PATH=/path/to/android-sdk-linux/tools:$PATH
4548

0 commit comments

Comments
 (0)