简体   繁体   中英

Detect if the App android is in Debug or Release

I have a few questions about debugging.

I would like to create different preferences to use when the application runs on the phone (release) and when it is being tested. For example the URLs for the API or the passwords or the UserId which can be different during the test phase.

To do this I'm thinking of setting everything inside sharedPreferences and then if the application is debugging or not, take the right variables.

I also thought of saving everything up in config files , but for now I have not understood if it is possible to do it or not (I am studying) and if it is convenient

The sharedPreferences I understood how it works.

1) I would like to know if there is a method that returns a value if the application is debugged or not.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    if (BuildConfig.DEBUG) {
        toast("debug")
    }

}

but even when I install the application on the phone it tells me that it is always debugged

In the future I would also like to look for automatic testing systems (which I don't know). I read that there is Appium (but certainly others too) that allows you to test the application but for now I have not gone into this yet.

2) Eventually then there is a method to understand if you are in the test phase, I don't know if these utilities run the app in test or real mode.

3) Do you know of other test systems?

Thanks

The BuildConfig.DEBUG refers to the current variant and not if the app is deployed on a device or not. Here is a method, in Kotlin, to check if you app is deployed on the emulator

fun isProbablyAnEmulator() = Build.FINGERPRINT.startsWith("generic")
            || Build.FINGERPRINT.startsWith("unknown")
            || Build.MODEL.contains("google_sdk")
            || Build.MODEL.contains("Emulator")
            || Build.MODEL.contains("Android SDK built for x86")
            || Build.BOARD == "QC_Reference_Phone" //bluestacks
            || Build.MANUFACTURER.contains("Genymotion")
            || Build.HOST.startsWith("Build") //MSI App Player
            || (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
            || "google_sdk" == Build.PRODUCT

The best way, I think, to use a different configuration between Debug and Release is to use variables in your gradle file. For example, you can prefill a form like this

build.gradle

buildTypes {
    debug {
        resValue("string", "username", "JohnDoe")
        resValue("string", "password", "johnny")
    }

    release {
        resValue("string", "username", "")
        resValue("string", "password", "")
    }
}

How to use it

R.string.username

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM