adjust / cordova_sdk

This is the Cordova SDK of
http://www.adjust.com
MIT License
36 stars 43 forks source link

Cordova plugin dependency issue #102

Closed evgentset closed 4 years ago

evgentset commented 4 years ago

Hello, I'm trying to create cordova plugin for android and want to use Adjust in my plugin. I cannot be sure that cordova developer has Adjust plugin in his config.xml. So I set this in my plugin.xml:

        <preference name="ADJUST_VERSION" default="+"/>
        <framework src="com.adjust.sdk:adjust-android:$ADJUST_VERSION" />
        <framework src="com.adjust.sdk:adjust-android-webbridge:$ADJUST_VERSION" />

In config.xml cordova developer set Adjust sdk and my plugin:

<plugin name="com.adjust.sdk" spec="4.21.0" />
<plugin name="cordova-plugin-mycustomplugin" spec="myCustomPlugin">
    <variable name="ADJUST_VERSION" value="4.21.0" />
</plugin>

When we try to build app, we have an error:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
  Program type already present: com.adjust.sdk.ActivityHandler$11
  Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.

I suppose it happens because of you use jar file in your android cordova plugin instead of maven dependency on your library. So gradle cannot resolve this dependency.

Could you please help me and specify how can I configure and use Adjust correctly with my cordova plugin?

uerceg commented 4 years ago

Hi @evgentset

Interesting thing you're trying to make in there, nice. 👍

Your assumption is correct, you're seeing that error most probably because our plugin itself uses our native SDK as JAR dependency and on the other end you're adding native SDK dependency (and web bridge as well for some reason, you don't really need that) via Gradle as well.

Never tried this out, but if we assume that:

<plugin name="com.adjust.sdk" spec="4.21.0" />

adds our Cordova plugin as a dependency to your Cordova plugin (does it?), then not sure why do you need:

<preference name="ADJUST_VERSION" default="+"/>
<framework src="com.adjust.sdk:adjust-android:$ADJUST_VERSION" />
<framework src="com.adjust.sdk:adjust-android-webbridge:$ADJUST_VERSION" />

in your configuration at all?

evgentset commented 4 years ago

But, first of all, let me explain. I'm trying to create cordova plugin for other developers. Adjust SDK is required for this plugin . Also I need WebBridge as plugin has WebView in Activity, and I need to attach Adjust to WebView. So I added dependency for Adjust SDK and WebBridge to my plugin (in plugin.xml).

When another developer will decide to use my plugin it may have an issue with that error(as described above). It happens if that developer use adjust plugin in its application. In this case

<plugin name="com.adjust.sdk" spec="4.21.0" />
<plugin name="cordova-plugin-mycustomplugin" spec="myCustomPlugin">

will be added by app developer. I cannot control this lines, this is app developer respinsibility.

To use Adjust SDK and Bridge in plugin, me (as plugin developer) need to say cordova to add depemdency for Adjust, because app developer may not use Adjust SDK in its app at all. So I use these lines in my plugin:

<preference name="ADJUST_VERSION" default="+"/>
<framework src="com.adjust.sdk:adjust-android:$ADJUST_VERSION" />
<framework src="com.adjust.sdk:adjust-android-webbridge:$ADJUST_VERSION" />

Otherwise, in case I not add these lines I won't be able to use Adjust SDK in my plugin.

If you use maven dependency in your plugin I suppose Gradle will be able to resolve this dependency issue. Or it's nessesery to use exactly jar file in your plugin? I suppose that you can also use something like <framework src="com.adjust.sdk:adjust-android:+/> instead of this line: <source-file src="src/android/adjust-android.jar" target-dir="libs" /> here https://github.com/adjust/cordova_sdk/blob/b8502bdc968a1dce101fe3117cd2a1f9f3689c2f/plugin.xml#L56

It should solve all issues. In other case I will need to provide some description for this issue for my plugin (like "if you use adjust plugin use this otherwise that...") and detect somehow this situation.

And other case is also possible. User installed my plugin at first, but after some time will decide to use Adjust plugin also. And it will get this error in this case. Developer will be a bit confused )

uerceg commented 4 years ago

Hi @evgentset

And sorry for a bit late reply on this discussion.

I see your point. And honestly not really sure at this moment if there's any workaround other than what you suggested - if our plugin would pull dependency to our native SDK from Maven instead of using JAR. That is definitely doable and I see how that would help your efforts, but reason why we're not doing that is most probably - convenience for us. We are often testing if our plugin with various naive SDK versions and lots of times those versions are still not published to Maven repository - not even to staging repository. We are simply making some adjustments in order to see how they behave in non native SDKs (including Cordova) and we need quick and easy way in our flows to make this test. If we were pulling our native SDK from Maven (like suggested), in these cases we would need to do one of two things:

First option is annoying and not something we would consider doing. Second option, even though complicates our lives a bit, sounds like something which in theory we might consider doing. But probably not in current major SDK release version (4). We are aiming to update all of our SDKs to next major release by EOY and we will keep this in mind when we start to discuss Cordova SDK changes. But until then, we will continue to use JAR dependency.

Closing this one for now, we added this ticket to our internal issue tracking system for v5 and will follow up when the time comes. Until then, feel free to comment in here in case you have any further questions/suggestions on this topic.

Cheers

evgentset commented 4 years ago

Hello, thanks, will wait next versions.