Amper - Projecte

Modularitzar

An Amper project is defined by a project.yaml file. This file contains the list of modules and the project-wide configuration. The folder with the project.yaml file is the project root. Modules can only be located under the project root (at any depth). If there is only one module in the project, the project.yaml file is not required.

An Amper module is a directory with a module.yaml configuration file, and optionally sources and resources. A module configuration file describes what to produce: e.g. a reusable library or a platform-specific application. Each module describes a single product. Several modules can’t share the same sources or resources, but they can depend on each other. How to produce the desired product, that is, the build rules, is the responsibility of the Amper build engine.

Make the project multiplatform

|-app/
| |-src/
| | |-main.kt
| |-...
| |-module.yaml
|-lib/
| |-src/
| | |-util.kt
| |-module.yaml
|-project.yaml

En el cas anterior, el project.yaml es veu així:

project.yaml
modules:
- app
- lib

Consulta la referència per a més opcions per definir la llista de mòduls al fitxer project.yaml.

Dividim el nostre projecte en una aplicació JVM i un mòdul de biblioteca, amb codi compartit que reutilitzarem més tard quan fem el projecte multiplataforma.

El nostre objectiu aquí és separar la nostra aplicació en un mòdul de biblioteca shared i un mòdul d’aplicació jvm-app i aconseguir la següent estructura:

Recursos

In multiplatform modules resources are merged from the common folders and corresponding platform-specific folders:

|-src/
| |-...
|-resources/ # these resources are copied into the Android and JVM artifact
| |-...
|-resources@android/ # these resources are copied into the Android artifact
| |-...
|-resources@jvm/ # these resources are copied into the JVM artifact
| |-...

In the case of duplicating names, the common resources are overwritten by the more specific ones. That is resources/foo.txt will be overwritten by resources@android/foo.txt.

Android modules also have res and assets folders:

|-src/
| |-...
|-res/
| |-drawable/
| | |-...
| |-layout/
| | |-...
| |-...
|-assets/
| |-...
|-module.yaml

Kotlin Symbol Processing

Documentation - KSP

Kotlin - Kotlin Symbol Processing

Amper provides built-in support for KSP.

Amper is compatible with KSP2, so all processors must be updated to this version.

To add a processor to your module, use the settings.kotlin.ksp section, where you can also specify options for KSP.

Veure un exemple a Room - Bàsic

Alguns processadors es poden personalitzar passant opcions. Pots passar aquestes opcions utilitzant la secció processorOptions:

settings:
kotlin:
ksp:
processors:
- androidx.room:room-compiler:2.7.0-alpha12
processorOptions:
room.schemaLocation: ./schema
Note

Totes les opcions es passen a tots els processadors per KSP. És responsabilitat del processador utilitzar noms d’opció únics per evitar conflictes amb les opcions d’altres processadors.

Dependències

Àmbits i visibilitat

PENDENT moure a l’àmbit corresponent.

Hi ha tres àmbits de dependència:

all(per defecte) la dependència està disponible durant la compilació i l’execució.
compile-onlyla dependència només està disponible durant la compilació.
runtime-onlyLa dependència no està disponible durant la compilació, però està disponible durant les proves i l’execució

En la forma completa, pots declarar l’àmbit de la següent manera:

module.yaml
dependencies:
- io.ktor:ktor-client-core:2.2.0:
scope: compile-only
- ../ui/utils:
scope: runtime-only

També hi ha una forma abreujada:

module.yaml
dependencies:
- io.ktor:ktor-client-core:2.2.0: compile-only
- ../ui/utils: runtime-only

Per defecte, totes les dependències no són accessibles des del codi dependent. Per fer que una dependència sigui visible per a un mòdul dependent, cal marcar-la explícitament com a exportada.

module.yaml
dependencies:
- io.ktor:ktor-client-core:2.2.0:
exported: true
- ../ui/utils:
exported: true

També hi ha una forma abreujada:

module.yaml
dependencies:
- io.ktor:ktor-client-core:2.2.0: exported
- ../ui/utils: exported

Aquí tens un exemple d’una dependència només de compilació i exportada:

module.yaml
dependencies:
- io.ktor:ktor-client-core:2.2.0:
scope: compile-only
exported: true

Vegeu la documentació completa sobre Dependencies.