BootstrapApplication无法强制转换为ApplicationClass



我正在运行Android Studio 2.0版本的新稳定版本。当我禁用即时运行时,我的应用程序运行良好,但当我打开它时,会出现以下错误:

Caused by: java.lang.ClassCastException: com.android.tools.fd.runtime.BootstrapApplication cannot be cast to com.my.app.CustomApplication

CustomApplication是我通过上下文获得的一个应用程序类。但我似乎不明白。当即时运行打开时,我的类被转换为BootstrapApplication,然后失败。

我的应用程序是一个类似FB聊天头的浮动服务。

我有最新的渐变版本:

classpath 'com.android.tools.build:gradle:2.0.0'

这里的其他答案是,InstantRun试图进行代码的热交换;这将导致应用程序类被移动。

那么我该如何绕过这一点呢?

解决方案#1-禁用即时运行设置

解决方案#2-使用反射从BootstrapApplication获取实际应用程序

public static CustomApplication getRealApplication (Context applicationContext)
{
    CustomApplication application = null;
    if (applicationContext instanceof CustomApplication)
    {
        application = (CustomApplication) applicationContext;
    }
    else
    {
        Application realApplication = null;
        Field magicField = null;
        try
        {
            magicField = applicationContext.getClass().getDeclaredField("realApplication");
            magicField.setAccessible(true);
            realApplication = (Application) magicField.get(applicationContext);
        }
        catch (NoSuchFieldException e)
        {
            Log.e(TAG, e.getMessage());
        }
        catch (IllegalAccessException e)
        {
            Log.e(TAG, e.getMessage());
        }
        application = (CustomApplication) realApplication;
    }
    return application;
}

使用地点:

    Context applicationContext = getContext().getApplicationContext();
    CustomApplication application = getRealApplication(applicationContext);

使用示例:

public class MyProvider extends OrmLiteProvider<OrmLiteSqliteOpenHelper, OrmLiteUriMatcher<OrmLiteMatcherEntry>>
{
    @Override
    protected OrmLiteSqliteOpenHelper createHelper ()
    {
        Context applicationContext = getContext().getApplicationContext();
        CustomApplication application = CustomApplication.getRealApplication(applicationContext);
        return application.getComponent().databaseHelper();
    }
   ...
}

public static CustomApplication getRealApplication (Context applicationContext)
{
    CustomApplication application = null;
    if (applicationContext instanceof CustomApplication)
    {
        application = (CustomApplication) applicationContext;
    } else if (applicationContext.getApplicationContext() instanceof CustomApplication) {
        application = (CustomApplication) applicationContext.getApplicationContext() ;
    }
    else
    {
        Application realApplication = null;
        Field magicField = null;
        try
        {
            magicField = applicationContext.getClass().getDeclaredField("realApplication");
            magicField.setAccessible(true);
            realApplication = (Application) magicField.get(applicationContext);
        }
        catch (NoSuchFieldException e)
        {
            Log.e(TAG, e.getMessage());
        }
        catch (IllegalAccessException e)
        {
            Log.e(TAG, e.getMessage());
        }
        application = (CustomApplication) realApplication;
    }
    return application;
}

在解决方案2中我添加了一个案例

相关内容

  • 没有找到相关文章