Android Library Projects could not be used with ACRA due to their resources identifiers not being final fields anymore

旧时模样 提交于 2019-12-11 22:39:44

问题


From the ADT Site:

The constants are not final in a library project. The reason for this is simple: When multiple library projects are combined, the actual values of the fields (which must be unique) could collide. Before ADT 14, all fields were final, so as a result, all libraries had to have all their resources and associated Java code recompiled along with the main project whenever they were used. This was bad for performance, since it made builds very slow. It also prevented distributing library projects that didn't include the source code, limiting the usage scope of library projects.

This is explained here.

So, in order to fix this, I used -

int id = view.getId();
if (id == R.id.button1) {
    action1();
} else if (id == R.id.button2) {
    action2();
} else if (id == R.id.button3) {
    action3();
}

Instead of -

    int id = view.getId();

    switch (id) {
    case R.id.button1:
        action1();
        break;
    case R.id.button2:
        action2();
        break;
    case R.id.button3:
        action3();
        break;
}

But I have a class GlobalData and I am not able to fix these errors for the same.

Code-

The error says--

The value for annotation attribute ReportsCrashes.resDialogText must be a constant expression when I point the cursor to error at R.string.crash_dialog_text.


回答1:


You have to add those parameters dynamically onCreate() in the application class as follows-

public class GlobalData extends Application {
@Override
        public void onCreate() {

            ACRAConfiguration config = ACRA.getConfig();
            config.setMailTo("abc@test.com");
            config.setResDialogIcon(android.R.drawable.ic_dialog_info);
            config.setResDialogText(R.string.crash_dialog_text);
            config.setResDialogTitle(R.string.crash_dialog_title);
            config.setResDialogCommentPrompt(R.string.crash_dialog_comment_prompt);
            config.setResDialogOkToast(R.string.crash_dialog_ok_toast);

            try
            {
                config.setMode(ReportingInteractionMode.DIALOG);
            }
            catch (ACRAConfigurationException e)
            {
                e.printStackTrace();
                return;
            }

            ACRA.setConfig(config);

            ACRA.init(this);
            super.onCreate();
    }
}



回答2:


This is only a problem if you define a concrete Application class in your Android library that you have annotated for ACRA.

The question is why you would have a concrete Application class in an Android library as it belongs in your Application project where you will have final resource attributes.




回答3:


You have to add those parameters dynamically in Application.onCreate(). You can't use ACRA.getConfig() before ACRA.init() (it returns null if called before ACRA.init()). Use ACRA.getNewDefaultConfig() instead. You can initialise some parameters with attributes and other programmatically.

Example:

@ReportsCrashes(
        mailTo = "abc@test.com",
        mode = ReportingInteractionMode.DIALOG,
        resDialogIcon = android.R.drawable.ic_dialog_info
)
public class MyApplication extends Application {

    @Override
    public void onCreate() {

        ACRAConfiguration config = ACRA.getNewDefaultConfig(this);
        config.setResDialogText(R.string.crash_dialog_text);
        config.setResDialogTitle(R.string.crash_dialog_title);
        config.setResDialogCommentPrompt(R.string.crash_dialog_comment_prompt);
        config.setResDialogOkToast(R.string.crash_dialog_ok_toast);

        ACRA.setConfig(config);
        ACRA.init(this);

        super.onCreate();
    }
}


来源:https://stackoverflow.com/questions/23078016/android-library-projects-could-not-be-used-with-acra-due-to-their-resources-iden

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!