[积累]使用proguard混淆代码后导致构造函数not fount 问题 Proguard and reflection in Android

在proguard的时候, 我们的类也会被混淆掉,导致出现
java.lang.NoSuchMethodException: [class android.content.Context这个错误

解决, 在proguard声明不要混淆需要实例化类的构造函数

-keepclassmembers class * extends com.hzy.tvmao.ir.engine.BaseIR{
 public <init>(android.content.Context);
}

这里写图片描述
我这里使用了一个工厂类来创建extends BaseIR的实例


/**
 * IREngine的工厂, 使用方式是<br>
 * sBaseIREngine = IREngineFactory.getBaseIR(SamsungIR.class,
 * TmApp.getContext());
 * 
 * @author gaoshuai
 * 
 */
public class IREngineFactory
{
    public static BaseIR getIR(Class<? extends BaseIR> clazz, Context context, int test) {
        try {
            Constructor<? extends BaseIR> constructor = clazz.getDeclaredConstructor(Context.class, int.class);
            constructor.setAccessible(true);
            BaseIR a1 = (BaseIR) constructor.newInstance(context, test);
            LogUtil.i(a1.getName() + " ir service found");
            return a1;
        }
        catch (Throwable e) {
            e.printStackTrace();
            LogUtil.i("IR Throwable t.toString=" + e.toString());
        }
        return null;
    }

    public static BaseIR getIR(Class<? extends BaseIR> clazz, Context context) {
        // TODO Auto-generated method stub
        return null;
    }
}

http://stackoverflow.com/questions/4447145/proguard-and-reflection-in-android

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值