title: Loading an encrypted apk and decrypting on the fly
body: <p>I want to dynamically load an encrypted jar file. I have loaded a Dex file dynamically and it works fine. I have tried it both on emulator and my android device. Now i want to load the encrypted file. As far as i understand i will have to customize the Class-loader so that it can first decrypt the file on the fly before executing it.</p>
  
  <p>I will be glad if someone can guide me how i will actually implement this. I have an idea but m not an experienced programmer.</p>
  
  <p>Thanks in advance</p>
  
  <p>public class MainActivity extends Activity {</p>
  
  <pre><code>@SuppressWarnings("unchecked")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      try {
          final String libPath = Environment.getExternalStorageDirectory() + "/shoaib.jar";
          final File tmpDir = getDir("dex", 0);
  
          final DexClassLoader classloader = new DexClassLoader(libPath, tmpDir.getAbsolutePath(), null, this.getClass().getClassLoader());
          final Class&lt;Object&gt; classToLoad = (Class&lt;Object&gt;) classloader.loadClass("com.example.custom.MyClass");
  
          final Object myInstance  = classToLoad.newInstance();
          final Method doSomething = classToLoad.getMethod("doSomething");
  
          doSomething.invoke(myInstance);
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  </code></pre>
  
  <p>}</p>