Oliver Metz
Oliver Metz

Reputation: 3748

Preventing preview composables getting into release

What is the recommended safety measure to prevent @Preview composables from reaching release? This question answered by using proguard but what if:

  1. I'm not using proguard
  2. I (accidentally) call the preview composable from the real code (proguard would not strip it due to the call reference)

Can / should I move the preview into /debug sources? Are there other safety mechanisms?

Upvotes: 2

Views: 1922

Answers (2)

Juice
Juice

Reputation: 36

You can mark Preview functions with annotation @RestrictTo(RestrictTo.Scope.TESTS) so no one could use your function even within the actual code. The same you could do with the Preview-data variables to prevent access in production code.

Upvotes: 2

Ashok
Ashok

Reputation: 2706

One of the thing you can do is make your preview composable as a private function.

So, now you will have two functions, one is the actual composable, which you can use in your codebase. And, one is the preview composable, which will be private.

By keeping this preview as private, no one can be able to access your Preview composable, this way you can prevent the misuse.

Also, since there is no one referring to this private function, proguard can mark this function as unused and remove it.

Upvotes: 2

Related Questions