Python Code Runner For Android

The monkeyrunner tool provides an API for writing programs that control an Android device or emulator from outside of Android code. With monkeyrunner, you can write a Python program that installs an Android application or test package, runs it, sends keystrokes to it, takes screenshots of its user interface, and stores screenshots on the workstation. The monkeyrunner tool is primarily designed to test applications and devices at the functional/framework level and for running unit test suites, but you are free to use it for other purposes.

  • The source code for both the apps (Python file, APK, and buildozer spec) can be found at this GitHub repository. If you don’t want to use the APK generated into your devices then you simply host them online using a free service called Appetize.io which allows you to run android apps in browsers!
  • Jun 14, 2018 This is what BeeWare provides. Tools to help you write Python code with a rich, native user interface; and the libraries and support code necessary to get that code running on iOS, Android, macOS.

Sep 30, 2018 Many android applications are available to run the python scripts on android devices. In modern computing you can’t do computing only on an 80×25 console window but you can also do that on phones, tablets, and desktop machines with rich user interfaces.

AndroidPython Code Runner For Android

The monkeyrunner tool is not related to the UI/Application Exerciser Monkey, also known as the monkey tool. The monkey tool runs in an adb shell directly on the device or emulator and generates pseudo-random streams of user and system events. In comparison, the monkeyrunner tool controls devices and emulators from a workstation by sending specific commands and events from an API.

The monkeyrunner tool provides these unique features for Android testing:

  • Multiple device control: The monkeyrunner API can apply one or more test suites across multiple devices or emulators. You can physically attach all the devices or start up all the emulators (or both) at once, connect to each one in turn programmatically, and then run one or more tests. You can also start up an emulator configuration programmatically, run one or more tests, and then shut down the emulator.
  • Functional testing: monkeyrunner can run an automated start-to-finish test of an Android application. You provide input values with keystrokes or touch events, and view the results as screenshots.
  • Regression testing - monkeyrunner can test application stability by running an application and comparing its output screenshots to a set of screenshots that are known to be correct.
  • Extensible automation - Since monkeyrunner is an API toolkit, you can develop an entire system of Python-based modules and programs for controlling Android devices. Besides using the monkeyrunner API itself, you can use the standard Python os and subprocess modules to call Android tools such as Android Debug Bridge.

    You can also add your own classes to the monkeyrunner API. This is described in more detail in the section Extending monkeyrunner with plugins.

The monkeyrunner tool uses Jython, an implementation of Python that uses the Java programming language. Jython allows the monkeyrunner API to interact easily with the Android framework. With Jython you can use Python syntax to access the constants, classes, and methods of the API.

Python Code Runner For Android Studio Code

A simple monkeyrunner program

Here is a simple monkeyrunner program that connects to a device, creating a MonkeyDevice object. Using the MonkeyDevice object, the program installs an Android application package, runs one of its activities, and sends key events to the activity. The program then takes a screenshot of the result, creating a MonkeyImage object. From this object, the program writes out a .png file containing the screenshot.

The monkeyrunner API

The monkeyrunner API is contained in three modules in the package com.android.monkeyrunner:

  • MonkeyRunner: A class of utility methods for monkeyrunner programs. This class provides a method for connecting monkeyrunner to a device or emulator. It also provides methods for creating UIs for a monkeyrunner program and for displaying the built-in help.
  • MonkeyDevice: Represents a device or emulator. This class provides methods for installing and uninstalling packages, starting an Activity, and sending keyboard or touch events to an application. You also use this class to run test packages.
  • MonkeyImage: Represents a screen capture image. This class provides methods for capturing screens, converting bitmap images to various formats, comparing two MonkeyImage objects, and writing an image to a file.

In a Python program, you access each class as a Python module. The monkeyrunner tool does not import these modules automatically. To import a module, use the Python from statement:

where <module> is the class name you want to import. You can import more than one module in the same from statement by separating the module names with commas.

Running monkeyrunner

You can either run monkeyrunner programs from a file, or enter monkeyrunner statements in an interactive session. You do both by invoking the monkeyrunner command which is found in the tools/ subdirectory of your SDK directory. If you provide a filename as an argument, the monkeyrunner command runs the file's contents as a Python program; otherwise, it starts an interactive session.

The syntax of the monkeyrunner command is

Table 1 explains the flags and arguments.

Table 1.monkeyrunner flags and arguments.

ArgumentDescription
-plugin <plugin_jar> (Optional) Specifies a .jar file containing a plugin for monkeyrunner. To learn more about monkeyrunner plugins, see Extending monkeyrunner with plugins. To specify more than one file, include the argument multiple times.
<program_filename> If you provide this argument, the monkeyrunner command runs the contents of the file as a Python program. If the argument is not provided, the command starts an interactive session.
<program_options> (Optional) Flags and arguments for the program in <program_file>.
Runner

monkeyrunner built-in help

You can generate an API reference for monkeyrunner by running:

The arguments are:

  • <format> is either text for plain text output or html for HTML output.
  • <outfile> is a path-qualified name for the output file.

Extending monkeyrunner with plugins

You can extend the monkeyrunner API with classes you write in the Java programming language and build into one or more .jar files. You can use this feature to extend the monkeyrunner API with your own classes or to extend the existing classes. You can also use this feature to initialize the monkeyrunner environment.

To provide a plugin to monkeyrunner, invoke the monkeyrunner command with the -plugin <plugin_jar> argument described in table 1.

In your plugin code, you can import and extend the main monkeyrunner classes MonkeyDevice, MonkeyImage, and MonkeyRunner in com.android.monkeyrunner (see The monkeyrunner API).

Note that plugins do not give you access to the Android SDK. You can't import packages such as com.android.app. This is because monkeyrunner interacts with the device or emulator below the level of the framework APIs.

The plugin startup class

The .jar file for a plugin can specify a class that is instantiated before script processing starts. To specify this class, add the key MonkeyRunnerStartupRunner to the .jar file's manifest. The value should be the name of the class to run at startup. The following snippet shows how you would do this within an ant build script:

To get access to monkeyrunner's runtime environment, the startup class can implement com.google.common.base.Predicate<PythonInterpreter>. For example, this class sets up some variables in the default namespace:

Table Of Contents

  • Create a package for Android
    • Packaging your application for the Kivy Launcher

You can create a package for android using the python-for-android project. This page explains howto download and use it directly on your own machine (seePackaging with python-for-android) oruse the Buildozer tool to automate the entire process. You can also seePackaging your application for the Kivy Launcher to run kivyprograms without compiling them.

For new users, we recommend using Buildozer as the easiest wayto make a full APK. You can also run your Kivy app without acompilation step with the Kivy Launcher app.

Kivy applications can be released on an Android market such as the Play store, with a few extrasteps to create a fully signed APK.

The Kivy project includes tools for accessing Android APIs toaccomplish vibration, sensor access, texting etc. These, along withinformation on debugging on the device, are documented at themain Android page.

Buildozer¶

Buildozer is a tool that automates the entire build process. Itdownloads and sets up all the prequisites for python-for-android,including the android SDK and NDK, then builds an apk that can beautomatically pushed to the device.

Buildozer currently works only in Linux, and is a betarelease, but it already works well and can significantly simplify theapk build.

You can get buildozer at https://github.com/kivy/buildozer:

This will install buildozer in your system. Afterwards, navigate toyour project directory and run:

This creates a buildozer.spec file controlling your buildconfiguration. You should edit it appropriately with your app nameetc. You can set variables to control most or all of the parameterspassed to python-for-android.

Install buildozer’s dependencies.

Finally, plug in your android device and run:

to build, push and automatically run the apk on your device.

Buildozer has many available options and tools to help you, the stepsabove are just the simplest way to build and run yourAPK. The full documentation is available here. You can also checkthe Buildozer README at https://github.com/kivy/buildozer.

Packaging with python-for-android¶

You can also package directly with python-for-android, which can giveyou more control but requires you to manually download parts of theAndroid toolchain.

See the python-for-android documentationfor full details.

Packaging your application for the Kivy Launcher¶

The Kivy launcheris an Android application that runs any Kivy examples stored on yourSD Card.To install the Kivy launcher, you must:

  1. Go to the Kivy Launcher pageon the Google Play Store

  2. Click on Install

  3. Select your phone… And you’re done!

If you don’t have access to the Google Play Store on your phone/tablet,you can download and install the APK manually from http://kivy.org/#download.

Once the Kivy launcher is installed, you can put your Kivyapplications in the Kivy directory in your external storage directory(often available at /sdcard even in devices where this memoryis internal), e.g.

<yourapplication> should be a directory containing:

The file android.txt must contain:

These options are just a very basic configuration. If you create yourown APK using the tools above, you can choose many other settings.

Installation of Examples¶

Kivy comes with many examples, and these can be a great place to starttrying the Kivy launcher. You can run them as below:

/sdcard/kivy

  1. Run the launcher and select one of the Pictures, Showcase, Touchtracer, Cymunk or other demos…

Release on the market¶

If you have built your own APK with Buildozer or withpython-for-android, you can create a release version that may bereleased on the Play store or other Android markets.

To do this, you must run Buildozer with the release parameter(e.g. buildozerandroidrelease), or if usingpython-for-android use the --release option to build.py. Thiscreates a release APK in the bin directory, which you mustproperly sign and zipalign.The procedure for doing this is described in the Android documentationat https://developer.android.com/studio/publish/app-signing.html#signing-manually -all the necessary tools come with the Android SDK.

Targeting Android¶

Python code runner for android program

Free Python Code Examples

Kivy is designed to operate identically across platforms and as a result, makessome clear design decisions. It includes its own set of widgets and by default,builds an APK with all the required core dependencies and libraries.

Python Code Runner For Android Software

It is possible to target specific Android features, both directly andin a (somewhat) cross-platform way. See the Using Android APIs sectionof the Kivy on Android documentation for more details.

Comments are closed.