title: How to integrate the Reflections library in android studio using gradle with save and collect
body: <p>My question is related to the <a href="https://github.com/ronmamo/reflections" rel="nofollow noreferrer">Reflections</a> library by @ronmamo on github and integrating this into my Android project to dynamically access all classes that inherit from a certain interface.</p>
  
  <p>I am not that familiar with gradle or maven so this is a learning process for me but i have reached a roadblock and do not know how to debug / find an answer to this one.</p>
  
  <p>As @ronmamo suggests <a href="https://github.com/ronmamo/reflections#integrating-into-your-build-lifecycle" rel="nofollow noreferrer">here</a>, I want to generate a xml file on build containing all scanned metadata and let Reflections collect it later when I use it in my code: </p>
  
  <blockquote>
    <p>Although scanning can be easily done on bootstrap time of your
    application - and shouldn't take long, it is sometime a good idea to
    integrate Reflections into your build lifecyle. With simple
    Maven/Gradle/SBT/whatever configuration you can save all scanned
    metadata into xml/json files just after compile time. Later on, when
    your project is bootstrapping you can let Reflections collect all
    those resources and re-create that metadata for you, making it
    available at runtime without re-scanning the classpath - thus reducing
    the bootstrapping time.</p>
  </blockquote>
  
  <p>I am not sure I fully understand where exactly in the entire process this "bootstrapping" takes place (in terms of the android app lifecycle etc. or even build time?) so I am not certain where exactly to call Reflections.collect(). Currently I am calling it at some point later in my app when the user has reached a certain point in the program.</p>
  
  <p>From several stackoverflow posts and the git readme files, I have come up with this for now: ([...] means removed unrelated code)</p>
  
  <p>build.gradle (Module:app):</p>
  
  <pre><code>dependencies {
      [...]
      compile 'org.reflections:reflections:0.9.11'
  }
  </code></pre>
  
  <p>build.gradle (Project: MyProject):</p>
  
  <pre><code>buildscript {
      repositories {
          jcenter()
          mavenCentral()
      }
      dependencies {
          classpath 'com.android.tools.build:gradle:2.3.3'
          classpath 'org.reflections:reflections:0.9.11'
      }
  }
  
  allprojects {
      repositories {
          jcenter()
      }
  }
  
  task runReflections {
      doLast {        
          org.reflections.Reflections("f.q.n").save("${sourceSet.main.output.classesDir}/META-INF/reflections/myproject-reflections.xml")
      }
  }
  
  task clean(type: Delete) {
      delete rootProject.buildDir
  }
  </code></pre>
  
  <p>And later on in my code (this class is reached at some point through user input, not loaded on app start):</p>
  
  <pre><code>Reflections reflections = Reflections.collect(); 
  Set&lt;Class&lt;? extends MyInterface&gt;&gt; allClasses = reflections.getSubTypesOf(MyInterface.class);
  </code></pre>
  
  <p>This generates the following exception since "reflections" is not instantiated and has the value of "null":</p>
  
  <pre><code>Attempt to invoke virtual method 'java.util.Set org.reflections.Reflections.getSubTypesOf(java.lang.Class)' on a null object reference
  </code></pre>
  
  <p>I understand that the generated .xml file resides on the computer where the build is happening, and I am not sure if this is also transferred to the android device so my guess is that is why this fails. But at what point does my Java code have access to this file before the apk is transferred and run on my android device?</p>
  
  <p>I have tried googling this in many different ways from different angles but I cannot seem to find a solution to make reflections work in Android. I understand the principle explained here and it seems better to generate the information in an xml file at build time to have the class information available at runtime. But how can I set this up properly?</p>
  
  <p>Thank you</p>