Alex with Branch.io here:
We recently made some changes to our tutorial, and it looks like we missed a few things. I appreciate that you are posting about this - we will click on the update later today for more clarity.
In this particular case, there are two problems:
- Mixing between Applied
onCreate() and Operations onCreate() , none of which are needed for the base implementation. - There is no application class (we accidentally deleted this step from our tutorial completely - my apologies).
To get started, update your files as follows:
AndroidManifest.xml
You have three options:
1. Use the Branch application class (easiest)
If you don't have a special application class yet, this is the easiest approach. Add android:name="io.branch.referral.BranchApp" to the Application class:
Edit: UPDATED snippet for comments below
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.chg.appbranch_01"> <meta-data android:name="io.branch.sdk.BranchKey" android:value="xxx" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" android:name="io.branch.referral.BranchApp"> <meta-data android:name="io.branch.sdk.TestMode" android:value="true" /> <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" /> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <data android:scheme="theapp" android:host="open" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> </intent-filter> </activity> <receiver android:name="io.branch.referral.InstallListener" android:exported="true"> <intent-filter> <action android:name="com.android.vending.INSTALL_REFERRER" /> </intent-filter> </receiver> </application> </manifest>
2. Extend the Application class with the BranchApp class
If you already have your own application class, this is the easiest approach. Your AndroidManifext.xml file will look like this:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" android:name="com.your.app.CustomApplicationClass" >
Your own application class ( CustomApplicationClass in the above example) would look like this:
public final class CustomApplicationClass extends YourApp { @Override public void onCreate() { super.onCreate(); } }
3. Integrate directly into your own application class
The most customizable approach for advanced implementations. You will have your AndroidManifext.xml file the same as above:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" android:name="com.your.app.CustomApplicationClass" >
And then configure your application class as follows:
public final class CustomApplicationClass { @Override public void onCreate() { super.onCreate(); Branch.getAutoInstance(this); } }
Activity determination
Remove the onCreate() calls. They are not needed here and are actually the cause of your error message ( Branch.getAutoInstance(this) passed an Activity context like this when the SDK was expecting the Application context from option 3 above).
import io.branch.referral.Branch; import io.branch.referral.BranchError; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onStart() { super.onStart(); Branch branch = Branch.getInstance(); branch.initSession(new Branch.BranchReferralInitListener(){ @Override public void onInitFinished(JSONObject referringParams, BranchError error) { if (error == null) {
Sorry for the inconvenience!