I am trying to use reflection instead of including a jar file. I want to do this to reduce my apk size when I am not using the jar code…but if I include the code, I must include the jar. That being said, I am trying to grasp reflection and not doing well.
I have this code to see the methods available (and it works):
//initialize method params
Class[] paramInitialize = new Class[2];
paramInitialize[0] = Activity.class;
paramInitialize[1] = String.class;
try {
Logger.i(DEBUG_TAG, "try statement");
Class <?> tremorVideoCls = Class.forName("com.tremorvideo.sdk.android.videoad.TremorVideo");
Class <?> tremorSettingsCls = Class.forName("com.tremorvideo.sdk.android.videoad.Settings");
Object object = tremorVideoCls.newInstance();
String apkName = this.getPackageManager().getApplicationInfo(this.getPackageName(), 0).sourceDir;
loader = new dalvik.system.PathClassLoader(apkName, ClassLoader.getSystemClassLoader());
tremorVideoCls = loader.loadClass("com.tremorvideo.sdk.android.videoad.TremorVideo");
Method m[] = tremorVideoCls.getMethods();
Method declaredM[] = tremorVideoCls.getDeclaredMethods();
for (int i = 0; i < m.length; i++) {
Logger.i(DEBUG_TAG, "getMethods is " + m[i].toString());
}
Field []field = tremorSettingsCls.getFields();
for (int i = 0; i < field.length; i++) {
Logger.i(DEBUG_TAG, "getFields is " + field[i].toString());
}
Method tremorInitialize = tremorVideoCls.getMethod("initialize", paramInitialize);
Logger.i(DEBUG_TAG, "tremorInitialize is " + tremorInitialize.toGenericString());
// tremorInitialize.invoke(object, this, ACCUWX.TREMOR_ADSPACE_TEST);
} catch (ClassNotFoundException e) {
} catch (NoSuchMethodException x) {
...
BUT, I get a logcat error throwing an exception:
08-06 11:48:51.134: W/System.err(11688): java.lang.NoSuchMethodException: initialize [class android.app.Activity, class java.lang.String]
Here is the logcat printing out that method:
08-06 11:48:51.118: I/Flagship/MainActivity(11688): getMethods is public static void com.tremorvideo.sdk.android.videoad.TremorVideo.initialize(android.content.Context,java.lang.String)
I’m not sure what do try next…any help greatly appreciated.
If the method which you are trying to invoke is
and the error is
It clearly indicates that the method expects
android.content.Contextbut what you are passingandroid.app.ActivityIn the call
instead of passing
thisyou should pass an instance ofandroid.content.Context.Hope this helps!!!