androidandroid-build-type

Prefill EditText according to buildtype


Is it possible to set text in EditText only for a certain buildtype? I want the EditText in the app I'm developing to be prefilled when running the debug buildtype. The only way I see this right now is by checking programmatically if the current the current buildtype is "debug" and call setText().

I was hoping to be able to do this in a cleaner way. Perhaps something like the tools namespace in XML layouts. Any suggestions?


Solution

  • Eventually I went with my own way of keeping it clean. I've had a look at Aspect Oriented Programming and made an Aspect with AspectJ.

    @Aspect
    class PrefillAspect {
    
        @After("execution(* com.example.aspect.LoginActivity.onCreate(*))")
        fun prefillLoginForm(joinPoint: JoinPoint) {
            try {
                val activity = joinPoint.target as LoginActivity
                activity.findViewById<EditText>(R.id.editEmail).setText("test@example.com")
                activity.findViewById<EditText>(R.id.editPassword).setText("MySecretPassword")
            } catch (e: Throwable) {
                Log.e("PrefillAspect", "prefillLoginForm: failed")
            }
        }
    
    }
    

    I've added this aspect to my src/debug/java folder so this aspect is only applied when a debug build is run. There's no code whatsoever in my main source, so this will never be shipped and the code base remains clean.

    I've written an article about this here: https://medium.com/@dumazy/prefill-forms-on-android-with-aspectj-97fe9b3b48ab