The Android Arsenal – Gradle Plugins

About

Plugin does not install python and pip itself and use globally installed python (by default). It’s easier to prepare python manually because python have good compatibility (from user perspective) and does not need to be updated often.

Linux/Macos install

Make sure the latest pip installed (required to overcome some older pip problems):

Note that on ubuntu pip installed with python3-pip package is 9.0.1, but it did not(!) downgrade module versions (e.g. pip install click 6.6 when click 6.7 is installed will do nothing). Maybe there are other differences, so it’s highly recommended to upgrade pip with pip3 install -U pip.

Automatic python install

Python is assumed to be used as java: install and forget. It perfectly fits user use case: install python once and plugin will replace all manual work.

It is also easy to configure python on CI (like travis).

If you want automatic python installation, try looking on JetBrain’s python-envs plugin. But be careful because it has some caveats (for example, on windows python could be installed automatically just once and requires manual un-installation).

Travis configuration

To make plugin work on travis you’ll need sudo and install pip manually:

Usage

Module format is: name:version (will mean name==version in pip notion). Non strict version definition is not allowed (for obvious reasons). Dependencies are installed in declaration order. If duplicate declaration specified then only the latest declaration will be used:

python.pip 'module1:2.0', 'module2:1.0', 'module1:1.0'

Will install version 1.0 of module1 because it was the latest declaration.

Dependencies are installed with pipInstall task which is called before any declared PythonTask.

Virtualenv

When you declare any pip modules, plugin will try to use virtualenv in order to install required modules locally (for current project only).

If virtualenv is not installed – it will be installed automatically in --user scope. If you don’t want automatic installation then disable it:

python.installVirtualenv =false

In any case, plugin checks if virtualenv is already installed and use it to create local environment (if not, then fall back to --user scope by default). Virtualenv usage is driven by declared scope, so if you don’t want to use it set:

python.scope =USER// or GLOBAL

With USER (or GLOBAL) scope, virtualenv will not be used, even if it’s already created in project (plugin will ignore it and use global python).

If you already use virtualenv in your project (have created environment), then simply point plugin to use it:

python.envPath ='path/to/your/env'

It will automatically change pythonPath configuration accordingly.

NOTE: plugin will not create environment if you don’t use any modules. If you still want to use project specific environment (without declared pip modules) then create it manually: python3 -m virtualenv .gradle/python (default location). Plugin will recognize existing env and use it.

IMPORTANT: virtualenv creates local python copy (by default in .gradle/python). Copy is created from global python and later used instead of global python. If you want to change used python version in the environment, then manually remove .gradle/python so it could be created again (from global python).

To copy environment instead of symlinking (default) set ( –always-copy):

Scope

Pip dependencies could be installed per project, for current user (~/) or globally.

Command parsing

When command passed as string it is manually parsed to arguments array (split by space):

Spaces in quotes are ignored: "quoted space" or 'quoted space'

Escaped sapces are ignored: with\ space (argument will be used with simple space then – escape removed).

Escaped quotes are ignored: "with \"interrnal quotes\" inside". But pay attention that it must be 2 symbols \" and not" because otherwise it is impossible to detect escape.

To view parsed arguments run gradle with -i flag (enable info logs). In case when command can’t be parsed properly (bug in parser or unsupported case) use array of arguments instead of string.

Configuration

Python location

On linux, plugin will use python3 if available (and fall back to python if not). To use different binary use:

python {
pythonBinary ='python'
}

This will force python 2 for linux. Also, this may be handy if python binary is named differently.

To use non global python:

python {
pythonPath ='path/to/python/binray/'
}

pythonPath must be set to directory containing python binary (e.g. ‘path/to/python/binray/python.exe’)

NOTE: pythonPath is ignored when virtualenv used.

Minimal python and pip versions

To set python version constraint:

python {
minPythonVersion ='3.2'
}

Python version format is: major.minor.micro. Constraint may include any number of levels: ‘3’, ‘3.1’, ‘2.7.5’

The same way pip version could be restricted:

python {
minPipVersion ='9.0.1'
}

Pip

By default, all installed python modules are printed to console after pip installations using pip list (of course, if at least one module were declared for installation). This should simplify problems resolution (show used transitive dependencies versions).

To switch off:

python {
showInstalledVersions =false
}

You can always see the list of installed modules with pipList task (exactly the same list as after pipInstall).

NOTE: if global python is used with USER scope and some modules were manually installed in global scope then they will not be shown by pipList (and after pip install). To see all modules:

Global modules are hidden by default (for USER scope) because on linux there are a lot of system modules pre-installed.

By default ‘pip install’ is not called for modules already installed with correct version. In most situations this is preferred behaviour, but if you need to be sure about dependencies then force installation: