Facebook Sdk Has Not Been Initialized FacebookSdk.sdkInitialize() - android

Hey I know this was asked before, but none of the solutions seem to help. I'm using first time Facebook SDK in my application.
What I've tried:
I had tried most of the things found on Internet but did not get anything regarding this.
Here is my MainActivity.java:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FacebookSdk.sdkInitialize(getApplicationContext());
}
}
Here is My Activitymain.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.login.widget.LoginButton
android:id="#+id/connectWithFbButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:text=" connect_with_facebook" />
</LinearLayout>
see my Logcat:
05-13 16:30:39.332: E/AndroidRuntime(10264): Caused by: The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first.

Problem
While integrating Android SDK for a react-native project, I had finished the Android with React Native v0.30+ Project Configuration guide, and ran react-native run-android and then got this screen:
I learned that FacebookSdk.sdkInitialize is deprecated. see here
After some searching, I realized that the guide did not contain the steps to add the Facebook App ID for my app.
Solution
Open android/app/src/main/AndroidManifest.xml file and look in the <application> tag to confirm that this meta-data tag exists:
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id"/>
Open android/app/src/main/res/values/strings.xml file and confirm that this there is a "facebook_app_id" string tag with your app id as the value:
<string name="facebook_app_id">YOUR_APP_ID_HERE</string>
Run react-native run-android.
These are the steps that worked for me.

You have to use FacebookSdk.sdkInitialize(getApplicationContext()); before setContentView(R.layout.activity_main); as documentation states out. In case you need a complete facebook login example, check this one here.

You don't need to use FacebookSdk.sdkInitialize anymore. Check if your:
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id"/>
is inside <application> tag.

For SDK v13.+ you must add Apple ID and Client Token.
Open the /app/res/values/strings.xml file in your app project and add:
<string name="facebook_app_id">1234</string>
<string name="facebook_client_token">56789</string>
Open the /app/manifests/AndroidManifest.xml file in your app project and add:
<application android:label="#string/app_name" ...>
...
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/facebook_app_id"/>
<meta-data android:name="com.facebook.sdk.ClientToken" android:value="#string/facebook_client_token"/>
...
</application>
You can find your Client Token in your FB Developer account:
App > Dashboard > Settings > Advanced > Security > Client token.

There is a reason why sdkInitialize() is deprecated.
Go to your manifest file within the android folder and add following
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id"/>
After that append in your strings.xml file (res/values/strings.xml) the string entry:
<string name="facebook_app_id">APP_ID</string>
Close your Metro Builder and rebuild your Project using react-native run-android

Follow only 2 Step and your Facebook Sdk iw working in React Native
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id"/>
<string name="facebook_app_id">YOUR_APP_ID_HERE</string>
Don't Need this b'coz Its Deprecated Now
FacebookSdk.sdkInitialize(getApplicationContext());

After checking the documentation I found that they are asking to initialize FacebookSdk in Application class onCreate() Method.
Snap code from Facebook doc:
public class MyApplication extends Application {
// Updated your class body:
#Override
public void onCreate() {
super.onCreate();
// Initialize the SDK before executing any other operations,
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
}
}

Seeing the responses listed in this question, the old way to initialize Facebook was:
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
// Initialize the SDK before executing any other operations,
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
}
}
But we can get the message:
Facebook Sdk Has Not Been Initialized FacebookSdk.sdkInitialize()
FacebookSdk.sdkInitialize() now is deprecated.
Now (2021) Facebook initialization has changed, the Facebook initialization is automatically.
1 Add the following to the dependencies {} section of your build.gradle (module: app) file to compile the latest version of the Facebook SDK for Android:
implementation 'com.facebook.android:facebook-android-sdk:latest.release'
Add Your Facebook App ID and Client Token
Add your Facebook App ID and Client Token to your project's strings file and update your Android manifest:
1 Open your /app/res/values/strings.xml file.
2 Add a string element with the name attribute facebook_app_id and value as your Facebook App ID to the file. For example
<string name="facebook_app_id">Facebook App ID</string>
<string name="facebook_client_token">Facebook Client ID</string>
3 Open /app/manifests/AndroidManifest.xml
4 Add a uses-permission element to the manifest:
<uses-permission android:name="android.permission.INTERNET"/>
5 Add a meta-data element to the application element:
<application android:label="#string/app_name" ...>
...
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/facebook_app_id"/>
<meta-data android:name="com.facebook.sdk.ClientToken" android:value="#string/facebook_client_token"/>
...
</application>

Use Initialise Callback Constructor like this:
Handler mHandler = new Handler();
FacebookSdk.InitializeCallback initializeCallback = new FacebookSdk.InitializeCallback() {
#Override
public void onInitialized() {
mHandler.post(new Runnable() {
#Override
public void run() {
//UI Code Here
}
});
}
};
//before setContentView()
FacebookSdk.sdkInitialize(getActivity().getApplicationContext(),initializeCallback);

If this helps anyone. For me I had to make this change
implementation 'com.facebook.android:facebook-login:latest.release' // for FB login
the above line of code must be added to the END of the dependencies object and not anywhere in between.
This is in android/app/build.gradle file

My app was crashing because the Privacy Policy url was not provided in "Settings/Basic" in https://developers.facebook.com/. (in fact it was provided but I had to "Save changes" again. Seems a bug from Facebook).
The app was working fine and the error message Facebook Sdk Has Not Been Initialized FacebookSdk.sdkInitialize()disappeared. Hope this help.

For me, this issue arose due to a package in my application called react-native-fbsdk.
Please check your package.json file for the same.
I removed the package using npm uninstall fbsdk and rebuilt by app and it worked perfectly.
Hope this solution works for you!

remove android:exported=true from AndroidManifest.xml and targetSdkVersion should be less than 31

Related

Sentry React Native Crash - Native Client is not available, can't start on native

I am getting crash on app launch in android with below error.
Native Client is not available, can't start on native.
Sentry.init({
enableNative: true,
enableNativeCrashHandling: true,
dsn:
'https://xyx.com/sentry/12',
})
When I set enableNative: false, then the crash stops coming but the android related crashes are not logged in sentry.
I have added sentry in build.gradle as well as per the sentry integration doc.
Any solution for this?
I had this problem as well, using the test of Sentry.nativeCrash() - the application would crash, but not be reported.
To fix, set autoInitializeNativeSdk to false and initialize in the AndroidManifest.
Sentry.init({
autoInitializeNativeSdk: false,
dsn:
'https://xyx.com/sentry/12',
})
Initialize the AndroidSDK in your AndroidManifest.xml
<application>
<meta-data android:name="io.sentry.auto-init" tools:replace="android:value" android:value="true" />
<meta-data android:name="io.sentry.dsn" android:value="https://examplePublicKey#o0.ingest.sentry.io/0" />
</application>
Make sure you add xmlns:tools="http://schemas.android.com/tools" to your manifest.
Follow the below link for documentation.
Native Initialize

What is the function or usefulness of the code below in Firebase? [duplicate]

I have an app that utilises Fabric's Crashlytics via Firebase.
The following is the first thing executed in my Applications onCreate
CrashlyticsCore crashlyticsCore = new CrashlyticsCore.Builder()
.disabled(BuildConfig.DEBUG)
.build();
Fabric.with(this, new Crashlytics.Builder().core(crashlyticsCore).build());
Nonetheless, the crashes are submitted in DEBUG == true mode.
I use the following versions
in my build.gradle
classpath "io.fabric.tools:gradle:1.25.1"
in my app/build.gradle
implementation "com.crashlytics.sdk.android:crashlytics:2.9.1"
Unfortunately the crashes still get reported. Any ideas, what I am doing wrong?
Correct answers have been posted by Bob Snyder and niqueco already however it seems kinda tedious to change the meta-data value every time you are building an actual release APK thus here's a solution that uses so called manifestPlaceholder and changes the value automatically to trueor false depending on the buildType.
Add the following to your apps build.gradle
android {
// ...
buildTypes {
debug {
manifestPlaceholders = [enableCrashReporting:"false"]
}
release {
manifestPlaceholders = [enableCrashReporting:"true"]
}
}
}
And this to your AndroidManifest.xml
<manifest ... >
<application ...>
// ...
<meta-data android:name="firebase_crashlytics_collection_enabled" android:value="${enableCrashReporting}" />
</application>
</manifest>
You can verify the current value by clicking on the Merged Manifest tab once you have opened the AndroidManifest.xml. You should see something like this:
The Firebase Crashlytics documentation explains that once reporting is enabled in an app session, it cannot be disabled.
By default, Crashlytics reporting is enabled in a ContentProvider named CrashlyticsInitProvider that executes before your Application instance is created. CrashlyticsInitProvider enables or disables reporting based on the meta-data value firebase_crashlytics_collection_enabled, which by default is true.
If you want reporting disabled, it's critical that the manifest meta-data be present and set to false:
<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="false" />
Look in the logcat during app initialization for the message:
CrashlyticsInitProvider: CrashlyticsInitProvider initialization successful
If the message is present, firebase_crashlytics_collection_enabled is true. If the message is not present, you have successfully set the meta-data to disable crash reporting.
If the meta-data is missing or set to true, you cannot disable reporting in your code using a call to Fabric.with(...).
In a comment to another answer, you indicate that you tried disabling reporting using the meta-data and were not successful. Check for a typo and ensure the declaration is correctly placed in the <application> element. In my tests, I am able to disabling reporting using the meta-data and then enable at run time.
I've finally found the issue. Crashlytics is initialized from a content provider, so by the time you try to disable from Application's onCreate() it's too late. Going through the decompiled code I've seen that you can disable that initialization by adding metadata to the <application> element in the manifest.
So, what I do is this... I've added this to app/src/debug/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><!--suppress ALL -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="<your app package>">
<application>
<meta-data android:name="firebase_crashlytics_collection_enabled" android:value="false" />
</application>
</manifest>
I've also disabled Crashlytics in the app module gradle build file by adding:
debug {
ext.enableCrashlytics = false
}
To my surprise I didn't need to do the Fabric.with(...) thing. The above was enough.
It's working fine: no reports.
I think it is possible to do it from code as well if you switched to firebase crashlytics and removed fabric crashlytics :
link to firebase doc
So in the onCreate of your application class :
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(!BuildConfig.DEBUG);
Got this information from android documentation
Customize your Firebase Crash Reports
Enable opt-in reporting:
By default, Firebase Crashlytics automatically collects crash reports for all your app's users. To give users more control over the data they send, you can enable opt-in reporting instead.
To do that, you have to disable automatic collection and initialize Crashlytics only for opt-in users.
Turn off automatic collection with a meta-data tag in your AndroidManifest.xml file:
<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="false" />
Enable collection for selected users by initializing Crashlytics from one of your app's activities:
Fabric.with(this, new Crashlytics());
You need to disable Crashlytics of app’s build.gradle. Disable Crashlytics for Debug Builds
android {
buildTypes {
debug {
// Disable fabric build ID generation for debug builds
ext.enableCrashlytics = false
...
If you would like to completely disable Firebase Crash reporting AND also not have to add the
com.crashlytics.sdk.android:crashlytics:2.9.1
dependency, then follow #reVerse's answer but also add this to your AndroidManifest.xml:
<application ...>
// ...
<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="${enableCrashReporting}" />
<meta-data
android:name="firebase_analytics_collection_deactivated"
android:value="true"/>
</application>

How to disable Crashlytics while in debug mode, in react-native-firebase?

I have integrated firebase Crashlytics on a react native application. I want to disable crash logs while working on development mode. How can I disable crash logs in debug mode for both Android and Ios Application. To create crash logs I have followed react native firebase documentation check this official link :: https://rnfirebase.io/docs/v5.x.x/crashlytics/android
I am using react-native-firebase version 5.2.2
I want to disable logs on debug mode without changing the version. I want to add code to disable the crash log for both Android and Ios. Please suggest how this should be done.
The documentation explains how to disable it
iOS
Turn off automatic collection with a new key to your Info.plist file:
Key: firebase_crashlytics_collection_enabled
Value: false
<key>firebase_crashlytics_collection_enabled</key>
<false/>
Android
Turn off automatic collection with a meta-data tag in your AndroidManifest.xml file:
<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="false" />
Enable collection at runtime
You can can initialise crashlytics in your javascript code using
firebase.crashlytics().enableCrashlyticsCollection();
You can then use
if (__DEV__) {
} else {
}
to run any specific code in development or in production.
I only need to set one line in firebase.json file,
{
"react-native": {
"crashlytics_debug_enabled": false
}
}
faced the same in one of my project : "react-native": "0.66.1"
Edit AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.reversedomain">
<application
...
...
// add this line
<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="false" />
</application>
install '#react-native-firebase/crashlytics'
add .env at the root of your project & set production = false
in app.js
import React from 'react'
import { production } from '#env';
import crashlytics from '#react-native-firebase/crashlytics';
useEffect(()=>{
if (production == true) {
// Enable on runtime
crashlytics().setCrashlyticsCollectionEnabled(true);
}
},[])
docs

facebook account kit - Returns error "We're sorry, something went wrong." in android

I am implementing facebook account kit in android app. I've completed initial setup and when I run the app it displays error
We're sorry, something went wrong.
https://developers.facebook.com/docs/accountkit/android
I searched a lot but did not find anything helpful.
If anyone has something to share please share here.
You need to go through link
https://developers.facebook.com/apps/ ----Your-App-ID--- /account-kit/
you can see Enable client access token flow.
make it yes.
#JayVDiyk You can get more information for an error by setting the debug flag of the init method to true, for more information about the reference:
https://developers.facebook.com/docs/accountkit/webjs/reference
The way you could set init method to true:
AccountKit.init({appId: 1, state: state, version: 'v1.0', debug: true})
Else you could try the solution by adding the http://localhost:3000' like this:
Make sure you set up the Account kit properly. The error shows with incorrect setup in my case.
steps are:
Generate Key Hash
Create a Facebook App.
Add Account Kit to your Facebook App
Android Integration
a. Gradle Dependencies
Add dependency in gradle for Account Kit
repositories {
jcenter()
}
dependencies {
compile 'com.facebook.android:account-kit-sdk:4.+'
}
b. Android Manifest
Add the following to the AndroidManifest.xml
<meta-data android:name="com.facebook.accountkit.ApplicationName" android:value="#string/app_name" />
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/facebook_app_id" />
<meta-data android:name="com.facebook.accountkit.ClientToken" android:value="#string/account_kit_client_token" />
<activity
android:name="com.facebook.accountkit.ui.AccountKitActivity"
android:theme="#style/AppLoginTheme"
tools:replace="android:theme"/>
Please Go through this links:
1) Link 1
2) Link 2

Gluon Charm Down Barcode Scanner - Intent handler not found

I am using Netbeans 8.1 and the gluonhq jfxplugin 2.2.0.
I am trying to read a barcode, and created a new project (the standard hello world). I changed the button handler to call a function UpdateText() (below) which in turn calls the Charm Down Scan service.
When I run the app, and click on the button I get the following error in the Android Device Manager:
E/AndroidRuntime(3583): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.gluonhq.charm.down.android.scan.SCAN cat=[android.intent.category.DEFAULT] flg=0x4080000 }
This crash is happening on the scanservice.scan() line.
Button click handler code:
protected void UpdateText(Label label) {
ScanService scanService = PlatformFactory.getPlatform().getScanService();
StringProperty scannedString = scanService.scan();
scannedString.addListener((obs, ov, nv) -> System.out.println("Scanned String = " + nv));
}
I would greatly appreciate any help
You need to define the com.gluonhq.charm.down.android.scan.SCAN intent in your AndroidManifest.xml file. Add the following activity definition below your main activity definition:
<activity android:name="com.gluonhq.charm.down.android.scan.zxing.CaptureActivity"
android:screenOrientation="sensorLandscape"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="com.gluonhq.charm.down.android.scan.SCAN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
By default, the AndroidManifest.xml file is generated for you under the hood by the plugin. If you haven't setup a custom AndroidManifest.xml file yet, you can copy the one that the plugin generates. The default version is located in build/javafxports/tmp/android/AndroidManifest.xml. Just copy that one to a persistent location, i.e. src/android. Then update your build.gradle to tell the plugin that it should use the custom AndroidManifest.xml file instead of generating the default one:
jfxmobile {
android {
manifest = 'src/android/AndroidManifest.xml'
}
}
update:
You also need to add an extra dependency to the zxing core library as it seems it isn't included automatically when depending on the charm library alone:
dependencies {
androidRuntime 'com.google.zxing:core:3.2.1'
}
Furthermore, you'll have to add the CAMERA permission to your manifest as well:
<uses-permission android:name="android.permission.CAMERA"/>

Categories

Resources