Skip to content

Commit

Permalink
Merge pull request #1610 from freakboy3742/android-libs
Browse files Browse the repository at this point in the history
Allow Android apps to specify their own list of libraries
  • Loading branch information
mhsmith authored Jan 30, 2024
2 parents 61e8c66 + 1652dbd commit 8b0e9b5
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/1610.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Android apps can now specify the base theme used to style the application.
1 change: 1 addition & 0 deletions changes/1610.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
It is highly recommended that Android applications add a definition for ``build_gradle_dependencies`` to their app configuration. A default value will be used if this option is not explicitly provided. Refer to `the Android documentation <https://briefcase.readthedocs.io/en/stable/reference/platforms/android.html#build-gradle-dependencies>`__ for the default value that will be used.
1 change: 1 addition & 0 deletions changes/485.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Android apps are now able to customize the libraries included in the app at build time.
17 changes: 17 additions & 0 deletions docs/reference/platforms/android/gradle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Colors

Android allows for some customization of the colors used by your app:

* ``base_theme`` is used to set the base Android theme.
* ``accent_color`` is used as a subtle highlight throughout your app to
call attention to key elements. It's used on things like form labels and
inputs.
Expand Down Expand Up @@ -156,6 +157,22 @@ the ``AndroidManifest.xml`` of your app.
Additional content that will be added verbatim just before the closing ``</activity>``
declaration of the ``AndroidManifest.xml`` of your app.

``base_theme``
--------------

The base theme for the application. Defaults to ``Theme.AppCompat.Light.DarkActionBar``

``build_gradle_dependencies``
-----------------------------

The list of libraries that should be linked into the Android application. Each library
should be a versioned Maven package specifier. If unspecified, three libraries will be
linked into the app:

* ``androidx.appcompat:appcompat:1.0.2``
* ``androidx.constraintlayout:constraintlayout:1.1.3``
* ``androidx.swiperefreshlayout:swiperefreshlayout:1.1.0``

``build_gradle_extra_content``
------------------------------

Expand Down
9 changes: 9 additions & 0 deletions src/briefcase/bootstraps/toga.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ def pyproject_table_android(self):
requires = [
"toga-android~=0.4.0",
]
base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
build_gradle_dependencies = [
"androidx.appcompat:appcompat:1.6.1",
"com.google.android.material:material:1.11.0",
# Needed for DetailedList
"androidx.swiperefreshlayout:swiperefreshlayout:1.1.0",
]
"""

def pyproject_table_web(self):
Expand Down
42 changes: 42 additions & 0 deletions src/briefcase/platforms/android/gradle.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,47 @@ def output_format_template_context(self, app: AppConfig):
build = int(getattr(app, "build", "0"))
version_code = f"{v[0]:d}{v[1]:02d}{v[2]:02d}{build:02d}".lstrip("0")

# The default runtime libraries included in an app. The default value is the
# list that was hard-coded in the Briefcase 0.3.16 Android template, prior to
# the introduction of customizable system requirements for Android.
try:
dependencies = app.build_gradle_dependencies
except AttributeError:
self.logger.warning(
(
"""
*************************************************************************
** WARNING: App does not define build_gradle_dependencies **
*************************************************************************
The Android configuration for this app does not contain a
`build_gradle_dependencies` definition. Briefcase will use a default
value of:
build_gradle_dependencies = [
"androidx.appcompat:appcompat:1.0.2",
"androidx.constraintlayout:constraintlayout:1.1.3",
"androidx.swiperefreshlayout:swiperefreshlayout:1.1.0",
]
You should add this definition to the Android configuration
of your project's pyproject.toml file. See:
https://briefcase.readthedocs.io/en/stable/reference/platforms/android.html#build_gradle-dependencies
for more information.
*************************************************************************
"""
),
prefix=app.app_name,
)
dependencies = [
"androidx.appcompat:appcompat:1.0.2",
"androidx.constraintlayout:constraintlayout:1.1.3",
"androidx.swiperefreshlayout:swiperefreshlayout:1.1.0",
]

return {
"version_code": version_code,
"safe_formal_name": safe_formal_name(app.formal_name),
Expand All @@ -180,6 +221,7 @@ def output_format_template_context(self, app: AppConfig):
for path in (app.test_sources or [])
if (name := Path(path).name)
),
"build_gradle_dependencies": {"implementation": dependencies},
}

def permissions_context(self, app: AppConfig, x_permissions: dict[str, str]):
Expand Down
18 changes: 18 additions & 0 deletions tests/commands/new/test_build_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ def main():
requires = [
"toga-android~=0.4.0",
]
base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
build_gradle_dependencies = [
"androidx.appcompat:appcompat:1.6.1",
"com.google.android.material:material:1.11.0",
# Needed for DetailedList
"androidx.swiperefreshlayout:swiperefreshlayout:1.1.0",
]
""",
pyproject_table_web="""
requires = [
Expand Down Expand Up @@ -1235,6 +1244,15 @@ def main():
requires = [
"toga-android~=0.4.0",
]
base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
build_gradle_dependencies = [
"androidx.appcompat:appcompat:1.6.1",
"com.google.android.material:material:1.11.0",
# Needed for DetailedList
"androidx.swiperefreshlayout:swiperefreshlayout:1.1.0",
]
""",
pyproject_table_web="""
requires = [
Expand Down
55 changes: 55 additions & 0 deletions tests/platforms/android/gradle/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,61 @@ def test_version_code(create_command, first_app_config, version, build, version_
assert int(version_code) < 2147483647


@pytest.mark.parametrize(
"input, output, has_warning",
[
(
None,
{
"implementation": [
"androidx.appcompat:appcompat:1.0.2",
"androidx.constraintlayout:constraintlayout:1.1.3",
"androidx.swiperefreshlayout:swiperefreshlayout:1.1.0",
]
},
True,
),
(
[],
{"implementation": []},
False,
),
(
[
"com.example.foo:foo:1.2.3",
"com.example.bar:bar:2.3.4",
],
{
"implementation": [
"com.example.foo:foo:1.2.3",
"com.example.bar:bar:2.3.4",
]
},
False,
),
],
)
def test_build_gradle_dependencies(
create_command,
first_app_config,
input,
output,
has_warning,
capsys,
):
"""Validate that create adds version_code to the template context."""
if input is not None:
first_app_config.build_gradle_dependencies = input

context = create_command.output_format_template_context(first_app_config)
assert context["build_gradle_dependencies"] == output

assert (
"** WARNING: App does not define build_gradle_dependencies **"
in capsys.readouterr().out
) == has_warning


extract_packages_params = [
([], ""),
([""], ""),
Expand Down

0 comments on commit 8b0e9b5

Please sign in to comment.