Skip to content

Commit 2ce2488

Browse files
farfromrefugclaude
andcommitted
feat(android): ship the app and plugin gradle files with the CLI
The gradle build scripts used to live only in the android runtime, so any fix to them had to wait for a runtime release. This bundles the app-level gradle files in `vendor/gradle-app` and copies them over the ones the runtime lays down when the platform is added, the same way the plugin build already uses `vendor/gradle-plugin`. - `vendor/gradle-app` holds `build.gradle`, `settings.gradle`, `app/build.gradle`, `app/gradle.properties` and the `app/gradle-helpers`. They are copied on top of the runtime files in `createProject`, so the runtime keeps providing everything that is not part of the overlay. - `--no-override-runtime-gradle-files` opts out and keeps the runtime files. - The directory the files come from is resolved through `getGradleFilesPath`, which already understands an `android.gradleFilesPackageName` config key so the files can later be provided by an npm package instead of the bundled copy. - The CLI now interpolates `__PACKAGE__` (android namespace) and `USER_PROJECT_ROOT` in the copied files, and honours `android.gradleVersion` by rewriting the gradle wrapper. - `--gradleArgs` becomes an array option, so it can be passed several times, and a single value may hold several space separated arguments. Arguments listed in `android.gradleArgs` are passed too, before the command line ones. Both app and plugin builds go through the same merge. - Both app and plugin gradle invocations now get `-PcompileSdk`, `-PtargetSdk`, `-PbuildToolsVersion`, `-PgenerateTypings`, `-PprojectRoot` and `-PappBuildPath` (the last two also as `-D` so `settings.gradle` can read them before project properties exist). - A debug build is signed when the `--key-store-*` options are passed, which is needed for system app builds. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 2f9f2e0 commit 2ce2488

29 files changed

Lines changed: 2489 additions & 148 deletions

docs/man_pages/project/testing/build-android.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ General | `$ ns build android [--compileSdk <API Level>] [--key-store-path <File
3434
* `--env.sourceMap` - creates inline source maps.
3535
* `--env.hiddenSourceMap` - creates sources maps in the root folder (useful for Crashlytics usage with bundled app in release).
3636
* `--aab` - Specifies that the build will produce an Android App Bundle(`.aab`) file.
37+
* `--gradleArgs` - Passes additional arguments to gradle. Can be passed multiple times, and a single value may hold several space separated arguments. Use the `=` form so the value is not mistaken for another flag, for example `--gradleArgs="-PsomeProperty=value"`. Arguments listed under `android.gradleArgs` in `nativescript.config` are passed too, before these ones.
38+
* `--no-override-runtime-gradle-files` - If set, keeps the gradle files coming from the android runtime instead of the ones shipped with the CLI.
3739
* `--force` - If set, skips the application compatibility checks and forces `npm i` to ensure all dependencies are installed. Otherwise, the command will check the application compatibility with the current CLI version and could fail requiring `ns migrate`.
3840
* `--path <Directory>` - Specifies the directory that contains the project. If not set, the project is searched for in the current directory and all directories above it.
3941

docs/man_pages/project/testing/debug-android.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Attach the debug tools to a running app in the native emulator | `$ ns debug and
3838
* `--env.sourceMap` - creates inline source maps.
3939
* `--env.hiddenSourceMap` - creates sources maps in the root folder (useful for Crashlytics usage with bundled app in release).
4040
* `--aab` - Specifies that the command will produce and deploy an Android App Bundle.
41+
* `--gradleArgs` - Passes additional arguments to gradle. Can be passed multiple times, and a single value may hold several space separated arguments. Use the `=` form so the value is not mistaken for another flag, for example `--gradleArgs="-PsomeProperty=value"`. Arguments listed under `android.gradleArgs` in `nativescript.config` are passed too, before these ones.
42+
* `--no-override-runtime-gradle-files` - If set, keeps the gradle files coming from the android runtime instead of the ones shipped with the CLI.
4143
* `--force` - If set, skips the application compatibility checks and forces `npm i` to ensure all dependencies are installed. Otherwise, the command will check the application compatibility with the current CLI version and could fail requiring `ns migrate`.
4244

4345
<% if(isHtml) { %>

docs/man_pages/project/testing/run-android.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Start a default emulator if none are running, or run application on all connecte
4343
* `--env.sourceMap` - creates inline source maps.
4444
* `--env.hiddenSourceMap` - creates sources maps in the root folder (useful for Crashlytics usage with bundled app in release).
4545
* `--aab` - Specifies that the command will produce and deploy an Android App Bundle.
46+
* `--gradleArgs` - Passes additional arguments to gradle. Can be passed multiple times, and a single value may hold several space separated arguments. Use the `=` form so the value is not mistaken for another flag, for example `--gradleArgs="-PsomeProperty=value"`. Arguments listed under `android.gradleArgs` in `nativescript.config` are passed too, before these ones.
47+
* `--no-override-runtime-gradle-files` - If set, keeps the gradle files coming from the android runtime instead of the ones shipped with the CLI.
4648
* `--force` - If set, skips the application compatibility checks and forces `npm i` to ensure all dependencies are installed. Otherwise, the command will check the application compatibility with the current CLI version and could fail requiring `ns migrate`.
4749

4850
<% if(isHtml) { %>

lib/contracts/project-data.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,6 @@ export abstract class ProjectData {
8989
abstract getAppResourcesDirectoryPath(projectDir?: string): string;
9090

9191
abstract getAppResourcesRelativeDirectoryPath(): string;
92+
93+
abstract getBuildRelativeDirectoryPath(): string;
9294
}

lib/data/build-data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class AndroidBuildData extends BuildData {
5252
public keyStorePassword: string;
5353
public androidBundle: boolean;
5454
public gradlePath: string;
55-
public gradleArgs: string;
55+
public gradleArgs: string[];
5656
public hostProjectPath: string;
5757

5858
constructor(projectDir: string, platform: string, data: any) {

lib/declarations.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,13 @@ interface IEmbedOptions {
572572

573573
interface IAndroidOptions extends IEmbedOptions {
574574
gradlePath: string;
575-
gradleArgs: string;
575+
gradleArgs: string[];
576+
/**
577+
* When true (the default) the gradle files bundled with the CLI are copied
578+
* over the ones shipped by the android runtime. Pass `--no-override-runtime-gradle-files`
579+
* to keep the runtime files untouched.
580+
*/
581+
overrideRuntimeGradleFiles: boolean;
576582
}
577583

578584
interface IIOSOptions extends IEmbedOptions {}

lib/definitions/android-plugin-migrator.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface IAndroidBuildOptions {
1111
aarOutputDir: string;
1212
tempPluginDirPath: string;
1313
gradlePath?: string;
14-
gradleArgs?: string;
14+
gradleArgs?: string[];
1515
}
1616

1717
interface IAndroidPluginBuildService {
@@ -48,5 +48,5 @@ interface IBuildAndroidPluginData extends Partial<IProjectDir> {
4848
/**
4949
* Optional custom Gradle arguments.
5050
*/
51-
gradleArgs?: string;
51+
gradleArgs?: string[];
5252
}

lib/definitions/build.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ interface IAndroidBuildData
3232
IAndroidSigningData,
3333
IHasAndroidBundle {
3434
gradlePath?: string;
35-
gradleArgs?: string;
35+
gradleArgs?: string[];
3636
}
3737

3838
interface IAndroidSigningData {

lib/definitions/gradle.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ interface IGradleBuildService {
3030
interface IGradleBuildArgsService {
3131
getBuildTaskArgs(buildData: IAndroidBuildData): Promise<string[]>;
3232
getCleanTaskArgs(buildData: IAndroidBuildData): string[];
33+
getBuildLoggingArgs(): string[];
3334
}

lib/definitions/project.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,25 @@ interface INsConfigAndroid extends INsConfigPlaform {
179179
* Custom runtime package name
180180
*/
181181
runtimePackageName?: string;
182+
183+
/**
184+
* Pin the gradle wrapper to a specific gradle version, overriding the one
185+
* shipped by the android runtime.
186+
*/
187+
gradleVersion?: string;
188+
189+
/**
190+
* Additional arguments passed to every gradle invocation (app and plugin
191+
* builds). Merged with the ones passed on the command line through
192+
* `--gradleArgs`.
193+
*/
194+
gradleArgs?: string[];
195+
196+
/**
197+
* Package providing the gradle files copied over the ones shipped by the
198+
* android runtime. Defaults to the files bundled with the CLI.
199+
*/
200+
gradleFilesPackageName?: string;
182201
}
183202

184203
interface INsConfigHooks {

0 commit comments

Comments
 (0)