1. Are you ready for the Galaxy S20? Here is everything we know so far!

Dynamically loading an external library at runtime

Discussion in 'Android Development' started by mshoaib, Jul 29, 2013.

  1. mshoaib

    mshoaib Lurker
    Thread Starter

    I am using and android 2.2 phone for testing and getting an error which is driving me crazy :(

    07-27 01:24:55.692: W/System.err(14319): java.lang.ClassNotFoundException: com.shoaib.AndroidCCL.MyClass in loader dalvik.system.DexClassLoader@43abbc20

    Can someone help me what to do now..i have tried various suggested solutions on different posts

    First I wrote the following class:

    Code (Text):
    1.  package org.shoaib.androidccl;
    2.  
    3.     import android.util.Log;
    4.  
    5.     public class MyClass {
    6.         public MyClass() {
    7.             Log.d(MyClass.class.getName(), "MyClass: constructor called.");
    8.         }
    9.    
    10.         public void doSomething() {
    11.             Log.d(MyClass.class.getName(), "MyClass: doSomething() called.");
    12.         }
    13.     }
    And I packaged it in a DEX file that I saved on my device's SD card as `/sdcard/testdex.jar`.

    Then I wrote the program below, after having removed `MyClass` from my Eclipse project and cleaned it:

    Code (Text):
    1. public class Main extends Activity {
    2.    
    3.         [MENTION=1299008]supp[/MENTION]ressWarnings("unchecked")
    4.         [MENTION=439709]override[/MENTION]
    5.         protected void onCreate(Bundle savedInstanceState) {
    6.             super.onCreate(savedInstanceState);
    7.             setContentView(R.layout.main);
    8.  
    9.             try {
    10.                 final String libPath = Environment.getExternalStorageDirectory() + "/testdex.jar";
    11.                 final File tmpDir = getDir("dex", 0);
    12.  
    13.                 final DexClassLoader classloader = new DexClassLoader(libPath, tmpDir.getAbsolutePath(), null, this.getClass().getClassLoader());
    14.                 final Class<Object> classToLoad = (Class<Object>) classloader.loadClass("org.shoaib.androidccl.MyClass");
    15.  
    16.                 final Object myInstance  = classToLoad.newInstance();
    17.                 final Method doSomething = classToLoad.getMethod("doSomething");
    18.            
    19.                 doSomething.invoke(myInstance);
    20.            
    21.             } catch (Exception e) {
    22.                 e.printStackTrace();
    23.             }
    24.         }
    25.     }
     


  2. out of ideas

    out of ideas Android Enthusiast

    why are you trying to do it externally?
     
  3. mshoaib

    mshoaib Lurker
    Thread Starter

    Its something that i can utilize later on in my project..just wanted to test if it works or not..i assume i didnt correctly use dx to convert .class file
     
Loading...
Similar Threads - Dynamically loading external
  1. amazijn
    Replies:
    2
    Views:
    151
  2. Glepp
    Replies:
    0
    Views:
    205
  3. shan syed
    Replies:
    0
    Views:
    66
  4. Gopalakrishnanandroid
    Replies:
    0
    Views:
    276
  5. kk63852
    Replies:
    0
    Views:
    419
  6. Brian Berry
    Replies:
    3
    Views:
    433
  7. jackraj25
    Replies:
    3
    Views:
    648
  8. harsh_p
    Replies:
    0
    Views:
    1,702
  9. Cofyka
    Replies:
    0
    Views:
    347
  10. Flizzy
    Replies:
    0
    Views:
    321

Share This Page

Loading...