3

Suppose I developed an android library XYZ which has methods
animateWithTransition() which has code related with Transition api(i.e. minsdk=21)
animateSimply() which has simple animation.

When Client uses XYZ library, he should be able to see animateWithTransition() as a suggestion(ctrl+space) if his minsdk < 21. and should be able to see only animateSimply() : |

How to go about this?

AskQ
  • 4,215
  • 7
  • 34
  • 61
  • 1
    Why would you want to do that? As a user of libraries, I don't want one function to call if I'm API< 21 and another for API >21. I want one call, and you should figure out which version to do. – Gabe Sechan Jun 16 '17 at 06:10
  • I'd like to achieve that for avoiding my clients time in writing boilerplate code to figure out versions.. There is annotation @hide wh\ich completely hides method https://stackoverflow.com/questions/17035271/what-does-hide-mean-in-the-android-source-code – AskQ Jun 16 '17 at 06:12
  • You can use the RequiresApi annotation. See [link](https://developer.android.com/reference/android/support/annotation/RequiresApi.html) – Kunal Chawla Jun 16 '17 at 06:14
  • @AskQ If you want clients to avoid writing boilerplate, then do what I suggested- 1 method, which does a version lookup and chooses which branch of code to use. Then there's no boilerplate, and no confusion about which function to use. – Gabe Sechan Jun 16 '17 at 06:17
  • @GabeSechan, Kunal Thanks its simple solution. But may be visibility logic is possible with java reflection or some custom annotation processor. I'm looking for that. – AskQ Jun 16 '17 at 06:30

1 Answers1

2

You should try structuring your code in the following manner -

    public void performAnimation() {
    if(Build.VERSION.SDK_INT < 21 )
         { 
      // write code for animateSimply function here 
           }
    else
         { 
      // write code for animateWithTransition function here 
           }
        }

That ways, you'll have a single function (which means less code, clean code), and easier testing. Plus your client has to call only 1 function, which makes it easier for him/her to use your library.

Kunal Chawla
  • 1,236
  • 2
  • 11
  • 24