|
| 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 | + |
0 commit comments