我有一个类WifiScanning:
public class WifiScanning extends AbstractSetting {
/**
*
*/
private static final long serialVersionUID = 226897434530036069L;
public WifiScanning(Object valueToApply) {
super(valueToApply, WifiScanning.class);
}
/**
* For persistence only
*/
public WifiScanning() {
super(null, WifiScanning.class);
}
可以看到,它有2个构造函数。一个用于我的简单持久层,并且是一个空构造函数,因此newInstance()可以工作,另一个接受单个参数,这是我的应用程序定义的标准参数。其他代码则假定必须有一个带单个参数的构造函数,否则会抛出异常。
/**
*
* @param setting
* @param ctx
* @return
* @throws SettingException
*/
private synchronized static AbstractSetting getOriginalSetting(AbstractSetting setting,
Context ctx) throws SettingException {
Class<? extends AbstractSetting> clazz = setting.getClass();
try {
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
for (Constructor<?> c : constructors) {
if(c.getParameterTypes().length == 1) {
Object original = setting.getCurrentSettingValue(ctx);
LOG.debug("Caching original value '"+original+"' for "+clazz.getSimpleName());
return (AbstractSetting) c.newInstance(original);
}
}
/*
* ###################### DEBUG BLOCK ######################
*
* This has been put here to work out why we are getting to this point in the code when
* using WifiScanning.java
*/
LOG.error("There are "+constructors.length+" constructors for "+clazz.getName()+" which we got from "+setting);
for (Constructor<?> c : constructors) {
if(c.getParameterTypes().length == 1) {
LOG.debug("Found the consructor! How the hell can that be?");
}
else {
LOG.error("Unusable constructor: "+c.toGenericString());
LOG.error("From: "+c.getDeclaringClass());
LOG.error("Modifiers:");
LOG.error("private="+Modifier.isPrivate(c.getModifiers()));
LOG.error("protected="+Modifier.isProtected(c.getModifiers()));
LOG.error("public="+Modifier.isPublic(c.getModifiers()));
LOG.error("static="+Modifier.isStatic(c.getModifiers()));
Type[] genericParameterTypes = c.getGenericParameterTypes();
LOG.error("Constructor has "+genericParameterTypes.length+" generic parameter types");
for (Type type : genericParameterTypes) {
LOG.error("Generic parameter type: "+type.getClass().getName());
}
Class<?>[] parameterTypes = c.getParameterTypes();
LOG.error("Constructor has "+parameterTypes.length+" parameters");
for (Class<?> arg1 : parameterTypes) {
LOG.error("Constructor arg: "+arg1.getName());
}
}
}
/*
* ###################### END DEBUG BLOCK ######################
*/
throw new SettingException(clazz+" does not have a constructor with a single argument");
如果您考虑上面的代码,就会发现添加了DEBUG BLOCK以尝试理解这里发生的事情。如果您暂时忽略这一点,那么您所拥有的是一段代码,它从代码中获取构造函数数组并遍历它们,寻找具有单个参数的构造函数。如果循环退出而没有找到,则抛出异常。
添加了异常块后,日志显示:
E/reference: 10/7 22:28:59.917 E .b[126]:有1个构造函数我们从。com。domloge.comwifi扫描"测试"[set:true|current:false|priority:1][132]:不可用的构造函数:publiccom.domloge. preference .setting. wiiscanning () V/preference: PurgingE/Proference: 10/7 22:28:59.918 E .b[133]: From: class . log文件com.domloge. ference.setting. wifi扫描E/ference: 10/722:28:59.919 E .b[134]:修饰语:E/优选:10/7 22:28:59.919E .b[135]: private=false E/ference: 10/7 22:28:59.920 E .b[136]:E/ference: 10/7 22:28:59.920 E/b[137]: public=trueE/Proference: 10/7 22:28:59.920 E/Proference: [138]: static=false[141]:构造函数有0个泛型参数类型E/references: 10/7 22:28:59.921 E .b[147]:构造函数有0个参数j.b[270]: Could not apply
正如你所看到的,VM正在显示wiiscanning类正在给一个构造函数,而不是2。这怎么可能呢?
当我在我的个人设备和各种模拟器上运行代码时,这不是问题,数组中有2个构造函数。
问题是,当应用程序通过Google Play商店分发时,数组包含单个构造函数。当我的应用程序在Google play商店发布时,我无法调试它,我只能查看日志。
这个相同的principal在应用程序中的其他10多个类中工作得很好…但这只坏了…就好像我漏掉了一个错别字,那将是一个拍额头的d'哦!当有人指出我愚蠢的错误时……
任何想法?
是的,这显然是保护"michief"。
将以下行放入proguard-project.txt
-keepclassmembers class * extends full-package-name.AbstractSetting {
public protected <init>(...);
}