Android Question FireBase - Package Analysis Error [SOLVED]

VictorTandil

Member
Licensed User
Longtime User
Hi. I have a little problem. When I try to run applications that use the Firebase services (Ads, Notifications, etc.) I get an error in the device that says "Package Analysis Error". The code compiles correctly. However, the error occurs in the cell phone. I have tried with different Apps, on different phones, but I get the same error.-

I have uninstalled the SDK and reinstalled everything to the latest version. I modified the "Manifest" by adding the "Snippets" and downloaded the ".JSON" required, but it still does not work. Could someone tell me what I'm doing wrong?
 

VictorTandil

Member
Licensed User
Longtime User
The example APK worked, or so I think. It only displays a blank screen with the title "B4A Example". I try an example of "AdMob" and "FireBase Notification" but still giving error when trying to install through the console.-
 

Attachments

  • CMD.png
    CMD.png
    37.1 KB · Views: 420
  • PHONE.png
    PHONE.png
    38.8 KB · Views: 402
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The example APK worked, or so I think. It only displays a blank screen with the title "B4A Example".
This means that it worked.

Start with a new project with only FirebaseAnalytics. This is similar to the APK I posted. If it doesn't work for you then you are doing something wrong.
 
Upvote 0

VictorTandil

Member
Licensed User
Longtime User
One step closer. After thoroughly reviewing each line of the "Manifest" I found some errors that I corrected. However, the "Logs" window throws the following error:

PackageAdded: package: "------------"
Copying updated assets files (1)
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (firebasemessaging) Create **
Error occurred on line: 12 (FirebaseMessaging)
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process -------------. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
at com.google.firebase.messaging.FirebaseMessaging.getInstance(Unknown Source)
at anywheresoftware.b4a.objects.FirebaseNotificationsService$FirebaseMessageWrapper.Initialize(FirebaseNotificationsService.java:94)
at Pitbull.Test.Android.firebasemessaging._service_create(firebasemessaging.java:173)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:163)
at Pitbull.Test.Android.firebasemessaging.onCreate(firebasemessaging.java:55)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2918)
at android.app.ActivityThread.access$1900(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5643)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
** Service (firebasemessaging) Start **

What does this mean?
 
Upvote 0

VictorTandil

Member
Licensed User
Longtime User
Actually yes. But "Firebase Notifications" still does not work. I have noticed that the "Manifest" segment related to "Firebase Notifications" (Snippets of the tutorial) produces the error. I have set up the "google-services.json" file and placed it in the project folder. The strange thing is that if I compile via USB in Debug Mode, the compilation window throws the following error: "Failure [INSTALL_PARSE_FAILED_MANIFEST_MALFORMED]". Attached is a copy of the "Manifest".-

I really do not know what to do. I have three projects waiting for this. I know that sure is my mistake. But I followed the tutorials to the letter and I can not see the fault.-
 
Last edited:
Upvote 0

VictorTandil

Member
Licensed User
Longtime User
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="25"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'************ Google Play Services Base ************
AddApplicationText(
<activity android:name="com.google.android.gms.common.api.GoogleApiActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:exported="false"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@Integer/google_play_services_version" />
)
'************ Google Play Services Base (end) ************
'************ Firebase Base ************
CreateResourceFromFile("google-services", "google-services.json")
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.WAKE_LOCK)
AddPermission(com.google.android.c2dm.permission.RECEIVE)
AddPermission(${applicationId}.permission.C2D_MESSAGE)
AddManifestText( <permission android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />)
AddApplicationText(
<receiver
android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.measurement.UPLOAD"/>
</intent-filter>
</receiver>

<service
android:name="com.google.android.gms.measurement.AppMeasurementService"
android:enabled="true"
android:exported="false"/>
<provider
android:authorities="${applicationId}.firebaseinitprovider"
android:name="com.google.firebase.provider.FirebaseInitProvider"
android:exported="false"
android:initOrder="100" />
<receiver
android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.measurement.UPLOAD"/>
</intent-filter>
</receiver>

<service
android:name="com.google.android.gms.measurement.AppMeasurementService"
android:enabled="true"
android:exported="false"/>
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
android:exported="false" />


<service
android:name="com.google.firebase.iid.FirebaseInstanceIdService"
android:exported="true">
<intent-filter android:priority="-500">
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
)
'************ Firebase Base (end) ************
'************ Firebase Notifications ************
AddApplicationText(
<service
android:name="com.google.firebase.messaging.FirebaseMessagingService"
android:exported="true">
<intent-filter android:priority="-500">
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="anywheresoftware.b4a.objects.FirebaseNotificationsService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
)
'************ Firebase Notifications (end)************
 
Upvote 0

VictorTandil

Member
Licensed User
Longtime User
----- Main -----
... by default and ...
B4X:
Private Sub SendMessage(Topic As String, Title As String, Body As String)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    m.Put("data", data)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
    Job.GetRequest.SetContentType("application/json;charset=UTF-8")
    Job.GetRequest.SetHeader("Authorization", "key=" & "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
End Sub


Sub JobDone(job As HttpJob)
    Log(job)
    If job.Success Then
        Log(job.GetString)
    End If
    job.Release
End Sub

----- Starter -----
B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

'Starter service
Sub Process_Globals

End Sub

Sub Service_Create
    CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

----- FirebaseMessaging -----
B4X:
#Region  Service Attributes
    #StartAtBoot: False
  
#End Region

Sub Process_Globals
    Private fm As FirebaseMessaging
End Sub

Sub Service_Create
    fm.Initialize("fm")
End Sub

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("general") 'you can subscribe to more topics
End Sub

Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized And fm.HandleIntent(StartingIntent) Then Return
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
    n.Notify(1)
End Sub

Sub Service_Destroy

End Sub
 
Upvote 0

VictorTandil

Member
Licensed User
Longtime User
I can not believe I'm going to say this, but ... IT WORKS !!!

Apparently it only accepts lowercase names. Thank you! thank you very much! This opens up a new world of possibilities for me. I'm very happy :_ )
 
Upvote 0
Top