Android Question Cannot use external aar Conflicting dependencies

Multiverse app

Active Member
Licensed User
Longtime User
I'm using external libs added through #AdditionalJar which contain the use of okhttp.
My B4A project also uses okhttp, and I cannot modify those external libs. When I run the project, I receive this error:

B4X:
B4A Version: 9.90 BETA #2
Java Version: 11
Parsing code.    (0.00s)
Building folders structure.    (0.02s)
Compiling code.    (0.06s)
Compiling layouts code.    (0.00s)
Organizing libraries.    (0.00s)
    (AndroidX SDK)
Generating R file.    (0.00s)
Compiling debugger engine code.    (0.77s)
Compiling generated Java code.    (1.21s)
Convert byte code - optimized dex.    Error
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Address;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Authenticator;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Authenticator$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache$2;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache$CacheRequestImpl;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache$CacheRequestImpl$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lokhttp3/Cache$CacheResponseBody;
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: Translation has been interrupted
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:692)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:315)
    at com.android.dx.command.dexer.Main.runDx(Main.java:293)
    at com.android.dx.command.dexer.Main.main(Main.java:249)
    at com.android.dx.command.Main.main(Main.java:94)
Caused by: java.lang.InterruptedException: Too many errors
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:684)
    ... 4 more


1599834385488.png

When I remove the okhttp libs, the project runs just fine, but I want to be able to use the libs in the project.

How do Iimport both the libs and make this work?
 

drgottjr

Expert
Licensed User
Longtime User
what happens when you remove (or comment out) the #additionaljar lines for okhttp-3.12.0.jar and okio-1.15.0.jar? (by the way, you don't need the .jar suffix. but you do need the .aar). okhttp and okio classes are already defined in b4a's okhttp and okhttputils .jars. the versions may not be the same, but that isn't necessarily a problem.
 
Upvote 0

Multiverse app

Active Member
Licensed User
Longtime User
Thanks for the reply.
Even after commenting those lines I get the same error.
This makes me believe that the other aars and jars I'm importing have okhttp implemented. Not sure how to get around that.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
okhttputils wraps getting and posting to http(s) servers. you can implement those things yourself. (to see what's involved, i've done it with okhttp3's client, and java's urlconnection and httpurlconnection. pretty straightforward.) obviously easier to use okhttputils (for several reasons), but if all those other libraries play nice together and okhttputils is the problem, you may have to bite the bullet.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i'll send it to you, if you don't mind. i had a very unpleasant experience posting some code here. the actual client code is simple (pretty much straight from square's example), but there are some things you need to keep in mind about internet io on android (eg, running it on the main thread. this and other matters are taken into account by okhttputils. if you roll your own, you need to know about android's thread policy and the like.) erel's got a post somewhere. i'll try to find it. basically, it allows you to run okhttp3's client on the main thread, but you need to understand the ramifications of doing that. i don't know what the rest of your app does. overriding the policy and allowing io on the main thread doesn't mean you're won't run up against the very reasons why google instituted the policy: namely, causing the ui to freeze waiting for some result, and having android kill the app. anyway, i'll dig out the code and pm it to you (along with a link to erel's post. you can probably find it by searching for "main thread" or "io on main thread")
 
Upvote 0
Top