title: SecurityException: <<other_package>> from uid xxx not allowed to perform READ_CLIPBOARD when calling setPrimaryClip
body: <p>I've been getting reports of the following crash</p>
  <pre class="lang-java prettyprint-override"><code>SecurityException: &lt;&lt;other_package&gt;&gt; from uid xxx not allowed to perform READ_CLIPBOARD
  </code></pre>
  <p>The crash occurred when a user clicks on a button that copies a text into clipboard as shown below.</p>
  <pre class="lang-kotlin prettyprint-override"><code>val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
  val clip = ClipData.newPlainText(&quot;label&quot;, shareUrl)
  clipboard.setPrimaryClip(clip)
  </code></pre>
  <p>So my app doesn't try to read anything from clipboard. Really confusing why this error could happen.</p>
  <p>Does anyone know how this crash happen and how can I fix this?</p>
  <p><strong>Additional Information</strong></p>
  <p>This crash only happens in Android 9 and Android 10 and not happens easily (only 6 users from 200k monthly active users)</p>
  <p>I only saw two &lt;&lt;other_package&gt;&gt; in Crashlytics (one is a Bank app and another is a Music app).</p>
  <p>I tried to read the source code of <code>ClipboardService.java</code> and <code>AppOpsManager.java</code> and found that the crash might came from <code>noteOp</code> in <code>AppOpsManager</code>.</p>
  <p>Here is the stack trace of the crash:</p>
  <pre><code>Fatal Exception: java.lang.SecurityException: &lt;&lt;other_package&gt;&gt; from uid xxx not allowed to perform READ_CLIPBOARD
         at android.os.Parcel.createException(Parcel.java:2087)
         at android.os.Parcel.readException(Parcel.java:2055)
         at android.os.Parcel.readException(Parcel.java:2003)
         at android.content.IClipboard$Stub$Proxy.setPrimaryClip(IClipboard.java:293)
         at android.content.ClipboardManager.setPrimaryClip(ClipboardManager.java:106)
         at my.package.MyClass.copyToClipboard(MyClass.java:63)
         at android.view.View.performClick(View.java:7375)
         at android.view.View.performClickInternal(View.java:7336)
         at android.view.View.access$3900(View.java:822)
         at android.view.View$PerformClick.run(View.java:28214)
         at android.os.Handler.handleCallback(Handler.java:883)
         at android.os.Handler.dispatchMessage(Handler.java:100)
         at android.os.Looper.loop(Looper.java:238)
         at android.app.ActivityThread.main(ActivityThread.java:7829)
         at java.lang.reflect.Method.invoke(Method.java)
         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:986)
  
  Caused by android.os.RemoteException: Remote stack trace:
      at android.app.AppOpsManager.noteOp(AppOpsManager.java:2568)
      at com.android.server.clipboard.ClipboardService.clipboardAccessAllowed(ClipboardService.java:933)
      at com.android.server.clipboard.ClipboardService.setPrimaryClipInternal(ClipboardService.java:775)
      at com.android.server.clipboard.ClipboardService.setPrimaryClipInternal(ClipboardService.java:710)
      at com.android.server.clipboard.ClipboardService$ClipboardImpl.setPrimaryClip(ClipboardService.java:358)
  
  </code></pre>