22

I'm building a Cordova Android plugin. I want to use a 3rd party View inside an Intent that is created by the plugin (specifically scissors). Normally (in non Cordova projects) I would go to my project's build.gradle file and add it like this:

dependencies {
compile 'com.lyft:scissors:1.0.1' }

But it seems like the build.gradle file in my plugin's project wasn't meant to be touched? What is the proper way to add a dependency to a plugin project, to support both builds via Cordova and builds via Android Studio? Same question, but for a local project (not hosted on GitHub).

fasteque
  • 4,309
  • 8
  • 38
  • 50
YakirNa
  • 519
  • 1
  • 5
  • 15

2 Answers2

24

You have to use your own gradle file then link it on the plugin.xml like this

<framework src="relative/path/your.gradle" custom="true" type="gradleReference" />

You have to put that tag on the plugin.xml, so on plugin install it's read and cordova handles it (not sure how it works internally, but I suppose that it copies the values from your custom .gradle to the main build.gradle).

So you can't test it on your current project, you have to create a new project and add the plugin and see if it works

jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
  • Hi, thanks for the answer. can you explain what do you mean by _use your own gradle file_ ? is there a way to link the dependency to a git repository (such as in the link in the question)? – YakirNa Dec 16 '15 at 16:46
  • You create a new .gradle file with the text you posted on the question, then you link your .gradle file on your plugin.xml as I told you on my answer. Take a look into the crosswalk plugin – jcesarmobile Dec 16 '15 at 18:09
  • and how do I add it to Android Studio so that I can actually use it? – YakirNa Dec 17 '15 at 10:44
  • have you ever created a plugin before? read the plugin development guide http://cordova.apache.org/docs/en/latest/guide/hybrid/plugins/index.html, try to create a basic plugin and once you get it working add the scissors – jcesarmobile Dec 17 '15 at 11:09
  • No, I haven't. It's a working plugin that I am editing. i'm pretty sure they used cordova create to generate all the files, and then just added a plugin.xml file, like this (old) tutorial suggests http://devgirl.org/2013/07/17/tutorial-how-to-write-a-phonegap-plugin-for-android/. this approach gives the advantage that I can test my plugin pretty easily. now, since my plugin is a cordova project itself, I can't just edit it's gradle files. I managed to work around this in the iOS version, but with android it seems more complicated. hope what i'm saying is making sense... – YakirNa Dec 17 '15 at 12:07
  • You have to put that tag I told you on the plugin.xml, so on plugin install it's read and cordova handles it (not sure how it works internally, but I suppose that it copies the values in your custom .gradle to the main build.gradle). So you can't test it on your current project, you have to create a new project and add the plugin and see if it works – jcesarmobile Dec 17 '15 at 12:21
  • Thank. If you will edit the answer to contain the info in the comment, I will accept this as the answer. – YakirNa Dec 19 '15 at 12:26
  • What if I wanted to add to the application ? not to the plugin. tried adding in gradle file directly, but it removes when I do build. – droidev Nov 03 '17 at 07:37
  • @droidev when you add the plugin the dependency will be added to the app where you install the plugin – jcesarmobile Nov 03 '17 at 07:46
  • @jcesarmobile what if I am not using any plugins ? in my case I wanted to integrate facebook SDK, so I just wanted to add the gradle dependency and initialize in main activity. – droidev Nov 03 '17 at 07:47
  • @droidev create a plugin that just have the plugin.xml and the .gradle file (the framework tag should also work instead of the .gradle file) – jcesarmobile Nov 03 '17 at 08:55
  • @jcesarmobile is there any other better way ? – droidev Nov 03 '17 at 08:56
  • @droidev The other way is a hook that writes in the cordova .gradle file, but I don't think that's better – jcesarmobile Nov 03 '17 at 09:31
  • Will use this approach then. – droidev Nov 03 '17 at 09:32
  • @jcesarmobile `Unable to resolve dependency for ':app@releaseUnitTest/compileClasspath': Could not resolve project :k4l-video-trimmer.` when I add the module project in plugin and add that plugin in Ionic demo project – Kishore Jethava Jul 02 '18 at 09:49
  • @KishoreJethava create a new question and provide the code you used – jcesarmobile Jul 02 '18 at 10:14
  • I didnt see plugin.xml in my root project folder. I only saw many plugin.xml under /plugins/cordova-xxx folder. Should I manually create the plugin.xml file? – Panadol Chong Apr 08 '21 at 09:55
  • The question is about adding dependencies to plugins, all plugins must have a plugin.xml. Looks like you are trying to do it to a regular cordova app without a plugin. – jcesarmobile Apr 08 '21 at 11:16
14

The Cordova Android plugins doc (in "Adding Dependency Libraries") specify that you can add dependency libraries with the <framework> tag in your plugin.xml.

So for the scissors dependency you can use:

<platform name="android">
    <framework src="com.lyft:scissors:1.0.1" />
louisbl
  • 366
  • 2
  • 6
  • and after that? What do I need to do, so that I can use it in Android Studio? running 'cordova prepare ios' seems not to be enough, Nor does build or clean. – YakirNa Dec 17 '15 at 11:26
  • Assuming you meant `cordova prepare android`, you need to create a Cordova app with a dependency to your plugin in order to test it. You can use `cordova plugin add ` from this test app to add the dependency before running `cordova prepare android`. – louisbl Dec 18 '15 at 01:29
  • For some reason, when I tried this with my plugin, it seemed to occasionally cache these settings. Making the above changes followed by: `cordova platform rm android` `cordova platform add android@5.1.1` `cordova plugin add myplugin` – Adam Tegen Apr 14 '16 at 21:28
  • It works for me. This answer must be checked as correct. – gbixahue Mar 04 '20 at 10:37
  • I didnt see plugin.xml in my root project folder. I only saw many plugin.xml under /plugins/cordova-xxx folder. Should I manually create the plugin.xml file? – Panadol Chong Apr 08 '21 at 09:55