Skip to content

Commit 7dedb57

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 8a7c3ec commit 7dedb57

26 files changed

Lines changed: 2469 additions & 143 deletions

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ General | `$ ns build android [--compileSdk <API Level>] [--key-store-path <File
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.
3737
* `--gradleFlavor` - Builds the given product flavor, when the app declares any. `--gradleFlavor foo` runs the `assembleFooDebug`/`assembleFooRelease` gradle task instead of `assembleDebug`/`assembleRelease`.
38+
* `--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.
39+
* `--no-override-runtime-gradle-files` - If set, keeps the gradle files coming from the android runtime instead of the ones shipped with the CLI.
3840
* `--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`.
3941
* `--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.
4042

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Attach the debug tools to a running app in the native emulator | `$ ns debug and
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.
4141
* `--gradleFlavor` - Builds the given product flavor, when the app declares any. `--gradleFlavor foo` runs the `assembleFooDebug`/`assembleFooRelease` gradle task instead of `assembleDebug`/`assembleRelease`.
42+
* `--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.
43+
* `--no-override-runtime-gradle-files` - If set, keeps the gradle files coming from the android runtime instead of the ones shipped with the CLI.
4244
* `--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`.
4345

4446
<% if(isHtml) { %>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Start a default emulator if none are running, or run application on all connecte
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.
4646
* `--gradleFlavor` - Builds the given product flavor, when the app declares any. `--gradleFlavor foo` runs the `assembleFooDebug`/`assembleFooRelease` gradle task instead of `assembleDebug`/`assembleRelease`.
47+
* `--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.
48+
* `--no-override-runtime-gradle-files` - If set, keeps the gradle files coming from the android runtime instead of the ones shipped with the CLI.
4749
* `--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`.
4850

4951
<% if(isHtml) { %>

lib/data/build-data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class AndroidBuildData extends BuildData {
5353
public androidBundle: boolean;
5454
public gradleFlavor: string;
5555
public gradlePath: string;
56-
public gradleArgs: string;
56+
public gradleArgs: string[];
5757
public hostProjectPath: string;
5858

5959
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
@@ -578,7 +578,13 @@ interface IAndroidOptions extends IEmbedOptions {
578578
*/
579579
gradleFlavor: string;
580580
gradlePath: string;
581-
gradleArgs: string;
581+
gradleArgs: string[];
582+
/**
583+
* When true (the default) the gradle files bundled with the CLI are copied
584+
* over the ones shipped by the android runtime. Pass `--no-override-runtime-gradle-files`
585+
* to keep the runtime files untouched.
586+
*/
587+
overrideRuntimeGradleFiles: boolean;
582588
}
583589

584590
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
@@ -33,7 +33,7 @@ interface IAndroidBuildData
3333
IHasAndroidBundle {
3434
gradleFlavor?: string;
3535
gradlePath?: string;
36-
gradleArgs?: string;
36+
gradleArgs?: string[];
3737
}
3838

3939
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 {

lib/options.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,12 @@ export class Options {
221221
},
222222
gradleFlavor: { type: OptionType.String, hasSensitiveValue: false },
223223
gradlePath: { type: OptionType.String, hasSensitiveValue: false },
224-
gradleArgs: { type: OptionType.String, hasSensitiveValue: false },
224+
gradleArgs: { type: OptionType.Array, hasSensitiveValue: false },
225+
overrideRuntimeGradleFiles: {
226+
type: OptionType.Boolean,
227+
default: true,
228+
hasSensitiveValue: false,
229+
},
225230
hostProjectPath: { type: OptionType.String, hasSensitiveValue: false },
226231
hostProjectModuleName: {
227232
type: OptionType.String,

0 commit comments

Comments
 (0)