Spending a huge amount of time studying java, for writing simple bot programs I consider not the best solution
Closed due to the fact that the essence of the issue is incomprehensible by the participants of the Kromster , αλεχολυτ , Yuriy Spb ♦ , user194374, Denis Aug 29 '16 at 6:08 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
1 answer
Python-for-android First of all, let's look at how Python gets the opportunity to work under Android - a tool called, oddly enough, python-for-android. Its main function is to create a distribution kit - a project folder containing everything you need to run your application. Or rather, the interpreter itself, Kivy, and the libraries on which it depends: Pygame, SDL, and several others. The distribution also includes a Java loader that displays OpenGL and acts as an intermediary between Kivy and the operating system. Then you add to all this your scripts, settings like icons and names, compile using Android NDK and voila - APK is ready with your application!
And this is just a basic procedure, in fact the generated batch file can include (and includes) much more. Along with everything else, most of the standard library is stitched into the APK, and any third-party module written in Python can be easily added - all the same as in the development of desktop applications. Adding modules with compiled components is also not difficult, you only need to specify how they should be assembled. As a rule, this does not constitute anything complicated; it is enough just to put a couple of checkboxes before starting the assembly procedure, although in rare isolated cases additional actions may be needed. Python-for-android already includes instructions for compiling such popular modules as: numpy, sqlite3, twisted, and even django!
The above principles explain only in general terms how python-for-android works. You can get more information on this topic at any time by looking at the Kivy documentation. I recommend you Buildozer - an add-on for python-for-android, which provides a convenient interface and automatic resolution of some dependencies. We are trying to make the above chain of actions used not only in Kivy, but also in other projects. The basic build process will remain the same, but the need for a Java loader will disappear, since it is only needed to support some of the specific needs of the framework.
Accessing the Android API with PyJNIus Interacting with the Android API: getting information from sensors, creating notifications, vibrating, pausing, and restarting, whatever - an important part of your application. Kivy will take care of the basics for you, but you will want to manage many things yourself. For this, PyJNIus was created - a tool that automatically wraps Java code into the Python interface.
As a simple example, here’s a program that makes the phone vibrate for 10 seconds:
from jnius import autoclass # Для начала нам нужна ссылка на Java Activity, в которой # запущено приложение, она хранится в загрузчике Kivy PythonActivity PythonActivity = autoclass('org.renpy.android.PythonActivity') activity = PythonActivity.mActivity Context = autoclass('android.content.Context') vibrator = activity.getSystemService(Context.VIBRATOR_SERVICE) vibrator.vibrate(10000) # аргумент указывается в миллисекундах If you are familiar with the Android API, you will easily notice that the code above is very similar to similar to Java - PyJNIus simply allows us to access the same API, but directly from Python. Most of the Android API can be called in a similar way, which allows to achieve the same functionality as when developing in Java.
The main disadvantage of PyJNIus is that it requires a good understanding of the structure of the Android API, and the code is cumbersome, although its Java equivalent looks exactly the same. To solve this problem, Kivy includes Plyer.
Plyer: cross-platform API for platform-specific tasks The Plyer project aims to create a simple “pythonous” interface for functions that are present on most platforms. For example, the code above with a flick of the wrist turns into ...
from plyer.vibrator import vibrate vibrate(10) # В Plyer аргументы указываются в секундах Moreover, the written code will try to perform its task on all platforms supported by Plyer - at the moment it is: Android, iOS, Linux, Windows and OS X (for iOS there is also an analog of PyJNIus, called PyOBJus). In fact, vibration is not the best example, because now it is implemented only for Android, but features such as checking the battery level:
from plyer import battery; print(battery.status) or text-to-speech:
from plyer import tts; tts.speak('hello world') - work in both desktop and mobile applications, and data acquisition from the compass / gyroscope and sending SMS are implemented without any problems on Android and iOS.
Plyer is at the initial stage of development, so any help in the design is welcome. Also, we are participating with him in the Google Summer of Code this year.
Not only for the sake of Kivy. All of the above tools were developed for our framework, but in fact they are more intended to be developed for Python as a whole. In Plyer, we specifically avoid any dependency on Kivy, and PyJNIus is only needed to access the Android JNI. We sincerely hope that these tools will be useful for anyone who writes in Python under Android. You can already try PyJNIus using QPython. Python-for-android is more tied up with Kivy, but we will be happy to discuss this issue.
Much can be realized when developing on Android using Python, despite all the differences with Java, which is designed for this, but these capabilities can be expanded even further in the near future. And if you are interested in the projects described above, then it's time to join our team!
Original article " Kivy Planet "
Russian source " How to develop in Python under Android "
- You forgot to specify a link to the source;) - s8am
- @ s8am for sure. Added by - Artem Gafarov
apt-get install pythonand continue using it as in any normal Linux (or macosi, the same thing ) - andreymal