NativeScript: Android plugins and Windows path size issues

If your on Linux or OSX you can skip this post; it only applies to us pathetic people that prefer Windows (like me ). I do have Linux and OSX, so maybe I'm not that pathetic; but since I do prefer Windows, maybe I am.

If you don't want to read the reason this occurs but all you want is the fix; then scroll down to the fix!

Well if you see this error: Command [Some Path]\gradlew.bat failed with exit code 1 then you more than likely ran into this error if you see a lot of messages above it with "No Delegate Set" and errors.

Well to understand the issue you need to understand that this is a issue between Windows, Gradle and how NativeScript (A fix might show up by v1.8 of NativeScript) currently does Android plugins. Windows, if it supported larger path sizes; this wouldn't be an issue (which is why it works on Mac/Linux). NativeScript if it handled plugins differently might not run into this issue either. But at the current place we are at; it does so here is the crux of the issue.

1. Windows only supports 255 character path sizes (if they are not UNC or Unicode path's)

2. NativeScript scans all your plugins; any that have a platforms/android folder; it will use the include.gradle file found in that folder. If there is NOT a include.gradle file; it will create one for you using the plugin name as the information.

3. Gradle combines all the productFlavors (which are inside the include.gradle files) into one long string.

Ok, lets look at a sample include.gradle file (this is also what is also generated for you, if it doesn't exist)

So lets pretend your code exists at: C:\nativescript\AwesomeProject\, so there is (32) characters. Now add platforms\android\ (18). Next add build\intermediates\res\merged\ (31) as this is where Gradle does its resource merging. So lets say you are using the Telerik NativeScript UI plugin; and lets grab one of the image names \debug\drawable-xxhdpi\someimage.png which is of course adds another 49 characters. So this means we are using 130 characters already; leaving us with 122 characters left to play with.

So if you have a plugin installed like nativescript-websockets you get 23 characters used just for a single plugin. I bet you can see where this is going. Add on the cool nativescript-telerik-ui plugin and you have another 23 characters used. Add just a couple more plugins, and yes -- you are now well over 255 character limit!

Guess what happens??? Well if you are following the story so far; you get a nice error that gradle quit with error 1.

The Fix:

Go into you node_modules folder, and then you need to repeat this next set of steps for EACH of the plugins you have installed.

Enter that plugins platforms\android folder and edit the include.gradle -- if that folder doesn't exist then you are done and can skip this plugin. If the folder exists but the include.gradle file doesn't exist you can choose to ignore it or create your own. DO NOT create a platforms\android folder, if it doesn't exist. Only create a include.gradle if the plugin already has a platforms\android folder and it doesn't have a include.gradle.

Edit the include.gradle and make the name shorter; this is the formula that I used. Replace nativescript with "ns", eliminate any dashes, and then use the first letter for each word in the plugin. So "nativescript-cardview" becomes "nscv",and "nativescript-telerik-ui" becomes "nstu". So the include.gradle changes from above (nativescript-websockets) to:

android {
productFlavors {
"nsws" {
dimension "nsws"
}
}
}

Pretty simple huh? By shorting the name, you go from 23 character per plugin to 4 characters per plugin. So you can now easily fit 5 plugins in the same space it used to take for 1 plugin. Please make sure the total letters are unique -- don't have two plugins with the same name (i.e. "nsws" for nativescript-websockets and nativescript-webserver) as the same name, make them distinct. Having duplicate names will probably break Gradle.

Do a nativescript platform add android to re-install the platform after you are all done, and then you should be good to go.

Please note this potential side effect: I have confirmation below that if you are using a plugin that requires additional permissions; this work around will unfortunately not merge the info from the plugins AndroidManifest.xml file. With all the plugins I had, I never saw this issue; but depending on what you are doing this is something you may run into. You have two choices to deal with this:

Don't rename that specific plugin inside the include.gradle file. This way the AndroidManifest.xml file will get merged properly. You an afford a couple plugins to still have large names.

Copy the information that you need (like permissions) from the plugins AndroidManifest and manually merge it into your master manifest located at App/App_Resources/Android/AndroidManifest.xml

The bug report so you can track this is Android/369 and based on the conversation with the NativeScript devs this should hopefully be fixed in v1.8 (but no promises).

Nathanael, this is @nedium from github.
First of all, i am grateful for this blog post.

I have applied your workaround, modified the include.gradle files across all plugins that have platforms/android folder and viola, my application is compiling fine.
But, (there is always a "but") application started to behave wrong. As i investigated the problem, i saw that none of the AndroidManifest.xml files merged to the project manifest. As a result, none of the modules requiring permissions are working.

Is it something that i did wrong, or is it same on your side?
Thanks in advance,
Nedim

Interesting, none of my plugins had that issue; but Plamen did mention in the bug report when we were discussing the workaround that this COULD be a side effect of doing this. I guess you have confirmed it. You will either need to not do the renaming with that specific plugin, or manually copy the info from that AndroidManifest.xml into your master AndroidManifest.xml file that is in the app/app_resources/android folder.