Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to dynamically load a Activity class from a jar library on the sdCard & actually use it?

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);
like image 758
pulancheck1988 Avatar asked Oct 19 '12 14:10

pulancheck1988


1 Answers

I got similar scenarios.

If u do not put below codes in AndroidManifest.xml

<activity android:name="com.appno1.futureLib.LibActivity" android:label="@string/app_name" > </activity>

U will get below error.

10-21 17:04:25.860: E/AndroidRuntime(11050): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {... ...}; have you declared this activity in your AndroidManifest.xml?

But if u register the activity in your client application, then java.lang.ClassNotFoundException will be got.

The loader tried to search com.appno1.futureLib.LibActivity in [/data/app/com.appno2-1.apk]. Obviously this is not expected. I believed com.appno.futureLib.LibActivity is located in the .jar file.

If there is a way to let the loader to search LibActivity in the .jar file, the problem may be solved.

like image 74
whogiawho Avatar answered Oct 06 '22 15:10

whogiawho