androidamazon-web-serviceskotlinaws-sdkaws-mobilehub

Error setting up AWS credentials in Kotlin


I am currently creating an android version of an IOS app that is built. I need to be able to access AWS services and I am having issues with my credentials.

In swift the way to set up credentials is to import the 'awsconfiguration.json' file, then write the following code:

let credentialsProvider = AWSStaticCredentialsProvider(accessKey:"MY_ACCESS_KEY", secretKey:"MY_KEY")
       let configuration = AWSServiceConfiguration(
           region: AWSRegionType.USWest2,
           credentialsProvider: credentialsProvider)
       AWSServiceManager.default().defaultServiceConfiguration = configuration

I tried things from the following the documentation on aws and I cannot figure out what I am missing. I got the json file and pasted it in the /res/raw folder and am trying to call the following code from my MainActivity.

https://docs.aws.amazon.com/aws-mobile/latest/developerguide/how-to-android-sdk-setup.html

https://discuss.kotlinlang.org/t/kotlin-aws-credentials/8399

https://docs.aws.amazon.com/aws-mobile/latest/developerguide/getting-started.html

I get no errors in the IDE (Android Studio). But when I launch the app, it crashes immediately and I get the following error from logcat:

2018-07-25 14:20:10.476 22581-22581/? D/AWSMobileClient: Fetching the Cognito Identity. 2018-07-25 14:20:10.481 22581-22581/? D/AndroidRuntime: Shutting down VM 2018-07-25 14:20:10.484 22581-22581/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.mammothvr.checkarafragments, PID: 22581 java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/logging/LogFactory; at com.amazonaws.util.VersionInfoUtils.(VersionInfoUtils.java:41) at com.amazonaws.util.VersionInfoUtils.getUserAgent(VersionInfoUtils.java:77) at com.amazonaws.ClientConfiguration.(ClientConfiguration.java:43) at com.amazonaws.mobile.auth.core.IdentityManager.(IdentityManager.java:212) at com.amazonaws.mobile.client.AWSMobileClient.fetchCognitoIdentity(AWSMobileClient.java:280) at com.amazonaws.mobile.client.AWSMobileClient.initializeWithBuilder(AWSMobileClient.java:186) at com.amazonaws.mobile.client.AWSMobileClient.access$100(AWSMobileClient.java:74) at com.amazonaws.mobile.client.AWSMobileClient$InitializeBuilder.execute(AWSMobileClient.java:446) at com.mammothvr.checkarafragments.MainActivity.onCreate(MainActivity.kt:60) at android.app.Activity.performCreate(Activity.java:7136) at android.app.Activity.performCreate(Activity.java:7127) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 2018-07-25 14:20:10.485 22581-22581/? E/AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.commons.logging.LogFactory" on path: DexPathList[[zip file "/data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/base.apk", zip file "/data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_dependencies_apk.apk", zip file "/data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_resources_apk.apk", zip file "/data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_0_apk.apk", zip file "/data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_1_apk.apk", zip file "/data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_2_apk.apk", zip file "/data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_3_apk.apk", zip file "/data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_4_apk.apk", zip file "/data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_5_apk.apk", zip file "/data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_6_apk.apk", zip file "/data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_7_apk.apk", zip file "/data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_8_apk.apk", zip file "/data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/lib/arm64, /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/base.apk!/lib/arm64-v8a, /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_dependencies_apk.apk!/lib/arm64-v8a, /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_resources_apk.apk!/lib/arm64-v8a, /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_0_apk.apk!/lib/arm64-v8a, /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_1_apk.apk!/lib/arm64-v8a, /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_2_apk.apk!/lib/arm64-v8a, /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_3_apk.apk!/lib/arm64-v8a, /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_4_apk.apk!/lib/arm64-v8a, /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_5_apk.apk!/lib/arm64-v8a, /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_6_apk.apk!/lib/arm64-v8a, /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_7_apk.apk!/lib/arm64-v8a, /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_8_apk.apk!/lib/arm64-v8a, /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_9_apk.apk!/lib/arm64-v8a, /system/lib64]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134) at java.lang.ClassLoader.loadClass(ClassLoader.java:379) at java.lang.ClassLoader.loadClass(ClassLoader.java:312) ... 24 more Suppressed: java.io.IOException: No original dex files found for dex location /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_resources_apk.apk at dalvik.system.DexFile.openDexFileNative(Native Method) at dalvik.system.DexFile.openDexFile(DexFile.java:354) at dalvik.system.DexFile.(DexFile.java:101) at dalvik.system.DexFile.(DexFile.java:75) at dalvik.system.DexPathList.loadDexFile(DexPathList.java:394) at dalvik.system.DexPathList.makeDexElements(DexPathList.java:354) at dalvik.system.DexPathList.(DexPathList.java:164) at dalvik.system.BaseDexClassLoader.(BaseDexClassLoader.java:74) at dalvik.system.BaseDexClassLoader.(BaseDexClassLoader.java:65) at dalvik.system.PathClassLoader.(PathClassLoader.java:64) 2018-07-25 14:20:10.485 22581-22581/? E/AndroidRuntime: at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:73) at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:88) at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:74) at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:40) at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:727) at android.app.LoadedApk.getClassLoader(LoadedApk.java:810) at android.app.LoadedApk.getResources(LoadedApk.java:1032) at android.app.ContextImpl.createAppContext(ContextImpl.java:2345) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5749) at android.app.ActivityThread.access$1100(ActivityThread.java:199) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) ... 6 more

Here is my MainActivity Kotlin code:

package com.mammothvr.checkarafragments

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.amazonaws.auth.AWSCredentials
import com.amazonaws.auth.AWSCredentialsProvider
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.mobile.auth.core.IdentityHandler
import com.amazonaws.mobile.auth.core.IdentityManager
import kotlinx.android.synthetic.main.activity_main.*
import com.amazonaws.mobile.client.AWSMobileClient;
import com.amazonaws.mobile.client.AWSMobileClient.InitializeBuilder
import com.amazonaws.mobile.config.AWSConfiguration
import androidx.core.app.CoreComponentFactory


class MainActivity : AppCompatActivity() {

    companion object {

        //AWS singleton link to mobile client
        private val TAG: String = this::class.java.simpleName
        // Used to load the 'native-lib' library on application startup.
        init {
            System.loadLibrary("native-lib")
        }
    }

    private var credentialsProvider: AWSCredentialsProvider? = null
    private var awsConfiguration: AWSConfiguration? = null


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

        AWSMobileClient.getInstance().initialize(this) {

           // val credentials: AWSCredentials = BasicAWSCredentials("MY_ACCESS_KEY", "MY_KEY")

            credentialsProvider = AWSMobileClient.getInstance().credentialsProvider
            awsConfiguration = AWSMobileClient.getInstance().configuration

            IdentityManager.getDefaultIdentityManager().getUserID(object : IdentityHandler {
                override fun handleError(exception: Exception?) {
                    Log.e(TAG, "Retrieving identity: ${exception?.message}")
                }

                override fun onIdentityId(identityId: String?) {
                    Log.d(TAG, "Identity = $identityId")
                    val cachedIdentityId = IdentityManager.getDefaultIdentityManager().cachedUserID
                    // Do something with the identity here
                }
            })
        }.execute()

        hideSystemUI()

        val fragment = LoginFragment.newInstance()
        replaceFragment(fragment)

    }

    private fun replaceFragment(fragment: Fragment){
        val fragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).replace(R.id.fragmentContainer, fragment).commit()
    }



    private fun hideSystemUI() {

        val decorView = window.decorView
        decorView?.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
                // Set the content to appear under the system bars so that the
                // content doesn't resize when the system bars hide and show.
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                // Hide the nav bar and status bar
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }

    external fun stringFromJNI(): String
}

Here are my grade dependencies:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    implementation 'pub.devrel:easypermissions:1.1.3'
    implementation 'com.android.support:design:27.1.1'

    implementation ('com.amazonaws:aws-android-sdk-s3:2.6.+') { transitive = true }
    implementation ('com.amazonaws:aws-android-sdk-rekognition:2.6.+') { transitive = true }
    implementation ('com.amazonaws:aws-android-sdk-cognitoauth:2.6.+@aar') { transitive = true }
    implementation ('com.amazonaws:aws-android-sdk-cognitoidentityprovider:2.6.+') { transitive = true }
    implementation ('com.amazonaws:aws-android-sdk-mobile-client:2.6.+@aar') { transitive = true }
    implementation ('com.amazonaws:aws-android-sdk-cognitoauth:2.6.+@aar') { transitive = true }


    // Cognito UserPools for SignIn
    implementation 'com.android.support:support-v4:26.+'
    implementation ('com.amazonaws:aws-android-sdk-auth-userpools:2.6.+@aar') { transitive = true }

    // Sign in UI Library
    implementation 'com.android.support:appcompat-v7:26.+'
    implementation ('com.amazonaws:aws-android-sdk-auth-ui:2.6.+@aar') { transitive = true }

}

This is my first Android project coming from a primarily c#/swift background. Any help is greatly appreciated.


Solution

  • It can't find class org.apache.commons.logging.LogFactory. You need to add commons-logging to dependencies.