0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Nativescript】platforms内のファイルを`ns run(prepare) ***`実行時に自動で変更したい→フックスクリプトを導入した備忘録

Posted at

はじめに

Nativescript+Angularを用いてアプリ開発中、ns run android実行前にplatforms/android/gradle.propertiesの値を手動で変更していました。
ただ、package.json変更時など毎回手動変更はつらい(忘れる)ので、スクリプトで実行できるように調整しました。
その際に調べた内容・対応方法の備忘録になります。

環境

  • MacOS: 14.7.2
  • nodeバージョン: v16.14.2
  • Nativescript 8.5.3
  • Angular 15.2.10

今回追加するフックスクリプトの内容について ※参考程度に。飛ばしても問題ないはず

開発環境構築中にns run android実行した際、下記のエラーが発生しました。

FAILURE: Build failed with an exception.
* What went wrong:
Gradle build daemon disappeared unexpectedly (it may have been killed or may have crashed)

MacOSとの問題がありそうですが、下記のStackOverflow回答を参考にplatforms/android/gradle.propertiesorg.gradle.jvmargsの値を変更して解消しました。
Gradle build daemon disappeared unexpectedly (it may have been killed or may have crashed) while building Android project on Jenkins - Stack Overflow

今回は、上記の値を変更を行うスクリプトをns run(prepare) ***実行に動作するよう調整します。

フックスクリプトファイルを追加する

  • hooksplatformsと同階層にcustom-hooksディレクトリを追加します。
    • 追加する場所はお好きなところでOKです。
  • custom-hooks直下にhook type名でディレクトリを追加、そのディレクトリにスクリプトファイルを追加します。
    • typeはNativescriptのHooks Configuration Referenceを参照。
    • 今回はwebpackのコンパイル前に実行したいので、custom-hooks/before-prepare/modify-gradle-properties.jsを追加しました。
  • 以下は、スクリプトファイルの内容です。参考までに。
custom-hooks/before-prepare/modify-gradle-properties.js
const fs = require('fs');
const path = require('path');

module.exports = function ($logger, $projectData, hookArgs) {
    const gradlePropertiesPath = path.join($projectData.platformsDir, 'android', 'gradle.properties');

    if (fs.existsSync(gradlePropertiesPath)) {
        let content = fs.readFileSync(gradlePropertiesPath, 'utf8');

        // org.gradle.jvmargs を変更
        content = content.replace(
            /^org\.gradle\.jvmargs=.*$/m,
            'org.gradle.jvmargs=-Xmx15g -XX:+UseParallelGC'
        );

        fs.writeFileSync(gradlePropertiesPath, content, 'utf8');
        $logger.info('Updated gradle.properties: org.gradle.jvmargs modified');
    }
};

nativescript.config.tsにhooksを追加する

  • nativescript.config.tsにhooksを追加します。
  • hooks内には追加するhookのtypeとスクリプトファイルのパスを追加します。
nativescript.config.ts
import { NativeScriptConfig } from '@nativescript/core'

export default {
  ...省略
  hooks: [
    {
      type: 'before-prepare',
      script: 'custom-hooks/before-prepare/modify-gradle-properties.js'
    }
  ]
} as NativeScriptConfig

ここまで設定すれば、ns run(prepare) android実行時にフックスクリプトが実行されます🎉

おまけ

ChatGPTの回答でApp_Resources/Android/app.gradleの設定を追加する方法もあったのですが、自分の環境では動作しませんでした。
念のため参考に載せておきます。

App_Resources/Android/app.gradle に以下を追加すると、gradle.propertiesorg.gradle.jvmargs の値をカスタマイズできます。

App_Resources/Android/app.gradle
android {
    project.ext {
        set("org.gradle.jvmargs", "-Xmx4g -XX:+UseParallelGC")
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?