TimCO TimCO - 3 years ago 107
Android Question

avoid login when redeploying on Android Emulator

Trying to speed up my manual testing on Android Emulator by not having to login and navigate to a specific page every time I deploy a new apk to the emulator. Need to make sure that a careless developer on the team cannot accidentally deploy a release to play store that allows skipping login (e.g., don't just uncomment code to skip login).

Current approach:

  1. Create product flavors "normal" and "skiplogin" (no attributes).

  2. Where code grabs REST API credentials from SharedPreferences add this:

    if (BuildConfig.DEBUG && BuildConfig.FLAVOR.equals("skiplogin")){
    // use hard-coded username/token

pros - it works. greatly reduces time spent click testing

cons - "gradle clean build" takes twice as long when you have two flavors. Does not work on pages that depend on state that is fetched on previous activities/fragments, such as data fetched only at login.

Gotta be a simpler way, who knows it?

Answer Source

If what you want is that the code don't run on release why don't you instead use BuildConfig.DEBUG?

if (BuildConfig.DEBUG && BuildConfig.mockLogin) {
  // use hard-coded username/token

and in your gradle define buildConfigField('boolean' , 'mockLogin', 'true') in the case that the code makes it to production it won't run cause BuildConfig.DEBUG it's false when you do a release build, and you can't upload a debug build to play store cause it won't allow it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download