title: How to create a Firebase short dynamic link
body: <p>I'm successfully creating a Firebase dynamic link in Java on Android. My code to do so is in a button click listener.</p>
  <pre><code>shareButton.setOnClickListener(new View.OnClickListener()
  {
      @Override
      public void onClick(View v)
      {
          DynamicLink dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
              .setLink(Uri.parse(&quot;https://www.mycompany.com/&quot;))
              .setDomainUriPrefix(&quot;https://mycompany.page.link/test&quot;)
              .setAndroidParameters(
                  new DynamicLink.AndroidParameters.Builder(&quot;com.mycompany.app&quot;)
                      .setFallbackUrl(Uri.parse(&quot;https://www.mycompany.com/&quot;))
                      .setMinimumVersion(1)
                      .build())
              .buildDynamicLink();
  
          Uri dynamicLinkUri = dynamicLink.getUri();
          shareDynamicLink(dynamicLinkUri);
      }
  });
  
  public void shareDynamicLink(Uri dynamicLink)
  {
      Intent shareIntent = new Intent();
      String msg = &quot;Check this out: &quot; + dynamicLink;
      shareIntent.setAction(Intent.ACTION_SEND);
      shareIntent.putExtra(Intent.EXTRA_TEXT, msg);
      shareIntent.setType(&quot;text/plain&quot;);
      startActivity(shareIntent);
  }
  </code></pre>
  <p>This sends a LONG dynamic link that works just fine. Now I'd like to shorten the link, so I replaced the 'shareDynamicLink' method with this code.</p>
  <pre><code>public void shareDynamicLink(Uri dynamicLink)
  {
      Task&lt;ShortDynamicLink&gt; shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
          .setLongLink(dynamicLink)
          .buildShortDynamicLink()
          .addOnCompleteListener(Objects.requireNonNull(this.getActivity()), new OnCompleteListener&lt;ShortDynamicLink&gt;()
          {
              @Override
              public void onComplete(@NonNull Task&lt;ShortDynamicLink&gt; task)
              {
                  if (task.isSuccessful())
                  {
                      // Short link created
                      Uri shortLink = Objects.requireNonNull(task.getResult()).getShortLink();
                      Uri flowchartLink = task.getResult().getPreviewLink();
  
                      Log.e(&quot;DynamicLink&quot;, &quot;shortLink: &quot; + shortLink + System.lineSeparator());
                      Log.e(&quot;DynamicLink&quot;, &quot;flowChartLink: &quot; + flowchartLink + System.lineSeparator());
  
                      Intent shareIntent = new Intent();
                      String msg = &quot;Check this out: &quot; + shortLink;
                      shareIntent.setAction(Intent.ACTION_SEND);
                      shareIntent.putExtra(Intent.EXTRA_TEXT, msg);
                      shareIntent.setType(&quot;text/plain&quot;);
                      startActivity(shareIntent);
                  }
                  else
                  {
                      Toast.makeText(context, &quot;Failed to share event.&quot;, Toast.LENGTH_SHORT).show();
                  }
              }
          });
  }
  </code></pre>
  <p>This second method produces an error that I don't understand.</p>
  <pre><code>&quot;400: Cannot shorten a short Dynamic Link:
  https://mycompany.page.link/test?afl=https%3A%2F%2Fwww.mycompany.com%2F&amp;amv=1
  &amp;apn=com.mycompany.app&amp;ibi=com.mycompany.app&amp;ifl=https%3A%2F%2F
  www.mycompany.com%2F&amp;isi=963543827&amp;ipfl=https%3A%2F%2F
  www.mycompany.com%2F&amp;link=https%3A%2F%2Fwww.mycompany.com%2F
  [https://firebase.google.com/docs/dynamic-links/rest#create_a_short_link_from_parameters]
  
  </code></pre>
  <p>What am I missing here? This seems like it should work.</p>
  <p>Note: I don't need the long dynamic link, just the short one. I tried changing the onClickListener as follows.</p>
  <pre><code>shareButton.setOnClickListener(new View.OnClickListener()
  {
      @Override
      public void onClick(View v)
      {
          Task&lt;ShortDynamicLink&gt; dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
              .setLink(Uri.parse(&quot;https://www.mycompany.com/&quot;))
              .setDomainUriPrefix(&quot;https://mycompany.page.link/test&quot;)
              .setAndroidParameters(
                  new DynamicLink.AndroidParameters.Builder(&quot;com.mycompany.app&quot;)
                      .setFallbackUrl(Uri.parse(&quot;https://www.mycompany.com/&quot;))
                      .setMinimumVersion(1)
                      .build())
              .buildShortDynamicLink()
              .addOnCompleteListener(Objects.requireNonNull(getActivity()), new OnCompleteListener&lt;ShortDynamicLink&gt;()
              {
                  @Override
                  public void onComplete(@NonNull Task&lt;ShortDynamicLink&gt; task)
                  {
                      if (task.isSuccessful())
                      {
                          Uri shortLink = Objects.requireNonNull(task.getResult()).getShortLink();
                          Uri flowchartLink = task.getResult().getPreviewLink();
  
                          Log.e(&quot;DynamicLink&quot;, &quot;shortLink: &quot; + shortLink + System.lineSeparator());
                          Log.e(&quot;DynamicLink&quot;, &quot;flowChartLink: &quot; + flowchartLink + System.lineSeparator());
                      }
                      else
                      {
                          Log.e(&quot;DynamicLink&quot;, &quot;Link failed: &quot; + task.getException().getMessage() + System.lineSeparator());
                      }
                  }
              });
  
      }
  });
  </code></pre>
  <p>But I still get the same 400 error.</p>
  <pre><code>400: Cannot shorten a short Dynamic Link:
  https://mycompany.page.link/test?afl=https%3A%2F%2Fwww.mycompany.com%2F&amp;amv=1
  &amp;apn=com.mycompany.app&amp;ibi=com.mycompany.app&amp;ifl=https%3A%2F%2F
  www.mycompany.com%2F&amp;isi=963543827&amp;ipfl=https%3A%2F%2Fwww.mycompany.com%2F
  &amp;link=https%3A%2F%2Fwww.mycompany.com%2F
  [https://firebase.google.com/docs/dynamic-links/rest#create_a_short_link_from_parameters]
  </code></pre>