sjain
sjain

Reputation: 23354

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

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-

GlobalData file with errors when project is added as a library project

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.

Upvotes: 0

Views: 369

Answers (3)

babay
babay

Reputation: 4844

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 = "[email protected]",
        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();
    }
}

Upvotes: 0

William
William

Reputation: 20196

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.

Upvotes: 0

sjain
sjain

Reputation: 23354

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("[email protected]");
            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();
    }
}

Upvotes: 1

Related Questions