Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include.gradle without default "android { productFlavors {} }" setting crashes during build #542

Closed
petekanev opened this issue Aug 22, 2016 · 10 comments
Assignees
Labels

Comments

@petekanev
Copy link
Contributor

From @roblav96 on August 22, 2016 17:57

Building a plugin using the nativescript-plugin-seed,

I get this gradle build error:

Successfully prepared plugin nativescript-onesignal for android.
Successfully prepared plugin tns-core-modules for android.
Successfully prepared plugin tns-core-modules-widgets for android.
Project successfully prepared (android)

:config phase:  createDefaultIncludeFiles
    +found plugins: nativescript-onesignal

FAILURE: Build failed with an exception.

* Where:
Build file '/Volumes/OSX HD/Projects/nativescript-onesignal/demo/platforms/android/build.gradle' line: 348

* What went wrong:
A problem occurred evaluating root project 'demo'.
> String index out of range: -1

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

BUILD FAILED

Total time: 2.673 secs
Command /Volumes/OSX HD/Projects/nativescript-onesignal/demo/platforms/android/gradlew failed with exit code 1

This is build.gradle line: 348:

    if (indexEnd != -1)
    {
        // full content of productFlavors { ... } -> the substring is parenthesis to parenthesis -> { ... }
      // >>>> THIS IS LINE 348 RIGHT HERE! >>>>
      def oldProductFlavorsText = content.substring(indexStart, indexEnd + 1);
      // <<<< THIS IS LINE 348 RIGHT HERE! <<<<
      def newProductFlavorsContent = updateProductFlavorsContent(flavor, dimension, oldProductFlavorsText);

      return content.replace(oldProductFlavorsText, newProductFlavorsContent);
    }

In my plugin/platforms/android/include.gradle

dependencies {
    compile "com.onesignal:OneSignal:3.+@aar"
    compile "com.google.android.gms:play-services-gcm:+"
    compile "com.google.android.gms:play-services-location:+"
}

In my demo/app/App_Resources/app.gradle

android {
    defaultConfig {
        applicationId "org.nativescript.demo"
        manifestPlaceholders = [
            manifestApplicationId: "${applicationId}",
            onesignal_app_id: "b2f7f966-d8cc-11e4-bed1-df8f05be55ba",
            onesignal_google_project_number: "703322744261"
        ]
    }
}

Copied from original issue: NativeScript/NativeScript#2613

@petekanev
Copy link
Contributor Author

petekanev commented Aug 23, 2016

2017 July edit: no action was taken regarding rewriting of include.gradles, as pointed out in #542 (comment)

Hey @roblav96, there is indeed a flaw in our include.gradle rewriting logic that doesn't test whether the provided configuration file contains a android { productFlavors {} } setting. The fix will make it in the next release. In the meantime you can work around the bug by specifying productFlavors configuration, so that the file be parsed without error. Here's a sample configuration:

android {
    productFlavors {
        "nativescript-onesignal" {
            dimension "nativescript-onesignal"
        }
    }
}

dependencies {
    compile "com.onesignal:OneSignal:3.+@aar"
    compile "com.google.android.gms:play-services-gcm:+"
    compile "com.google.android.gms:play-services-location:+"
} 

Let us know if you need additional help setting up!

@roblav96
Copy link

OK awesome! I'll test it out and see if it works. Thank you!

@roblav96
Copy link

This is a good workaround for the moment. Thanks :D

@petekanev petekanev changed the title [Android] build.gradle String index out of range: -1 include.gradle without "android { productFlavors {} }" setting crashes during build Aug 24, 2016
@petekanev petekanev changed the title include.gradle without "android { productFlavors {} }" setting crashes during build include.gradle without default "android { productFlavors {} }" setting crashes during build Sep 1, 2016
@petekanev
Copy link
Contributor Author

@roblav96 after a short discussion we've come to the conclusion that it is not our responsibility to make the include.gradles compliant, as the cases that need to be covered are endless, and taking care of all of them will not be efficient without a groovy language parser (and there does not appear to be such yet, not publicly available at least).

Follow the guidelines in our docs http://docs.nativescript.org/plugins/plugins#includegradle-specification to avoid unexpected behavior.

@roblav96
Copy link

roblav96 commented Sep 1, 2016

@Pip3r4o I couldn't agree with you more!

The only reason I didn't do this in the first place was because I was following the Migrating {N} Android Plugins from version 1.7 to 2.0 posted here:
https://www.nativescript.org/blog/details/migrating-n-android-plugins-from-version-1.7-to-2.0

  • Are there any other changes?

Yes, but the good news is, they are delete only. You need to go to the include.gradle file, if you have one, and delete the first block of code entirely.

//default elements
android {
    productFlavors {
        "my-plugin" {
            dimension "my-plugin"
        }
    }
}

This code will no longer be necessary and there will no longer be problems on Windows with path being too long, because of concatenation of the productFlavors names.

@petekanev
Copy link
Contributor Author

@roblav96 as noted previously, some of our articles are outdated, and have not been revisited since, also, we don't remove old, and irrelevant-as-of-this-moment blogs, as they are a good way to document the path NS has gone by. Originally .jar libs were to be deprecated, but later it was found inconvenient for developers to have to wrap libraries in android aars to use them in NS projects.

@roblav96
Copy link

roblav96 commented Sep 1, 2016

@Pip3r4o I totally understand. Is there a way the community could modify those articles as easy as it is to modify the docs?

@petekanev
Copy link
Contributor Author

petekanev commented Sep 1, 2016

@roblav96 The blog posts are there to stay... normally.
You can comment on them to make future readers aware of possible issues with the content of the post. Additionally I'll see if it would be more appropriate to take the post down, or at least put a disclaimer, as its content is misleading.

@roblav96
Copy link

roblav96 commented Sep 1, 2016

@Pip3r4o OK sounds good. I'm leaving a coment now.

@grantwparks
Copy link

"not our responsibility to make the include.gradles compliant, as the cases that need to be covered are endless". Yes, software is hard. Releasing stuff that doesn't work is very weak sauce. How hard would it be to print "missing productFlavors" instead of me having to work through the script and find out what it's looking for and getting an index out of range on? At least when you're parsing something and don't find something that has to be there, you notify the user of what's missing. Having failure in an atomic operation inside what's being done is amateurish. In my career that wouldn't be acceptable for in-house software, let alone for the consumption of the world.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants