Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ tmp
.project
.settings
.classpath
.tox/
.pytest_cache/
__pycache__/
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ However, if you *do* want use this template directly...
application lifecycle (`onCreate`, `onResume` and so on); it's up to
you what your code does with those lifecycle hooks.

If your code has any dependencies, they should be listed in the file
`My Project/app/requirements.txt`.
Briefcase installs dependencies into `My Project/app/app_packages`, with shared files in `common` and architecture-specific files in directories named for each Android ABI. When using the template directly, populate this directory layout before building. Chaquopy packages these directories without installing dependencies.

If you've done this correctly, a project with a formal name of
`My Project`, with an app name of `my-project` should have a directory
Expand All @@ -61,7 +60,10 @@ structure that looks something like:
AndroidManifest.xml
build.gradle
proguard-rules.pro
requirements.txt
app_packages/
common/
arm64-v8a/
x86_64/
briefcase.toml
build.gradle
gradle.properties
Expand Down
40 changes: 40 additions & 0 deletions tests/test_requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import tomllib
from pathlib import Path

import pytest
from cookiecutter.main import cookiecutter


@pytest.mark.parametrize(
("python_version", "abis"),
[
("3.11.17", '"arm64-v8a", "armeabi-v7a", "x86_64"'),
("3.12.15", '"arm64-v8a", "x86_64"'),
("3.13.16", '"arm64-v8a", "x86_64"'),
],
)
def test_preinstalled_requirements(tmp_path, python_version, abis):
"""The rendered project accepts dependency directories prepared by Briefcase."""
project = Path(
cookiecutter(
str(Path(__file__).resolve().parents[1]),
no_input=True,
default_config={"replay_dir": str(tmp_path / "replay")},
output_dir=str(tmp_path),
extra_context={
"python_version": python_version,
"permissions": {},
"features": {},
},
)
)
config = tomllib.loads((project / "briefcase.toml").read_text(encoding="utf-8"))
assert config["paths"]["app_packages_path"] == "app/app_packages"
assert "app_requirements_path" not in config["paths"]
assert "app_requirement_installer_args_path" not in config["paths"]
gradle = (project / "app/build.gradle").read_text(encoding="utf-8")
assert 'install "-r", "requirements.txt"' not in gradle
assert 'outputDir.set(layout.projectDirectory.dir("app_packages"))' in gradle
assert "enabled = false" in gradle
assert "afterEvaluate {" in gradle
assert f"abiFilters {abis}\n" in gradle
9 changes: 9 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tox]
env_list = py313

[testenv]
skip_install = true
deps =
briefcase
pytest
commands = python -m pytest {posargs:tests}
22 changes: 11 additions & 11 deletions {{ cookiecutter.format }}/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ android {
python {
version "{{ cookiecutter.python_version|py_tag }}"

pip {
install "-r", "requirements.txt"

def f = new File(project.projectDir, "pip-options.txt")
def pipOptions = f.getText("utf-8").split("\n")
pipOptions = pipOptions.findAll { !it.isBlank() }
options(*pipOptions)
}
def f = new File(project.projectDir, "extract-packages.txt")
def packages = f.readLines("utf-8")
.collect { it.trim() }
Expand All @@ -41,9 +33,7 @@ android {
{% if cookiecutter.ndk.abi_filters %}
abiFilters {% for abi in cookiecutter.ndk.abi_filters %}"{{ abi }}"{% if not loop.last %}, {% endif %}{% endfor %}

{% elif cookiecutter.python_version|py_tag in ["3.9", "3.10", "3.11"] %}
// Chaquopy also supports x86, but it's not very useful anymore, so we'll
// disable it to speed up the build.
{% elif cookiecutter.python_version|py_tag == "3.11" %}
abiFilters "arm64-v8a", "armeabi-v7a", "x86_64"
{% else %}
// For this Python version, only 64-bit ABIs are available.
Expand All @@ -64,6 +54,16 @@ android {
}
}

// Briefcase installs requirements into common and ABI-specific directories.
// Keep Chaquopy's packaging tasks, but bypass its pip installation task for every
// build variant. This also preserves the packages when Gradle's build is cleaned.
afterEvaluate {
tasks.withType(com.chaquo.python.TaskBuilder.ReqsTask).configureEach {
enabled = false
outputDir.set(layout.projectDirectory.dir("app_packages"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than depending on Chaquopy's undocumented internals, I'd be open to adding a public API for this. But first we need to deal with the Windows compatibility issue in beeware/briefcase#3099, because that might make this whole approach impossible.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Windows problem is fixed in beeware/xbuild#119, which is now merged, so this approach works on Windows. I'd be happy to move to a public API if you add one.

}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "androidx.appcompat:appcompat:1.7.1"
Expand Down
5 changes: 2 additions & 3 deletions {{ cookiecutter.format }}/briefcase.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Generated using Python {{ cookiecutter.python_version }}
[briefcase]
target_version = "0.3.27"
target_version = "0.4.7"

[paths]
app_path = "app/src/main/python"
app_requirements_path = "app/requirements.txt"
app_requirement_installer_args_path = "app/pip-options.txt"
app_packages_path = "app/app_packages"
metadata_resource_path = "app/src/main/res/values/briefcase.xml"
extract_packages_path = "app/extract-packages.txt"

Expand Down
Loading