NativeScript / android

NativeScript for Android using v8
https://docs.nativescript.org/guide/android-marshalling
Apache License 2.0
524 stars 135 forks source link

NativeScript 8.5.3 Android build failing: Could not find method task() for arguments [replaceSettings, settings_9f0ihdbhsry4akdttkhpjzk68$_run_closure1@860396b] on settings 'proto' of type org.gradle.initialization.DefaultSettings. #1763

Closed KevSeenan closed 1 year ago

KevSeenan commented 1 year ago

Environment Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

Describe the bug

ns run android gets to Gradle build and fails. Below is the output form the terminal:

Gradle build...
         + applying user-defined configuration from /<path>/app/App_Resources/Android/settings.gradle

FAILURE: Build failed with an exception.

* Where:
Script '/<path>/app/App_Resources/Android/settings.gradle' line: 3

* What went wrong:
A problem occurred evaluating script.
> Could not find method task() for arguments [replaceSettings, settings_9f0ihdbhsry4akdttkhpjzk68$_run_closure1@860396b] on settings 'proto' of type org.gradle.initialization.DefaultSettings.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org/

BUILD FAILED in 510ms
Unable to apply changes on device: emulator-5554. Error is: Command ./gradlew failed with exit code 1.

Here is the settings.gradle file:

import groovy.json.JsonSlurper

task replaceSettings {
    description "Replaces configuration settings."
    def jsonSlurper = new JsonSlurper()
    def pathToSettingsJson

    if (project.hasProperty("appResourcesPath")) {
        pathToSettingsJson = "$project.appResourcesPath/Android/settings.json";
    } else {
        pathToSettingsJson = "$rootDir/../../app/App_Resources/Android/settings.json";
    }

    def settingsJsonFile = file(pathToSettingsJson);
    def settingsResolvedPath = settingsJsonFile.getAbsolutePath();

    if(settingsJsonFile.exists())
    {
        println "\t Applying settings from $settingsResolvedPath"
        String settingsGradleTemplate = """android {
            defaultConfig {
                applicationId = "__appId__"

                if (__minSdkVersion__) {
                    minSdkVersion = __minSdkVersion__
                }

                if (__targetSdkVersion__) {
                    targetSdkVersion = __targetSdkVersion__
                }
            }
        }"""

        def settingsJsonContent = settingsJsonFile.getText("UTF-8");
        def settingsMap = jsonSlurper.parseText(settingsJsonContent);

        for ( setting in settingsMap ) {
            def placeholder = "__${setting.key}__";
            def settingValue = setting.value;

            if (settingValue == null) {
                settingValue = false
            }

            settingsGradleTemplate = settingsGradleTemplate.replaceAll( placeholder, settingValue as String);
        }

        new File( "$rootDir/temp_setting.gradle" ).write( settingsGradleTemplate, 'UTF-8');
        apply from: "$rootDir/temp_setting.gradle";
    }
}

Here is the app.gradle file:

android {
  defaultConfig {
    minSdkVersion 27
    generatedDensities = []
    targetSdkVersion 33
  }
  aaptOptions {
    additionalParameters "--no-version-vectors"
  }
}

def settingsGradlePath

if(project.hasProperty("appResourcesPath")){
    settingsGradlePath = "$project.appResourcesPath/Android/settings.gradle";
} else {
    settingsGradlePath = "$rootDir/../../app/App_Resources/Android/settings.gradle";
}

def settingsGradleFile = new File(settingsGradlePath);

if(settingsGradleFile.exists())
{
    apply from: settingsGradleFile;
}

Here is my package.json -

  "dependencies": {
    "@nativescript-community/https": "4.1.0",
    "@nativescript-community/ui-material-tabs": "^7.0.37",
    "@nativescript/core": "8.5.3",
    "@nativescript/email": "^2.0.5",
    "@nativescript/firebase": "^11.1.3",
    "@nativescript/secure-storage": "^3.0.0",
    "@nstudio/nativescript-barcodescanner": "^5.0.1",
    "@nstudio/nativescript-filterable-listpicker": "^3.0.0",
    "@nstudio/root-detection": "^2.0.1",
    "@triniwiz/nativescript-socketio": "^5.0.1",
    "@triniwiz/nativescript-toasty": "^4.1.3",
    "@types/aes-js": "^3.1.1",
    "aes-js": "^3.1.2",
    "date-fns": "^2.15.0",
    "electron": "^25.0.0",
    "js-sha256": "^0.9.0",
    "nativescript-carousel": "^7.0.1",
    "nativescript-exit": "^1.0.1",
    "nativescript-fonticon": "^2.0.2",
    "nativescript-modal-datetimepicker": "^2.1.5",
    "nativescript-phone": "^3.0.2",
    "nativescript-picker": "^2.1.2",
    "nativescript-socketio": "^3.3.1",
    "nativescript-toasty": "3.0.0-alpha.2",
    "nativescript-videoplayer": "^5.0.1",
    "nativescript-vue": "^2.7.1",
    "nativescript-vue-shadow": "^0.1.0",
    "vuex": "^3.6.2"
  },
  "devDependencies": {
    "@babel/core": "^7.11.1",
    "@babel/preset-env": "^7.11.0",
    "@nativescript/android": "~8.5.0",
    "@nativescript/ios": "8.5.2",
    "@nativescript/types": "^8.0.1",
    "@nativescript/webpack": "~5.0.13",
    "@types/node": "20.2.5",
    "@typescript-eslint/eslint-plugin": "^5.51.0",
    "@typescript-eslint/parser": "^5.51.0",
    "babel-loader": "9.1.2",
    "eslint": "8.41.0",
    "eslint-config-prettier": "^8.2.0",
    "eslint-plugin-html": "7.1.0",
    "eslint-plugin-prettier": "^4.0.0",
    "eslint-plugin-vue": "9.14.1",
    "hard-source-webpack-plugin": "^0.13.1",
    "nativescript-vue-template-compiler": "^2.7.1",
    "sass": "1.62.1",
    "typescript": "^5.0.4",
    "vue": "^2.7.14",
    "vue-loader": "^17.1.2"
  }

I have tried upgrading to latest version of node (18.16.0), upgrading Android Studio (Flamingo), removing the gradle file from my home directory so it re-installs on ns run android but I'm still getting the error. Reverting to older versions of the run time / cli also doesn't help.

Any help with this would be greatly appreciated. Thanks.

rigor789 commented 1 year ago

I believe you can just delete the settings.gradle that's an old artifact from a previous version that's no longer used in that form.

I recommend copying over the defaults from a fresh project:

KevSeenan commented 1 year ago

Thanks @rigor789 for the quick reply.

I have tried what you advised and now get a different error:

Gradle build...

Script '//node_modules/@nativescript/firebase/platforms/android/include.gradle' line: 96 A problem occurred evaluating script. Plugin with id 'com.google.gms.google-services' not found.

Have you seen this before? Does this mean I need to change firebase plugin?

rigor789 commented 1 year ago

I have seen that before, but I can't recall the solution (other than switching/migrating to the new firebase-core plugins). Perhaps there's a solution somewhere in the old firebase repo.

Actually, found this: https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/1882#issuecomment-1165777314

But note that the old firebase plugin is deprecated, so it's best to migrate to the new one instead.

KevSeenan commented 1 year ago

Will look into the post and migrating.

Thanks again for the speedy response.

Have a good day!

KevSeenan commented 1 year ago

Update

Migrating worked so closing the issue.

nguyentrancong commented 8 months ago

I followed him https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/1882#issuecomment-1165777314/ and It works with me