Skip to content

Commit ba25f09

Browse files
committed
feat(gradle-api)!: settle the API before a stable release
- nativescript.projectDir is now projectRoot, so it can't be confused with Gradle's own projectDir in include.gradle. - PluginInfo no longer exposes aarName/aar; artifacts is the supported way to use another plugin's binaries. - self is null outside a plugin aar build, without needing facts. - Facts require dependenciesJson.sha256 and minReaderSchemaVersion, and are read through providers.fileContents so they are configuration inputs. - has() capabilities follow a documented area.feature naming convention. - A functional test puts two library versions in one build and checks the higher one wins; the README warns against root classpaths, which would pin a version for every plugin.
1 parent 8d3ca37 commit ba25f09

24 files changed

Lines changed: 423 additions & 204 deletions

File tree

‎gradle-api/README.md‎

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ Pin the **lowest** version that has what you need, never a range or `+`. Every p
6464
the library to the same classpath and Gradle keeps the highest version, which is always backwards
6565
compatible.
6666

67+
Add it **only** in `platforms/android/buildscript.gradle`, never on a root classpath: not in a
68+
plugin's `rootbuildscript.gradle`, not in the app's `App_Resources/Android/rootbuildscript.gradle`,
69+
and not in the root `build.gradle` of `platforms/android`. Gradle looks up classes in the root
70+
project's class loader before the app's, so a version on the root classpath wins over every
71+
plugin's `buildscript.gradle`, even one that asks for a newer version, and every plugin in the app
72+
gets that older version.
73+
6774
**2. If you use `nativescript` in the app build**, apply the library at the top of
6875
`platforms/android/include.gradle`. The whole file runs in the app build, including its
6976
`repositories {}` and `dependencies {}` blocks; only code inside the `tempBuild` check of
@@ -124,10 +131,16 @@ never put a `buildscript {}` block in `include.gradle`.
124131
| What your plugin needs | What it writes |
125132
|---|---|
126133
| Another plugin it lists in its `package.json` `dependencies` | Nothing: in its own `.aar` build the CLI adds that plugin's `.aar`/`.jar` files as `implementation`, and in the app build every plugin is linked. |
127-
| A plugin it doesn't list (typically `@nativescript/core`, usually a peer) | `compileOnly nativescript.plugin("...").artifacts` inside the `tempBuild` check, as in [the block above](#your-plugins-own-aar-build). Not `implementation`: Android's library plugin rejects a local `.aar` as `implementation` when it packages yours ("Direct local .aar file dependencies are not supported when building an AAR"), and the app links that plugin anyway. |
134+
| A plugin it doesn't list (typically `@nativescript/core`, usually a peer) | `compileOnly nativescript.plugin("...").artifacts` inside the `tempBuild` check, as in [the block above](#your-plugins-own-aar-build), for the `.aar`/`.jar` files that plugin **ships prebuilt** in its package. Not `implementation`: Android's library plugin rejects a local `.aar` as `implementation` when it packages yours ("Direct local .aar file dependencies are not supported when building an AAR"), and the app links that plugin anyway. |
128135
| Its own prebuilt `.jar`/`.aar` | Nothing, if they're in `platforms/android` or `platforms/android/libs`: the app build links them. |
129136
| A Maven library | The usual `implementation "group:artifact:version"`. |
130137

138+
The CLI builds plugins' `.aar`s one after the other: `@nativescript/core` first, then every plugin
139+
after the plugins it lists in its `dependencies`. The order between unrelated plugins is not
140+
defined, and `artifacts` only includes an `.aar` once it exists, so on a clean build
141+
`compileOnly nativescript.plugin(X).artifacts` can miss an `.aar` the CLI builds from X's sources.
142+
If you need such an `.aar`, list X in your `dependencies`. `@nativescript/core` is always fine.
143+
131144
Never write `implementation files(...)` or `flatDir` pointing into another package or into
132145
`node_modules`: the CLI decides where plugins' files are (see
133146
[`pluginBuildLocation`](#for-app-developers-building-plugins-in-platformsandroid-opt-in)), and the
@@ -152,9 +165,9 @@ Known limits of the CLI's and runtime's templates today:
152165
| `nativescript.apiLevel` | No | The API level of the library on the classpath. |
153166
| `nativescript.pluginBuild` | No | `true` in the CLI's temporary build of some plugin's `.aar`. |
154167
| `nativescript.isBuildingPlugin(String)` | No | Whether the plugin build is building that npm package. |
155-
| `nativescript.has(String)` | Yes | Whether the CLI guarantees a capability. |
156-
| `nativescript.self` | Yes | In a plugin build, the plugin being built (see below). `null` in the app build. |
157-
| `nativescript.projectDir` | Yes | The NativeScript project root (the folder with the app's `package.json`). |
168+
| `nativescript.self` | In a plugin build | In a plugin build, the plugin being built (see below). `null` in the app build, without reading the facts file. |
169+
| `nativescript.has(String)` | Yes | Whether the CLI guarantees a capability (see [Capabilities](#capabilities)). |
170+
| `nativescript.projectRoot` | Yes | The NativeScript project root (the folder with the app's `package.json`; `project.root` in the facts file). |
158171
| `nativescript.appDir` | Yes | The app source folder (`appPath` in `nativescript.config`). |
159172
| `nativescript.appResourcesDir` | Yes | `App_Resources`. |
160173
| `nativescript.plugin(String)` | Yes | A plugin the CLI lists, by npm name. Throws if the app doesn't have it. |
@@ -163,7 +176,8 @@ Known limits of the CLI's and runtime's templates today:
163176
| `nativescript.findPackage(String)` | Yes | Same, or `null`. |
164177

165178
"Needs facts" members read the facts file CLI 9.2+ writes, and throw one of the [errors](#errors)
166-
below when it is missing or out of date. Without it, only the first three work.
179+
below when it is missing or out of date. Without it, only the first three work, and `self` in the
180+
app build.
167181

168182
A plugin (`PluginInfo`):
169183

@@ -172,8 +186,7 @@ A plugin (`PluginInfo`):
172186
| `name` | The npm package name. |
173187
| `packageDir` | The installed package. Treat it as read-only. |
174188
| `nativeDir` | Where Gradle reads the plugin's Android files from. |
175-
| `artifacts` | Every `.aar` and `.jar` under `nativeDir`, as a lazy `FileCollection`. |
176-
| `aarName`, `aar` | The name and location of the `.aar` the CLI builds from the plugin's sources (the file only exists if it has sources). |
189+
| `artifacts` | Every `.aar` and `.jar` under `nativeDir`, as a lazy `FileCollection`. It includes the `.aar` the CLI builds from the plugin's sources once that exists (see [Depending on other plugins](#depending-on-other-plugins)). |
177190
| `options` | Per-plugin options. Reserved; empty for now. |
178191

179192
`plugin` / `findPlugin` only know the plugins the CLI lists: packages that declare Android support
@@ -198,6 +211,12 @@ if (!nativescript.pluginBuild) {
198211
that don't hoist (pnpm's default layout) may not resolve packages your app doesn't depend on
199212
directly.
200213

214+
### Capabilities
215+
216+
`has(String)` answers from the facts file's `capabilities` list, which is empty today. Capability
217+
names are dotted lowercase `area.feature` (for example `build.abis`). Once a CLI version lists a
218+
capability, every later CLI lists it with the same meaning: names are only ever added.
219+
201220
## Errors
202221

203222
The library reads the facts file on the first use of a member that needs it, and stops the build
@@ -220,6 +239,18 @@ with one of these messages when it can't:
220239
(Or, if `dependencies.json` is gone: "There is no `<…>/dependencies.json` next to the
221240
NativeScript facts file `<…>`." followed by the same advice.)
222241

242+
- **Library too old for the facts file**, because the file's `minReaderSchemaVersion` is above the
243+
schema version this library supports:
244+
245+
> The NativeScript facts file `<…>` needs org.nativescript:gradle-api supporting facts schema N,
246+
> but this version of the library supports up to M. Update the plugins of this app that use
247+
> org.nativescript:gradle-api: any one that pins a newer version of the library is enough.
248+
249+
- **Invalid facts file** ("Invalid NativeScript facts file `<…>`: …"), when a required field such as
250+
`minReaderSchemaVersion` or `dependenciesJson.sha256` is missing or has the wrong type, or
251+
`minReaderSchemaVersion` is greater than `schemaVersion`. The CLI
252+
never writes such a file; prepare again.
253+
223254
## Recipes
224255

225256
**Compile against another plugin's Android library (e.g. core's widgets) in your own `.aar` build:**
@@ -251,7 +282,7 @@ repositories {
251282
| | CLI 9.1 and older | CLI 9.2+ |
252283
|---|---|---|
253284
| Plugin not using the library | Unchanged | Its own `buildscript.gradle` also runs in its own `.aar` build |
254-
| Plugin using the library, app build | `apiLevel`, `pluginBuild` and `isBuildingPlugin` work; anything else stops the build with the [no facts file](#errors) error | Facts from the CLI |
285+
| Plugin using the library, app build | `apiLevel`, `pluginBuild`, `isBuildingPlugin` and `self` (null) work; anything else stops the build with the [no facts file](#errors) error | Facts from the CLI |
255286
| Plugin using the library, its own `.aar` build | Library not loaded, so the [check](#your-plugins-own-aar-build) stops the build with the plugin's message (or, if a plugin it depends on applied the library, the [no facts file](#errors) error) | Library loaded and applied, facts from the CLI |
256287
| Project prepared again by an older CLI after 9.2 | The [out-of-date facts file](#errors) error | |
257288

@@ -321,6 +352,7 @@ built. The schema is [`schema/nativescript-gradle.schema.json`](schema/nativescr
321352
```json
322353
{
323354
"schemaVersion": 1,
355+
"minReaderSchemaVersion": 1,
324356
"cli": { "version": "9.2.0" },
325357
"runtime": { "name": "@nativescript/android", "version": "9.0.0" },
326358
"capabilities": [],
@@ -341,6 +373,10 @@ built. The schema is [`schema/nativescript-gradle.schema.json`](schema/nativescr
341373
```
342374

343375
- Paths are relative to the file, with forward slashes, so the folder can move.
376+
- `minReaderSchemaVersion` is the lowest `schemaVersion` a reader must understand to use the file,
377+
never above the file's own `schemaVersion`.
378+
A library that supports only older schema versions refuses it (see [Errors](#errors)) instead of
379+
reading part of it. CLI 9.2 writes `1`.
344380
- `dependenciesJson.sha256` is the hash of the `dependencies.json` the CLI wrote next to it. A CLI
345381
that doesn't know about this file (9.1 and older, e.g. a global one used by mistake) still
346382
rewrites `dependencies.json` but leaves this file behind; the library notices the mismatch and
@@ -354,8 +390,10 @@ built. The schema is [`schema/nativescript-gradle.schema.json`](schema/nativescr
354390
These are what keep old plugins working on new CLIs, and new plugins working on CLIs from 9.2 on.
355391

356392
**Facts file:** fields are only added. A field never changes meaning and is never removed.
357-
`schemaVersion` goes up when fields are added; readers accept any version and read what they know.
358-
Paths are relative. Anything a newer CLI does differently is expressed as data in the file, not as a
393+
`schemaVersion` goes up when fields are added; readers accept any version and read what they know,
394+
as long as it isn't below the file's `minReaderSchemaVersion`. A CLI raises `minReaderSchemaVersion`
395+
only for a change that old readers would get wrong if they ignored it, which should be never: it is
396+
the one way to tell libraries already published "you can't read this". Paths are relative. Anything a newer CLI does differently is expressed as data in the file, not as a
359397
rule readers must know. A facts file that doesn't match the `dependencies.json` next to it is an
360398
error, never used.
361399

@@ -365,9 +403,12 @@ the CLI checks the facts file or a capability before relying on it. The library
365403

366404
## Releasing
367405

368-
Builds and tests run in `.github/workflows/gradle_api.yml`. Pushing a tag `gradle-api-v<version>`
369-
that matches `VERSION_NAME` in `gradle.properties` publishes to Maven Central, from the protected
370-
`maven-central` GitHub environment. It needs these environment secrets:
406+
Builds and tests run in `.github/workflows/gradle_api.yml`, on Linux (unit and functional tests)
407+
and Windows (unit tests). Pushing a tag `gradle-api-v<version>` on a commit of `main` that matches
408+
`VERSION_NAME` in `gradle.properties` (not a `-SNAPSHOT`) publishes to Maven Central and releases
409+
it at once, from the protected `maven-central` GitHub environment; the workflow refuses a tag on
410+
any other commit. Give that environment required reviewers, since a Maven Central release can't be
411+
undone. It needs these environment secrets:
371412

372413
- `MAVEN_CENTRAL_USERNAME`, `MAVEN_CENTRAL_PASSWORD`: a Central Portal user token with publishing
373414
rights on the `org.nativescript` namespace.
@@ -386,3 +427,15 @@ cd gradle-api
386427
./gradlew check # unit + functional tests (AGP tests need ANDROID_HOME)
387428
./gradlew publishToMavenLocal # try it from a local app
388429
```
430+
431+
### CLI release notes
432+
433+
CLI changes from this work that affect plugins that don't use the library, for the CLI 9.2 release
434+
notes:
435+
436+
- **A plugin's own `platforms/android/buildscript.gradle` now also runs in that plugin's own `.aar`
437+
build.** Before 9.2 it ran only in the app build and in the `.aar` builds of plugins that depend
438+
on it. A plugin whose `buildscript.gradle` adds classpath dependencies or repositories now gets
439+
them in its own build too. Check that it doesn't assume the app build (for example by reading
440+
properties only the app's Gradle project sets); `project.hasProperty("tempBuild")` tells the two
441+
apart.

‎gradle-api/build.gradle.kts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ val functionalTestTask = tasks.register<Test>("functionalTest") {
113113

114114
systemProperty("nativescript.testRepo", testRepo.get().asFile.absolutePath)
115115
systemProperty("nativescript.libraryVersion", providers.gradleProperty("VERSION_NAME").get())
116+
systemProperty("nativescript.libraryJar", tasks.jar.get().archiveFile.get().asFile.absolutePath)
116117
systemProperty("nativescript.samplesDir", file("samples").absolutePath)
117118
systemProperty("nativescript.vendorGradlePluginDir", file("../vendor/gradle-plugin").absolutePath)
118119
val gradleVersions = providers.gradleProperty("functionalTestGradleVersions").orElse("7.5.1,8.14.3,9.8.1")

‎gradle-api/schema/nativescript-gradle.schema.json‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
"title": "NativeScript Gradle facts",
55
"description": "Written by the NativeScript CLI at prepare time to platforms/android/nativescript-gradle.json and read by org.nativescript:gradle-api. Fields are only ever added: readers must ignore unknown fields, and a field never changes meaning. Every path is relative to the directory containing this file and uses forward slashes.",
66
"type": "object",
7-
"required": ["schemaVersion", "dependenciesJson", "project", "plugins"],
7+
"required": ["schemaVersion", "minReaderSchemaVersion", "dependenciesJson", "project", "plugins"],
88
"properties": {
99
"schemaVersion": {
10-
"description": "Bumped only when a field is added. Readers accept any version and read the fields they know.",
10+
"description": "Bumped only when a field is added. Readers accept any version that minReaderSchemaVersion allows them to, and read the fields they know.",
11+
"type": "integer",
12+
"minimum": 1
13+
},
14+
"minReaderSchemaVersion": {
15+
"description": "The lowest schemaVersion a reader must support to read this file; never greater than schemaVersion. A reader that supports only a lower schema version must refuse the file, rather than read it partially.",
1116
"type": "integer",
1217
"minimum": 1
1318
},

0 commit comments

Comments
 (0)