0

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.

sjain
  • 23,126
  • 28
  • 107
  • 185

3 Answers3

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();
    }
}
sjain
  • 23,126
  • 28
  • 107
  • 185
0

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.

William
  • 20,150
  • 8
  • 49
  • 91
  • Doesn't have to be concrete, if you create an abstract extension to Application in your library that handles ACRA, this occurs. Regardless the above answer works and the ACRA source indicates there will be a new init Method in a future release to avoid this issue altogether. https://github.com/ACRA/acra/blob/master/src/main/java/org/acra/ACRA.java#L135 – JCricket Aug 27 '14 at 14:34
  • The question is why would you want an abstract Application object in your library containing ACRA config when you will have to have a concrete Application object at some point which could just as easily have the ACRA config, especially since at least some of the config is app specific. And as one of the ACRA committers: the method you are referring to was released in version 4.3.0 (about 18 months ago). – William Aug 30 '14 at 03:23
  • I use a custom ACRA sender, which is in a separate library (along with common code for all my apps) which handles the ACRA details. However, my apps employ the same generic values for the toasts and prompts, so I don't redefine anything in the concrete app. As a committer, can you advise if that's not a recommended practice and why? For the #init, my mistake. I read the dates wrong. However, maybe you can help...when I link the ARCA 4.5.0 jar, why don't I have that ACRA#init(app,config) method available? That's one reason I came to the mistaken conclusion for a future release. – JCricket Aug 30 '14 at 05:23
  • But surely your concrete app has a key or url specific to it. Unless you are lumping the crashes for all your apps into the one bucket, which would be a really poor way of managing them IMHO. Mea culpa re #init(App, confi), that method was only added Feb 2014, that's why it's not in 4.5.0 – William Aug 30 '14 at 05:57
  • The error data sent already has package name and version code, and together I use those pieces of data to form a key for separating errors from each other. This way the app has little to nothing to do except send the error to the submission URL, which is common for all my apps since I manage the custom backend as well. So really they go into separate buckets without the need for a pre-defined unique key or app-specific url. Even with a key, redefining that single key per app is better DRY practice than keeping the custom sender coded in every app. Changes made to the library are much easier. – JCricket Aug 30 '14 at 11:41
0

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();
    }
}
Community
  • 1
  • 1
babay
  • 4,689
  • 1
  • 26
  • 40