0

I am developing an app using the Android KeyStore API calls to encrypt some string values. I am targeting API level 18 and upwards. I have a an if else block in my code which targets API levels 18 up to 23. My problem is that I either have to use deprecated method calls in the block with (shown below is my if block)

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 &&
            Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {

       KeyPairGenerator kpg = KeyPairGenerator.getInstance(
        KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");

       kpg.initialize(new KeyPairGeneratorSpec.Builder(alias, keyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY)
       .setDigests(KeyProperties.DIGEST_SHA256,KeyProperties.DIGEST_SHA512)
       .build());

       KeyPair kp = kpg.generateKeyPair();
     }

this appears with a line through it in the IDE as the method call is deprecated. But, it does target the api levels this code was built to run on?

If I change over to KeyGenParameterSpec.builder() I get a compile time error because this object can only be used on API level 23 and above. My app targets API level 18 and above?

Is there a way to generate a key pair entry in my KeyStore without using the deprecated method calls or having to move to API level 23 and above?

Thanks in advance!

Ryan
  • 1,863
  • 13
  • 20
filthy_wizard
  • 740
  • 11
  • 25

1 Answers1

0

Just include an else block with the new API level 23 code so you can do it both ways!

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 &&
            Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
    // deprecated code on 23
}else{
    // new code for 23+
}
Ryan
  • 1,863
  • 13
  • 20
  • yeah, still a line through the .build() method call eventhough its targeting the correct api levels. thats a bit of a worry. – filthy_wizard Jan 17 '17 at 11:19
  • 1
    The strike-through is to indicate that this method is deprecated and might be discontinued in future. That has nothing to do with the API levels you are supporting. However if you want to suppress Lint errors, you might like to use the @TargetApi annotation to indicate the minimum SDK till which this API is designed to run on. There is nothing to worry because, Android takes few years to actually chop off a deprecated API. I hope that's a reasonable time for you to migrate to newer API's – Dibzmania Jan 17 '17 at 11:37
  • It's fine as long as it's running in the supported API levels, don't rely on linting for error detection. Use `//noinspection deprecation` to disable the inspection for the below statement. – Ryan Jan 17 '17 at 11:47