> ## Documentation Index
> Fetch the complete documentation index at: https://help.airbridge.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Hybrid App Setup - Android SDK

<Info>
  **Attention**

  Before proceeding with the hybrid app setup, install the [Android SDK](/en/developers/android-sdk-v4) and [Web SDK](/en/developers/web-sdk).
</Info>

You can configure the Android SDK to handle Airbridge-related tasks occurring within the in-app website of a hybrid app without modifying the website's code.

## Handle Web SDK Commands with the Android SDK

The Android SDK can handle the commands related to event transmission, device settings, and user settings carried out by the Web SDK. Configure the `Airbridge.setWebInterface` function before displaying the website in the web view.

`YOUR_WEB_TOKEN` is the [Web SDK token](/en/guides/tokens#web-sdk-token). You can find it on the **\[Settings]>\[Tokens]** page in the Airbridge dashboard.

<CodeGroup>
  ```kotlin Kotlin lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  class MainActivity : Activity() {
      
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          initWebView()
      }
      
      fun initWebView() {
          webView.settings.javaScriptEnabled = true
          webView.settings.domStorageEnabled = true
          
          Airbridge.setWebInterface(webView, "YOUR_WEB_TOKEN")

          webView.webChromeClient = WebChromeClient()
          webView.webViewClient =  WebViewClient()
          webView.loadUrl("https://my_company.com/main")
      }
  }
  ```

  ```java Java lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  public class MainActivity extends Activity {
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          initWebView();
      }
      
      void initWebView() {
          webView = findViewById(R.id.webView);
          webView.getSettings().setJavaScriptEnabled(true);
          webView.getSettings().setDomStorageEnabled(true);

          Airbridge.setWebInterface(webView, "YOUR_WEB_SDK_TOKEN");    
      
          webView.setWebChromeClient(new WebChromeClient());
          webView.setWebViewClient(new WebViewClient());
          webView.loadUrl("https://dev.blog.airbridge.io/websdk-web-app/");
      }
  }
  ```
</CodeGroup>

## Enable Deep Links in Web View

Add the [Airbridge.click](/en/developers/android-sdk-v4#%EC%9D%B8%EC%95%B1%EC%97%90%EC%84%9C-%ED%8A%B8%EB%9E%98%ED%82%B9-%EB%A7%81%ED%81%AC%EB%A5%BC-%ED%81%B4%EB%A6%AD) function to enable deep links in the web view. The web view does not inherently support deep links. Therefore, the `Airbridge.click` function must be configured so that the SDK can handle the deep link embedded in the tracking link.

The `Airbridge.click` function returns `true` when the input address is a tracking link. When the app is launched through that address, the `onSuccess` function is called. If there is no internet connection or a different app is launched, the `onFailure` function is called.

When the input address is not a tracking link, the `Airbridge.click` function returns `false` , and no further action is executed.

<CodeGroup>
  ```kotlin Kotlin lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  fun click(
      trackingLink: String,
      onSuccess: OnSuccess<Unit>? = null,
      onFailure: OnFailure? = null
  ): Boolean
  ```

  ```java Java lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  boolean click(
      @NonNull String trackingLink,
      @Nullable OnSuccess<Unit> onSuccess,
      @Nullable OnFailure onFailure
  )
  ```
</CodeGroup>

Implementing `WebViewClient#shouldOverrideUrlLoading` in the `WebViewClient` allows you to determine whether to continue or cancel loading a specific URL after it is detected. For more information, refer to the [Android Developer Guide](https://developer.android.com/reference/android/webkit/WebViewClient#shouldOverrideUrlLoading\(android.webkit.WebView,%20android.webkit.WebResourceRequest\)).

<CodeGroup>
  ```kotlin Kotlin lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  private val webViewClient = object : WebViewClient() {

      override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
          return super.shouldOverrideUrlLoading(view, url)
      }

      @RequiresApi(Build.VERSION_CODES.N)
      override fun shouldOverrideUrlLoading(
          view: WebView?,
          request: WebResourceRequest?
      ): Boolean {
          return super.shouldOverrideUrlLoading(view, request)
      }
  }

  private fun initWebview() {
      webView.webViewClient = webViewClient
  }
  ```

  ```java Java lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  private WebViewClient webViewClient = new WebViewClient() {

      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          return super.shouldOverrideUrlLoading(view, url);
      }

      @Override
      @RequiresApi(Build.VERSION_CODES.N)
      public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
          return super.shouldOverrideUrlLoading(view, request);
      }
  };

  private void initWebview() {
      webView.setWebViewClient(webViewClient);
  }
  ```
</CodeGroup>

During loading, if the specific URL is an Airbridge tracking link, you can use the `Airbridge.click` function to send the `webView#shouldOverrideUrlLoading` result as `true`. This allows the Airbridge Android SDK to open the tracking link instead of the web view.

The URL host of Airbridge tracking links are `abr.ge`, `YOUR_APP_NAME.airbridge.io`, or a custom domain.

<CodeGroup>
  ```kotlin Kotlin lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  private val webViewClient = object : WebViewClient() {

      private fun handleDomain(view: WebView, uri: Uri): Boolean =
          // return true when uri is tracking link
          // return false when uri is not tracking link
          Airbridge.click(uri.toString(), {
              // on success
              // do nothing
          }, {
              // on failure
              // when uri is another app's tracking link or sdk fails to open uri
              view.loadUrl(uri.toString())
          })

      override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
          if (view === null) return false
          if (url == null) return false

          val uri = Uri.parse(url) ?: return false
          return handleDomain(view, uri)
      }

      @RequiresApi(Build.VERSION_CODES.N)
      override fun shouldOverrideUrlLoading(
          view: WebView?,
          request: WebResourceRequest?
      ): Boolean {
          if (view === null) return false

          val uri = request?.url ?: return false
          return handleDomain(view, uri)
      }
  }
  ```

  ```java Java lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  private WebViewClient webViewClient = new WebViewClient() {

      private boolean handleDomain(@NonNull WebView view, @NonNull Uri uri) {
          // return true when uri is tracking link
          // return false when uri is not tracking link
          return Airbridge.click(uri.toString()
                  , result -> {
                      // on success
                      // do nothing
                  }, throwable -> {
                      // on failure
                      // when uri is another app's tracking link or sdk fails to open uri
                      view.loadUrl(uri.toString())
                  }
          );
      }

      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          if (view == null) return false;
          if (url == null) return false;

          Uri uri = Uri.parse(url);
          if (uri == null) return false;

          return handleDomain(view, uri);
      }

      @Override
      @RequiresApi(Build.VERSION_CODES.N)
      public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
          if (view == null) return false;
          if (request == null) return false;

          Uri uri = request.getUrl();
          if (uri == null) return false;

          return handleDomain(view, uri);
      }
  };
  ```
</CodeGroup>

## Attention

<Accordion title="In-app web view event collection">
  Note that with the hybrid app setup, events that occur in the in-app web view must be collected through the Web SDK only. If collected through the App SDK as well, event duplication will occur. If your in-app web view uses a live mobile website, utilize only the Web SDK for event collection.
</Accordion>

<link rel="alternate" hrefLang="en" href="https://help.airbridge.io/en/developers/hybrid-app-settings-android-sdk-v4" />

<link rel="alternate" hrefLang="ko" href="https://help.airbridge.io/ko/developers/hybrid-app-settings-android-sdk-v4" />
