1

I was trying to run my part of a code, where I get method startDownload from external APK on my sdcard via DexClassLoader. In onCreate method of my activity I am trying to invoke this activity, that means I want to invoke download. Source code of main (DownloadManagerActivity):

         try {

            DexClassLoader cl = new DexClassLoader("/sdcard/GingerbreadDownload.apk", 
                    getFilesDir().getAbsolutePath(),
                    null,  
                    DownloadManagerActivity.class.getClassLoader());


            Class<?> n = cl.loadClass("com.gingerbread.download.Download23");
            Method m=n.getDeclaredMethod("startDownload", null);
            m.invoke(this,(Object[]) null );   

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Source code of Download23.java:

public class Download23 {


public static void startDownload() {
    DownloadManager mgr=null;
    long lastDownload;
    String downloadedAPKName="myApp";
    Uri uri=Uri.parse("http://someuri.com/test.apk");

    Environment
        .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
        .mkdirs();

    lastDownload = mgr.enqueue(new DownloadManager.Request(uri)
                                .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
                                                                                DownloadManager.Request.NETWORK_MOBILE)
                                .setAllowedOverRoaming(false)
                                .setTitle("Title")
                                .setDescription("Info")
                                .setShowRunningNotification(true)
                                .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, downloadedAPKName));
}


BroadcastReceiver onComplete=new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {    

        File file = new File(Environment.getExternalStorageDirectory()+"/Download/", "myApp");

        Intent intenta = new Intent(Intent.ACTION_VIEW);
        intenta.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        context.startActivity(intenta);


    }


};

}

I am getting error java.lang.reflect.InvocationTargetException in line m.invoke(this,(Object[]) null ); in my main. I really don't know, what mistake can be there. I have tried it with simple method, and connection to my "external" method is OK. But I am not able to launch download. This could be a tricky, but really I am stuck with it right now. Does anyone have any ideas?

Waypoint
  • 17,283
  • 39
  • 116
  • 170
  • 1
    Most of the other examples using DexClassLoader are using a .jar as a source, and not an APK as you are. Could it be related to that the .apk is signed? – dparnas Sep 22 '11 at 11:32
  • Thanks for comment. I can use DexClassLoader, both with JAR and APK. Problem was what, I needed to use my context appropriately – Waypoint Sep 22 '11 at 12:06

0 Answers0