Skip to content

Writing an Android application

Pierre Quentel edited this page Mar 7, 2017 · 29 revisions

With Brython you can write applications in Python that run in modern browsers, including most of smartphones browsers. But you can also write native Android applications : this page explains how to do it, taking the example of a basic geolocation application.

The first step is to install the latest version of Android Studio.

1. Initialize the application

Follow the instructions to create a new application in Android Studio. In this post, we suppose that you have entered :

application name  : NewBrythonApp
company domain : brython

For the Form factor : select Phone and Tablet, and the minimum SDK required (API 15 is ok).

In "Add an Activity to Mobile" select "Empty activity".

In "Customize the Activity" keep the default names and click "Finish".

Android Studio starts building the project and presents a window like this one :

The application files are stored in a directory shown in the title bar of the Android Studio window. In the rest of this page, you can edit the files either in Android Studio or in any text editor.

2. Customize settings

2.1 activity_main.xml

Edit the file activity_main.xml and replace its content by

<FrameLayout xmlns:android=""
         android:layout_height="match_parent" />


The content of this file may vary with the kind of application you want to use. The setting must at least allow the use of Javascript ; in the example below, geolocation is also enabled.

Keep the first line of the script (with the package name) and replace the rest with :

import android.content.Context;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.GeolocationPermissions;

public class MainActivity extends AppCompatActivity {

    private WebView mWebView;

    protected void onCreate(Bundle savedInstanceState) {

        mWebView = (WebView) findViewById(;

        WebSettings webSettings = mWebView.getSettings();

        // Enable Javascript

        // Enable geolocation

        mWebView.setWebChromeClient(new WebChromeClient() {
            // always accept geolocation
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
        Context context = getApplicationContext();
        mWebView.getSettings().setGeolocationDatabasePath( context.getFilesDir().getPath() );

        // WebView entry point


2.3 AndroidManifest.xml

Replace the content of the <manifest> tag by :

    android:label="Brython Geolocation"
    <activity android:name=".MainActivity">
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

The value of android:label will be the name of the application on the Android smartphone when it is installed.

2.4 styles.xml

The application uses a style, NoTitleBar, that must be defined in the file styles.xml, in the subdirectory res/values. We add the theme by adding a new <style> tag :


    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

    <!-- Theme with no title. -->
    <style name="NoTitleBar" parent="Theme.AppCompat.NoActionBar">


3. Write the Brython application

Create a subdirectory assets/www in the Android application directory. This is where we will store the Brython application. It includes at least index.html (the page loaded by the Android app) and brython.js, plus brython_stdlib.js if the application uses modules from the standard distribution and all the additional files (css, images, javascript...) needed by the application.

In this example, we only use brython.js and index.html holds a basic geolocation application :

<!doctype html>
<script type="text/javascript" src="brython.js"></script>
.rTable {
    display: table;
    width: 100%;
.rTableRow {
    display: table-row;
.rTableHead {
    display: table-cell;
    padding: 3px 10px;
    border: 0px

<body onload="brython()">

<script type="text/python">
from browser import document, window, html

map = document["map"]
def success(position):
    lat = position.coords.latitude
    long = position.coords.longitude
    # use Google Maps API to print a map
    url = ""
    params = "markers=%f,%f&zoom=15&size=320x298&sensor=true" % (lat, long)
    mapurl = url + "?" + params
    map <= html.IMG(src=mapurl)

def refresh(*args):
    # cf.

document["refresh"].bind("click", refresh)

<div class="rTable">
  <div class="rTableRow">
    <div class="rTableHead">Current location</div>
    <div class="rTableHead" align="right"><button id="refresh">Refresh</button></div>

<div id="map"></div>


4. Build the Android application (APK)

To generate the Android executable (a file with the extension .apk), select Build / Build APK in Android Studio. When the file is generated, use "Show in explorer" to open the file system explorer at the location of the APK file, which is called app-debug.apk.

5. Install the application on the smartphone

Connect the Android smartphone to the PC with a cable and choose a location for the APK file, for instance Download, preferably in Card if the smartphone has a memory card, otherwise in Phone. Use the file explorer to drag-and-drop app-debug.apk to this location.

Open the Applications window on the smartphone and select an application to explore the local files ; usually My Files is installed, otherwise download it from Google Play. With this application, navigate to the directory where app-debug.apk has been installed, and click on it.

You will get a message saying that the installation is blocked for security reasons ; select Parameters and check the "Unknown Sources" box. In the next page, select "Install", then "Open" to run the application.

The application is now featured in the Applications window, with the default Android icon, under the name "Brython Geolocation", the one we specified in section android:label of AndroidManifest.xml.

You can’t perform that action at this time.