title: ClassNotFoundException: org.jacoco.agent.rt.RT
body: <p>I want to use Jacoco to collect code coverage for manually testing my Android apps. What I did was implement a Broadcast Receiver and ask Jacoco to collect coverage data when receiving the intent. Here is my receiver code:</p>
  
  <pre><code>public class CoverageReceiver extends BroadcastReceiver {
  
      public static String TAG = "JacocoInstrumentation:";
      private static final String DEFAULT_COVERAGE_FILE_PATH = "/sdcard/coverage.ec";
  
  
      private static final boolean LOGD = true;
  
      private String mCoverageFilePath;
  
      @Override
      public void onReceive(Context context, Intent intent) {
          generateCoverageReport();
      }
  
      private void generateCoverageReport() {
          if (LOGD)
              Log.d(TAG, "generateCoverageReport()");
  
          java.io.File coverageFile = new java.io.File(getCoverageFilePath());
          OutputStream out = null;
          // We may use this if we want to avoid refecltion and we include
          // emma.jar
          //RT.dumpCoverageData(coverageFile, false, false);
          // Use reflection to call emma dump coverage method, to avoid
          // always statically compiling against emma jar
          try {
              Object agent = Class.forName("org.jacoco.agent.rt.RT")
                      .getMethod("getAgent")
                      .invoke(null);
              out = new FileOutputStream(coverageFile,false);
              out.write((byte[]) agent.getClass().getMethod("getExecutionData", boolean.class)
                      .invoke(agent, false));
          } catch (ClassNotFoundException e) {
              reportJacocoError("Jacoco.jar not in the class path?", e);
          } catch (SecurityException e) {
              reportJacocoError(e);
          } catch (NoSuchMethodException e) {
              reportJacocoError(e);
          } catch (IllegalArgumentException e) {
              reportJacocoError(e);
          } catch (IllegalAccessException e) {
              reportJacocoError(e);
          } catch (InvocationTargetException e) {
              reportJacocoError(e);
          } catch (FileNotFoundException e){
              reportJacocoError(e);
          } catch (IOException e) {
              reportJacocoError(e);
          } finally {
              if (out != null) {
                  try {
                      out.close();
                  } catch (IOException e) {
                      reportJacocoError(e);
                  }
              }
          }
      }
  
      public String getCoverageFilePath() {
          if (mCoverageFilePath == null) {
              return DEFAULT_COVERAGE_FILE_PATH;
          } else {
              return mCoverageFilePath;
          }
      }
  
      public boolean setCoverageFilePath(String filePath){
          if(filePath != null &amp;&amp; filePath.length() &gt; 0) {
              mCoverageFilePath = filePath;
              return true;
          }
          return false;
      }
      private void reportJacocoError(Exception e) {
          reportJacocoError("", e);
      }
  
      private void reportJacocoError(String hint, Exception e) {
          String msg = "Failed to generate Jacoco coverage. " + hint;
          Log.e(TAG, msg, e);
      }
  
  
  }
  </code></pre>
  
  <p>I also add apply the <code>plugin: 'jacoco'</code> and <code>testCoverageEnabled = true</code> in the Gradle file. </p>
  
  <p>I have seen a few people do this, so it should work. However, when I tried to broadcast the triggering intent and my receiver began to work, I got a ClassNotFoundException:</p>
  
  <pre><code>java.lang.ClassNotFoundException: org.jacoco.agent.rt.RT
  ...
  Caused by: java.lang.ClassNotFoundException: Didn't find class "org.jacoco.agent.rt.RT" on path:
  ...
  Suppressed: java.lang.ClassNotFoundException: org.jacoco.agent.rt.RT
  ...
  Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available      
  </code></pre>
  
  <p>I have tried to specify the Jacoco version, or add Jacoco to the dependencies, but none of that worked. So what should I do?</p>