Android Question #AdditionalJar: guava-23.0.jar causes the compile to fail

Bryan Joyce

Member
Licensed User
Longtime User
I have an app that uses Firebase notifications and needs to be able to send and receive notifications from B4A the way it did until Google changed their authentication method. I have followed Erel's example and it works fine to be able to send from B4J. I have attempted to use the same code and resources in B4A and I have run into a number of issues. If I add #AdditionalJar: guava-23.0.jar the project will not compile (error is below). If I comment it out, it will compile. The project attributes are

<code>
#Region Project Attributes
#ApplicationLabel:B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#BridgeLogger:True
#MultiDex: true

#AdditionalJar: google-auth-library-oauth2-http-1.18.0.jar
#AdditionalJar: google-auth-library-credentials-1.18.0.jar
#AdditionalJar: guava-23.0.jar
#AdditionalJar: google-http-client-1.43.3.jar
#AdditionalJar: google-http-client-gson-1.43.3.jar
#AdditionalJar: gson-2.10.1.jar
#AdditionalJar: opencensus-api-0.31.1.jar
#AdditionalJar: opencensus-contrib-http-util-0.31.1.jar
#AdditionalJar: grpc-context-1.27.2.jar
#AdditionalJar: com.google.android.gms:play-services-vision
</code>

<Code>
B4A Version: 13.00
Parsing code. (0.04s)
Java Version: 19
Building folders structure. (0.10s)
Running custom action. (0.06s)
Compiling code. (0.11s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.10s)
(AndroidX SDK)
Compiling resources (0.11s)
Linking resources (0.54s)
build tools: 34.0.0, android jar: android-34
Compiling debugger engine code. (0.01s)
Compiling generated Java code. (0.17s)
Finding libraries that need to be dexed. (0.04s)
Dex code (2.39s)
Dex merge Error
Error in C:\Android\tools\..\extras\b4a_local\unpacked-guava-23.0-63864375760027\dex_v1\guava-23.0.zip:classes.dex:
Type com.google.common.util.concurrent.ListenableFuture is defined multiple times: C:\Android\tools\..\extras\b4a_local\unpacked-guava-23.0-63864375760027\dex_v1\guava-23.0.zip:classes.dex, C:\Android\tools\..\extras\b4a_local\unpacked-com.google.guava-listenablefuture-63860276976000\dex_v1\com.google.guava-listenablefuture.zip:classes.dex
Compilation failed
Exception in thread "main" java.lang.RuntimeException: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: C:\Android\tools\..\extras\b4a_local\unpacked-guava-23.0-63864375760027\dex_v1\guava-23.0.zip:classes.dex
at com.android.tools.r8.internal.po.a(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:1269)
at com.android.tools.r8.D8.main(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:5)
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: C:\Android\tools\..\extras\b4a_local\unpacked-guava-23.0-63864375760027\dex_v1\guava-23.0.zip:classes.dex
at Version.fakeStackEntry(Version_8.3.37.java:0)
at com.android.tools.r8.Q.a(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:5)
at com.android.tools.r8.internal.po.a(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:244)
at com.android.tools.r8.internal.po.a(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:90)
at com.android.tools.r8.internal.po.a(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:909)
at com.android.tools.r8.internal.po.b(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:99)
at com.android.tools.r8.D8.a(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:1041)
at com.android.tools.r8.D8.b(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:514)
at com.android.tools.r8.D8.a(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:1076)
at com.android.tools.r8.internal.po.a(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:1256)
... 1 more
Caused by: com.android.tools.r8.internal.h: Type com.google.common.util.concurrent.ListenableFuture is defined multiple times: C:\Android\tools\..\extras\b4a_local\unpacked-guava-23.0-63864375760027\dex_v1\guava-23.0.zip:classes.dex, C:\Android\tools\..\extras\b4a_local\unpacked-com.google.guava-listenablefuture-63860276976000\dex_v1\com.google.guava-listenablefuture.zip:classes.dex
at com.android.tools.r8.internal.X30.a(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:346)
at com.android.tools.r8.internal.pX.a(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:232)
at com.android.tools.r8.internal.pX.a(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:91)
at java.base/java.util.concurrent.ConcurrentHashMap.merge(ConcurrentHashMap.java:2056)
at com.android.tools.r8.internal.pX.a(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:32)
at com.android.tools.r8.graph.q4$a.d(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:6)
at com.android.tools.r8.dex.c.a(R8_8.3.37_d0b8d9dfe8720e37c27d2233931d9d72dbd696ffd92111cc9b36e58b4baad1cc:264)
</Code>

The libraries I am using are
B4XPages 1.12
Core 12.8
Firebase Analytics 3.01
Firebase Notifications 3.11
JSON 1.21
OkHTTP 1.50
OKHTTPUtils2 3.04
Phone 2.60
Runtime Permissions 1.2

Manifest is
<Code>
AddManifestText(
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="34"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
CreateResourceFromFile(Macro, FirebaseAnalytics.FirebaseAnalytics)
CreateResourceFromFile(Macro, FirebaseNotifications.FirebaseNotifications)
</Code>

SDK Manager is up to date but I get an error "Error reading recommended items list". I don't think this is related because I was having the issue prior to seeing this.

Hopefully this is enough information and ideas are appreciated.
 

Attachments

  • Libraries used.png
    Libraries used.png
    22.2 KB · Views: 17
Solution

Bryan Joyce

Member
Licensed User
Longtime User
Thanks for the reply. That is most disappointing, it completely breaks an app that was working for years. I will have to look for an alternative
 
Upvote 0

Bryan Joyce

Member
Licensed User
Longtime User
Thanks Erel, it compiles now. It now crashes when I attempt to send a message with Firebase notifications. I can open a new question if you think it is not related.

Error Message:
** Activity (main) Resume **
*** Receiver (firebasemessaging) Receive (first time) ***
TokenRefresh: eGLtBx2RTJilLAe8-WJOSd:APA91bGhe8bQVcmVRprF-7XLCCWJ25bRc_X4bFK0KS1IySYhZPscpamQyGZZ42JXJbGS39Kbpid7y0ttuFHApcqbzqYT6IJyMZsbWoyORkl7rD-_-rf-4ZWn6ozuu3myKVqyMRoTcbTl
** Activity (main) Resume **
Error occurred on line: 93 (FirebaseMessaging)
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.keywords.Common.CallSubDebug(Common.java:1082)
    at FCM.Test2.b4xmainpage._btnsendmessage_click(b4xmainpage.java:331)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7792)
    at android.widget.TextView.performClick(TextView.java:16112)
    at android.view.View.performClickInternal(View.java:7769)
    at android.view.View.access$3800(View.java:910)
    at android.view.View$PerformClick.run(View.java:30218)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8663)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:336)
    at anywheresoftware.b4a.debug.Debug.CallSubNew(Debug.java:282)
    ... 24 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:318)
    ... 25 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at FCM.Test2.firebasemessaging._gettokenvalue(firebasemessaging.java:198)
    at FCM.Test2.firebasemessaging$ResumableSub_Send.resume(firebasemessaging.java:87)
    at FCM.Test2.firebasemessaging._send(firebasemessaging.java:61)
    ... 27 more
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/auth/oauth2/OAuth2Credentials$RefreshTask;
    at com.google.auth.oauth2.OAuth2Credentials.getOrCreateRefreshTask(OAuth2Credentials.java:273)
    at com.google.auth.oauth2.OAuth2Credentials.asyncFetch(OAuth2Credentials.java:226)
    at com.google.auth.oauth2.OAuth2Credentials.refreshIfExpired(OAuth2Credentials.java:203)
    ... 32 more
Caused by: java.lang.ClassNotFoundException: com.google.auth.oauth2.OAuth2Credentials$RefreshTask
    ... 35 more

My Firebase code:
Sub Process_Globals
    Private fm As FirebaseMessaging
    Private Const ProjectId As String = "fcmtest2-124ae" 'change
    Private SendToken As String
End Sub

Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    If FirstTime Then
        fm.Initialize("fm")
    End If
    fm.HandleIntent(StartingIntent)
End Sub

Public Sub SubscribeToTopics (Topics() As Object)
    For Each topic As String In Topics
        fm.SubscribeToTopic(topic)
    Next
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    CallSub(B4XPages.MainPage,"MessageRecieved")  'displays a message to the UI
    If B4XPages.IsInitialized And B4XPages.GetManager.IsForeground Then
        Log("App is in the foreground. In iOS a notification will not appear while the app is in the foreground (unless UserNotificationCenter is used).")
    End If
    Dim n2 As Notification
    n2.Initialize2(n2.IMPORTANCE_HIGH)
    n2.Icon = "icon"
    n2.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
    n2.Notify(1)
End Sub

Sub fm_TokenRefresh (Token As String)  'Used for recieving
    Log("TokenRefresh: " & Token)
End Sub

Private Sub Send'(topic As String, title As String, body As String)
    Dim SendToken As String = GetTokenValue(B4XPages.MainPage.ServiceAccountFilePath)
    Dim topic, title, body As String
    topic = "Test Topic"
    title = "Test Title"
    body = "Test Body"
    Wait For (SendMessage(topic, title, body)) Complete (Success As Boolean) 'B4A - comment if not needed
    'Wait For (SendMessage("ios_" & topic, title, body)) Complete (Success As Boolean) 'B4i - comment if not needed
    Log("Sent:" & topic & " "& title & " " & body)
End Sub

Private Sub SendMessage(Topic As String, Title As String, Body As String) As ResumableSub
    Dim Job As HttpJob
    Job.Initialize("", Me)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    Dim message As Map = CreateMap("topic": Topic, "data": data)
    message.Put("android", CreateMap("priority": "high"))
    Dim jg As JSONGenerator
    jg.Initialize(CreateMap("message": message))
    Log(jg.ToPrettyString(4))
    Job.PostString($"https://fcm.googleapis.com/v1/projects/${ProjectId}/messages:send"$, jg.ToString)
    Job.GetRequest.SetContentType("application/json;charset=UTF-8")
    Job.GetRequest.SetHeader("Authorization", "Bearer " & SendToken)
    Wait For (Job) JobDone(Job As HttpJob)
    If Job.Success Then
        Log(Job.GetString)
    End If
    Job.Release
    Return True
End Sub

Private Sub GetTokenValue (FilePath As String) As String 
    Dim GoogleCredentials As JavaObject
    GoogleCredentials.InitializeStatic("com.google.auth.oauth2.GoogleCredentials")
    Dim Credentials As JavaObject = GoogleCredentials.RunMethodJO("fromStream", Array(File.OpenInput(FilePath, ""))) _
        .RunMethod("createScoped", Array(Array As String("https://www.googleapis.com/auth/firebase.messaging")))
    Credentials.RunMethod("refreshIfExpired", Null)
    Return Credentials.RunMethodJO("getAccessToken", Null).RunMethod("getTokenValue", Null)
End Sub

The error is thrown from this line of code
<Code> Credentials.RunMethod("refreshIfExpired", Null) </Code> .
The filepath is to the json file that I copied into DIRInternal - /data/user/0/FCM.Test2/files/fcmtest2-124ae-firebase-adminsdk-rii0r-494c60c2d3.json
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
 
Upvote 0
Solution

Bryan Joyce

Member
Licensed User
Longtime User
Thanks Erel! I am able to send with a B4A app now! My app needs to send and receive and both are not working at the same time, I can do one or the other. Will getting tokens from the send code and receive code conflict? My code is above. I am going to experiment a little more on this.
 
Upvote 0
Top