Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[android] Outdated configuration tutorial in readme (?) #21

Closed
tafelnl opened this issue Oct 2, 2020 · 7 comments · Fixed by #22
Closed

[android] Outdated configuration tutorial in readme (?) #21

tafelnl opened this issue Oct 2, 2020 · 7 comments · Fixed by #22

Comments

@tafelnl
Copy link
Contributor

tafelnl commented Oct 2, 2020

The official branch documentation tells you to install a set of dependencies. As seen in their documentation:

dependencies {
    ...
    // required for all Android apps
    implementation 'io.branch.sdk.android:library:5.+'
    // required if your app is in the Google Play Store (tip: avoid using bundled play services libs)
    implementation 'com.google.firebase:firebase-appindexing:19.0.0'
    implementation 'com.google.android.gms:play-services-ads-identifier:16+'
    // optional
    // Chrome Tab matching (enables 100% guaranteed matching based on cookies)
    implementation 'androidx.browser:browser:1.0.0'
    // Replace above with the line below if you do not support androidx
    // implementation 'com.android.support:customtabs:28.0.0'
    ...
}

This differs pretty much from what the docs of this library are stating.

Is that intentional? Or is this a mistake?

@tafelnl
Copy link
Contributor Author

tafelnl commented Oct 5, 2020

I ended up with the following configuration (androidx targeted):

android\app\build.gradle:

apply plugin: 'com.android.application'

android {
    defaultConfig {
        // ...
+       multiDexEnabled true
        // ...
+       resValue "string", "applink_host", "example.app.link"
+       resValue "string", "applink_host_alternate", "example-alternate.app.link"
+       resValue "string", "deeplink_scheme", "example"
+       resValue "string", "branch_api_key", "key_live_example"
+       resValue "string", "branch_test_mode", "false"
        // ...
    }
}

dependencies {
+    implementation 'com.android.support:multidex:1.0.3'

+    // Branch: required for all Android apps
+    implementation 'io.branch.sdk.android:library:5.0.3'

+    // Branch: required if your app is in the Google Play Store (tip: avoid using bundled play services libs)
+    implementation 'com.google.firebase:firebase-appindexing:19.1.0' // App indexing
+    implementation 'com.google.android.gms:play-services-ads:19.4.0' // GAID matching

+    // Branch: optional
+    implementation 'androidx.browser:browser:1.2.0' // Chrome Tab matching (enables 100% guaranteed matching based on cookies)
}

android\app\src\main\AndroidManifest.xml:

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

    <application
+       android:name=".CustomApplicationClass"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!-- ... -->

+       <meta-data
+           android:name="com.google.android.gms.ads.AD_MANAGER_APP"
+           android:value="true"/>

        <activity
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
            android:name="com.example.MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBarLaunch"
            android:launchMode="singleTask">

+           <intent-filter>
+               <action android:name="android.intent.action.MAIN" />
+               <category android:name="android.intent.category.LAUNCHER" />
+           </intent-filter>

+           <!-- BEGIN BRANCH -->
+           <intent-filter android:autoVerify="true">
+               <action android:name="android.intent.action.VIEW" />
+               <category android:name="android.intent.category.DEFAULT" />
+               <category android:name="android.intent.category.BROWSABLE" />
+               <data
+                   android:scheme="http"
+                   android:host="@string/applink_host" />
+               <data
+                   android:scheme="https"
+                   android:host="@string/applink_host" />
+               <data
+                   android:scheme="http"
+                   android:host="@string/applink_host_alternate" />
+               <data
+                   android:scheme="https"
+                   android:host="@string/applink_host_alternate" />
+           </intent-filter>
+
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
-               <data android:scheme="@string/custom_url_scheme"/>
+               <data android:scheme="@string/deeplink_scheme"/>
            </intent-filter>
+           <!-- END BRANCH -->

        </activity>

+       <!-- BEGIN BRANCH -->
+       <!-- Branch init -->
+       <meta-data android:name="io.branch.sdk.BranchKey" android:value="@string/branch_api_key" />
+       <meta-data android:name="io.branch.sdk.TestMode" android:value="@string/branch_test_mode" />
+       <!-- END BRANCH -->
+
        <!-- ... -->

    </application>

    <!-- ... -->

</manifest>

android\app\src\main\java\com\example\CustomApplicationClass.java:

+package com.example;
+
+import android.content.Context;
+import androidx.multidex.MultiDex;
+import androidx.multidex.MultiDexApplication;
+import io.branch.referral.Branch;
+
+public class CustomApplicationClass extends MultiDexApplication {
+
+  @Override
+  public void onCreate() {
+    super.onCreate();
+
+    // Branch logging for debugging
+    Branch.enableLogging();
+
+    // Branch object initialization
+    Branch.getAutoInstance(this);
+  }
+
+  @Override
+  protected void attachBaseContext(Context base) {
+    super.attachBaseContext(base);
+    MultiDex.install(this);
+  }
+}

android\app\src\main\java\com\example\MainActivity.java:

package com.example;

import android.content.Intent;
import android.os.Bundle;
import co.boundstate.BranchDeepLinks;

import com.getcapacitor.BridgeActivity;
import com.getcapacitor.Plugin;
import java.util.ArrayList;

public class MainActivity extends BridgeActivity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initializes the Bridge
    this.init(
        savedInstanceState,
        new ArrayList<Class<? extends Plugin>>() {

          {
            // Additional plugins you've installed go here
            // Ex: add(TotallyAwesomePlugin.class);
+           add(BranchDeepLinks.class);
          }
        }
      );
  }
+
+  @Override
+  protected void onNewIntent(Intent intent) {
+    this.setIntent(intent);
+    super.onNewIntent(intent);
+  }
}

Optionally you can probably safely delete the following:

android\app\src\main\res\values\strings.xml

<?xml version='1.0' encoding='utf-8'?>
<resources>
    <!-- ... _>
-   <string name="custom_url_scheme">com.example</string>
</resources>

Some clarifications

To me this configuration looks more up to date than the one in the readme. It might be that I have done some redundant configuration. But it works 100% now. So better safe than sorry.

androidx + multidex

I have made the choice to target AndroidX and support multidex. You can read more about multidex here: https://stackoverflow.com/questions/33588459/what-is-android-multidex It depends on your use case, but if you have a lot of dependencies installed like GM Services, Branch and Facebook for example, it seems like the right choice.

default custom_url_scheme by capacitor

Also, Capacitor has deeplink url schemes configured by default within Android (hence the custom_url_scheme variable (which defaults to your package name) in strings.xml. I think it does not make sense to keep this in your project when you use Branch, but that is up to you to decide. In the config above it is dropped however by simply removing the lines:

- <data android:scheme="@string/custom_url_scheme"/>
- <string name="custom_url_scheme">com.example</string>

Basically the only thing this does is removing the scheme com.example:// (ergo: your package name) from the supported schemes. Not very important, just some cleaning up.

@tafelnl tafelnl changed the title [android] Outdated dependencies in gradle (?) [android] Outdated configuration tutorial in readme (?) Oct 5, 2020
@rahulMypcot
Copy link

getting this error " error: failed processing manifest."

@raosaddam
Copy link

raosaddam commented May 26, 2021

I'm getting error in MainActivity.java

2021-05-26

package co.boundstate does not exist
cannot find symbol class BranchDeepLinks

@tafelnl
Copy link
Contributor Author

tafelnl commented May 26, 2021

@raosaddam You can try the following:

In Android Studio click File > Sync Project with Gradle Files and try to build again.

@raosaddam
Copy link

@tafelnl Thank you for replying.
That didn't work either. Still same error

@tafelnl
Copy link
Contributor Author

tafelnl commented May 27, 2021

Ah I see, the docs are missing the following steps:

  • run npm i capacitor-branch-deep-links
  • run npx cap sync
  • In Android Studio click File > Sync Project with Gradle Files and try to build again

@raosaddam
Copy link

That worked. Thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants