1

Am trying to increase security to my request payload, one of the measures is encrypting or blurring the member key of my json this way it's more ambiguous...

Here is what am trying to do,

public class LoginRequest {

    private static final String SERIALIZED_NO = BuildConfig.DEBUG ? "no": "xyz";

    private LoginRequest() {
        // This class is not publicly instantiable
    }

    public static class ServerLoginRequest extends ParentRequest {

        @Expose
        @SerializedName(SERIALIZED_NO)
        private String no;

Here is an image of my code...

code preview

Now the error is that this, @SerializedName(SERIALIZED_NO) must be a constant i read some articles but no luck so far i think there is no way of conditional annotation like in C#, btw, of course, i can handle this with my back end since its not java, in some smart way.

shareef
  • 9,255
  • 13
  • 58
  • 89

2 Answers2

0

This looks like a duplicate of How to supply value to an annotation from a Constant java to me however there is solution but I don't think you'l like it :)

Here is what you can do:

  1. Create two new source code hierarchies, one starting with debug and the other starting with release - at the same level as your main hierarchy.
  2. Place one copy of your model under debug/java/...your package structure to model..., with @SerializedName("no")
  3. Place another copy of your model under release/java/...your package structure to model..., with @SerializedName("xyz")
  4. Remove your data model from main/...your package structure to model... hierarchy

You will now be able to use your model under main but it will always be one or another, depending on build type you are using.

ror
  • 3,295
  • 1
  • 19
  • 27
-1

SERIALIZED_NO is obviously not constant. Its value is dependent to BuildConfig.DEBUG. It sounds however a bit unnecessary to use a constant as condition to make another constant which won't be a constant anymore. Try to move definition of SERIALIZED_NO to interface and see what happens otherwise you can use Dependency Injection that is equivalent to compiler directives in C++.

Dan
  • 126
  • 1
  • 14