I have 1 .jar file on sdCard (i used dx tool to compile a usual .jar to dex.. otherwise android will complain at runtime)
The class that i want to use is actually a Activity & by "use" i mean Inflate!
A) i've noticed that if i load class like this, loaded class will have as package parrent the package of parrent-class (the one in which i run the code: example: real-package of class in jar library: "com.something" & my-app-package: "com.wonderfull" -> loaded class is in "com.wonderfull.com.something" :) ) -> retested: it keeps the original package, just as in the library
B) In "best" case scenario i get this error:
Unable to instantiate activity ComponentInfo{com.appno2/com.appno1.futureLib.LibActivity}: java.lang.ClassNotFoundException: com.appno1.futureLib.LibActivity in loader dalvik.system.PathClassLoader[/data/app/com.appno2-1.apk]
C) In manifest i've placed Activity (the one to be loaded dinamically) like this:
activity android:name="com.appno1.futureLib.LibActivity" android:label="@string/app_name" > /activity>
So i repeat question from title: is it possible to dinamically load a Activity class from a jar library on the sdCard & actually use it ?
In case of "Why would you want this?" - just curios! + as a concept: the posibility to download actual code of app (in the form of a jar of something) just after user entered a valid licence
Update: - this ideea works with fragments (meaning class to be imported is a fragment)
Current conclusion: - it is possible to have: lame java classes or even android UI related classes in a external .jar (on sdCard for example, or to be downloaded from internet..etc) & load them dinamically at need. Oh yeah: you cant work this way this activities :( .. probably not even services..
More info #1: Is it possible to dynamically load a library at runtime from an Android application?
More info #2: http://android-developers.blogspot.ro/2011/07/custom-class-loading-in-dalvik.html
Regardin URLClassLoader: this works (without involving DalvikClassLoader - ClassNotFoundException && obviosly: still cant inflate a activity loaded like this)
File file = new File("/sdcard/myLib.jar");
File tmpDir = getDir("dex", 0);
ClassLoader loader = new URLClassLoader(new URL[]{file.toURI().toURL()}, this.getClass().getClassLoader());
DexClassLoader classloader = new DexClassLoader(file.getAbsolutePath(), tmpDir.getAbsolutePath(), null, loader);
Class<Object> classref = (Class<Object>) classloader.loadClass("packageInLib.ClassName");
Object myInstance = classref.getConstructor().newInstance();
Or instead of "loadClass"
Class<?> classref = Class.forName("packageInLib.ClassName", true, dalvikClassLoader);