Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Branch.io tutorial: Branch.getAutoInstance(this);

Tags:

branch.io

I am trying to get Branch.io to work on Android, but I am running into:

myapplication.MainActivity cannot be cast to android.app.Application

I then changed:

Branch.getAutoInstance(this);

To:

Branch.getInstance();


In onCreate of the Activity.

Then I get:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean io.branch.referral.Branch.initSession(io.branch.referral.Branch$BranchReferralInitListener, android.net.Uri, android.app.Activity)' on a null object reference
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)

Can you help me get the basic up and running?

Following is my AndroidManifest.xml: (note: the branch_key is added in my app code)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.x.myapplication">

    <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">

        <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_xxxxxxxxxxxxxxx" />

        <activity android:name=".MainActivity">
            <intent-filter>
                <data android:scheme="yourapp" android:host="open" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </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>


My main Activity:

package com.example.chg.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.util.Log;

import org.json.JSONObject;

import io.branch.referral.Branch;
import io.branch.referral.BranchError;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Branch.getAutoInstance(this);
        Branch.getInstance();
        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) {
                    // params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
                    // params will be empty if no data found
                    // ... insert custom logic here ...
                } else {
                    Log.i("MyApp", error.getMessage());
                }
            }
        }, this.getIntent().getData(), this);
    }

    @Override
    public void onNewIntent(Intent intent) {
        this.setIntent(intent);
    }
}
like image 752
Chris G. Avatar asked May 27 '16 11:05

Chris G.


People also ask

How to use Branch io in android?

Branch.Io Integration On the Branch.io dashboard create your account,use link https://dashboard.branch.io/login. After the account was created, set up an account for android. As we see on the dashboard in account settings, we will get branch Key and branch Secret, copy them, and add them into the Android string.

What is branch in android?

The Branch Android SDK for deep linking and attribution. Branch helps mobile apps grow with deep links / deeplinks that power paid acquisition and re-engagement campaigns, referral programs, content sharing, deep linked emails, smart banners, custom user onboarding, and more. docs.branch.io/pages/apps/android/


1 Answers

Alex with Branch.io here:

We recently made some changes to our tutorial, and it looks like we missed a few things. I appreciate you posting about this — we'll be pushing an update later today for more clarity.

In this particular case, there are two issues:

  1. A mixup between the Application onCreate() and Activity onCreate() methods, neither of which are actually needed for a basic implementation.
  2. A missing Application class (we accidentally deleted this step from our tutorial completely — my apologies).

To get up and running, update your files as follows:

AndroidManifest.xml

You have three options here:

1. Use the Branch application class (easiest)

If you don't already have a custom application class, this is the simplest approach. Add android:name="io.branch.referral.BranchApp" to your Application class:

Edit: snippet UPDATED per 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">

        <!--Enable test mode to see debugging data
         (https://dev.branch.io/getting-started/integration-testing/guide/android/#use-debug-mode-to-simulate-fresh-installs)-->
        <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 your Application class with the BranchApp class

If you already have a custom Application class, this is the simplest 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 custom Application class (CustomApplicationClass in the example above) will look like this:

public final class CustomApplicationClass extends YourApp {
    @Override
    public void onCreate() {
        super.onCreate();
    }
}

3. Integrate directly into your custom application class

The most custom approach, for advanced implementations. You'd have your AndroidManifext.xml file set up 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 Definition

Remove the onCreate() calls. They aren't needed here and are actually the cause of your error message (Branch.getAutoInstance(this) was passing the Activity context as 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) {
                    // params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
                    // params will be empty if no data found
                    // ... insert custom logic here ...
                } else {
                    Log.i("MyApp", error.getMessage());
                }
            }
        }, this.getIntent().getData(), this);
    }

    @Override
    public void onNewIntent(Intent intent) {
        this.setIntent(intent);
    }
}

Sorry for the inconvenience!

like image 149
Alex Bauer Avatar answered Oct 21 '22 11:10

Alex Bauer