androidazure-ad-msalandroidxandroid-instant-run

MSAL for Android: SHA-256 digest error (AuthenticationCallback.class) during Instant Run with androidx


Is there a way to get MSAL working together with Instant Run and AndroidX?

Microsoft Authentication Library 0.2.1 works fine out-of-the-box with Android Studio but gives build errors after migrating to androidx when Instant Run is enabled.

The following error is reported by Java compiler during build:

java.lang.SecurityException: SHA-256 digest error for com/microsoft/identity/client/AuthenticationCallback.class

Repro:

  1. Create a new Android Studio project
  2. Ensure Instant Run is enabled (File > Settings > Build, Execution, Deployment > Instant Run)
  3. Follow instructions here: https://github.com/AzureAD/microsoft-authentication-library-for-android
  4. Debug. Things should work out fine.
  5. Add the following to gradle.properties:
    • android.useAndroidX=true
    • android.enableJetifier=true
  6. Refactor > Migrate to AndroidX (unrelated: if needed fix layouts etc)
  7. Try to start debugging
  8. Now compiler reports the errors mentioned above
  9. Disable Instant Run
  10. Debug
  11. Now things work out fine.

My MainActivity looks as follows:

class MainActivity : AppCompatActivity() {

val CLIENT_ID = "<My Client Id>"
val SCOPES = arrayOf("https://graph.microsoft.com/User.Read")
private lateinit var sampleApp: PublicClientApplication

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

    sampleApp = PublicClientApplication(
        this.applicationContext,
        CLIENT_ID
    )
    sampleApp.acquireToken(this, SCOPES, getAuthInteractiveCallback());
}

private fun getAuthInteractiveCallback(): AuthenticationCallback {
    return object : AuthenticationCallback {
        override fun onSuccess(authenticationResult: AuthenticationResult) {
            val accessToken = authenticationResult.getAccessToken()
        }
        override fun onError(exception: MsalException) {
            if (exception is MsalClientException) {
                /* Exception inside MSAL, more info inside MsalError.java */
            } else if (exception is MsalServiceException) {
                /* Exception when communicating with the STS, likely config issue */
            }
        }
        override fun onCancel() {
            /* User canceled the authentication */
        }
    }
}

/* Handles the redirect from the System Browser */
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    sampleApp.handleInteractiveRequestRedirect(requestCode, resultCode, data)
}
}

Edit: GitHub issue https://github.com/AzureAD/microsoft-authentication-library-for-android/issues/354


Solution

  • Seems to work now (don't know when it was fixed or how but both MSAL 0.2.2 and 0.3.1-alpha seem to work with April 10, 2019 build of Android Studio).