Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-Constant Fields in Case Labels #683

Closed
chirag-jn opened this issue Nov 3, 2020 · 8 comments · Fixed by #689
Closed

Non-Constant Fields in Case Labels #683

chirag-jn opened this issue Nov 3, 2020 · 8 comments · Fixed by #689

Comments

@chirag-jn
Copy link
Member

Describe the issue
With the introduction of Gradle 5.0, resource IDs won't be final anymore. Hence, a switch-case statement won't work in onClick function for view elements. We have to switch to if-else conditions of a view binding strategy for mapping resource IDs to their variables.

More information: http://tools.android.com/tips/non-constant-fields

For the first case, we have to switch from

@Override
public void onClick(View view) {

    switch (view.getId()) {
        case R.id.next_btn:
            // Do something
            break;
    }
}

to something like this:

@Override
public void onClick(View view) {
    
    int id = view.getId();
    if (id == R.id.next_btn) {
        nextBtnPressed();
    }
}

Old view binding libraries like JakeWharton/butterknife won't work anymore either.

@Prakhar-Agarwal-byte
Copy link
Contributor

@chirag-jn @cpg Hey can I work on this issue instead of Localisation Support? (#548 ).Please guide me where to start first as I new to code base of Amahi android.

@chirag-jn
Copy link
Member Author

@chirag-jn @cpg Hey can I work on this issue instead of Localisation Support? (#548 ).Please guide me where to start first as I new to code base of Amahi android.

Sure you can work on this.

@chirag-jn
Copy link
Member Author

The Android documentation link would be a good place to let you know about the issue.

@Prakhar-Agarwal-byte
Copy link
Contributor

@chirag-jn @cpg I have found 2 solutions for this issue. Please tell which is better so I can start implementing it.

  1. A quick fix for this issue that we can disable NonConstantResourceId in module level build.gradle file. Code is given below -

android { ... lintOptions { disable 'NonConstantResourceId' } }

  1. Other fix is to replace all switch statements that use Non-Constant Fields in Case Labels with if else statements without effecting efficiency of code.

@chirag-jn
Copy link
Member Author

In the first case, you are only disabling linting for NonConstantResourceId. That still leaves it as a pain point in future versions of Gradle. However, the second option seems great!

@Prakhar-Agarwal-byte
Copy link
Contributor

@chirag-jn Okay I am implementing second option. Thanks for your suggestion!

@Prakhar-Agarwal-byte
Copy link
Contributor

Prakhar-Agarwal-byte commented Nov 4, 2020

@chirag-jn @cpg I have sent a PR ( #685 ) for this issue please review it. I closed this PR due to some error.

@Prakhar-Agarwal-byte
Copy link
Contributor

@chirag-jn @cpg This is the new PR( #689 ) for this issue please review it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment