我正在尝试使用反射而不是包含 jar 文件。当我不使用 jar 代码时,我想这样做以减少我的 apk 大小......但如果我包含代码,我必须包含 jar。话虽如此,我正在努力掌握反思,但做得不好。
我有这段代码可以查看可用的方法(并且有效):
//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) {
...
但是,我得到一个引发异常的 logcat 错误:
08-06 11:48:51.134: W/System.err(11688): java.lang.NoSuchMethodException: initialize [class android.app.Activity, class java.lang.String]
这是打印出该方法的 logcat:
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)
我不确定接下来会尝试什么......非常感谢任何帮助。