1. Download our Official Android App: Forums for Android!

Apps Dynamically loading an external library at runtime

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

  1. mshoaib

    mshoaib Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Jul 29, 2013

    Jul 29, 2013
    2
    0
    5
    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.     }
     

    Advertisement

  2. out of ideas

    out of ideas Android Enthusiast
    Rank:
    None
    Points:
    93
    Posts:
    640
    Joined:
    May 19, 2012

    May 19, 2012
    640
    145
    93
    why are you trying to do it externally?
     
  3. mshoaib

    mshoaib Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Jul 29, 2013

    Jul 29, 2013
    2
    0
    5
    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
     

Share This Page

Loading...