在Kotlin中设置AWS凭证时出错

时间:2018-07-25 20:34:38

标签: android amazon-web-services kotlin aws-sdk aws-mobilehub

我当前正在创建一个内置的IOS应用程序的android版本。我需要能够访问AWS服务,并且我的凭证有问题。

快速设置凭据的方法是导入“ awsconfiguration.json”文件,然后编写以下代码:

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

我尝试了以下有关AWS的文档中的内容,但无法弄清缺少的内容。我得到了json文件并将其粘贴到/ res / raw文件夹中,并试图从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

我在IDE(Android Studio)中没有任何错误。但是,当我启动该应用程序时,它立即崩溃,并且我从logcat中收到以下错误:

  

2018-07-25 14:20:10.476 22581-22581 /? D / AWSMobileClient:获取   认知身份。 2018-07-25 14:20:10.481 22581-22581 /?   D / AndroidRuntime:关闭VM 2018-07-25 14:20:10.484   22581-22581 /? E / AndroidRuntime:致命异常:main       流程:com.mammothvr.checkarafragments,PID:22581       java.lang.NoClassDefFoundError:无法解决以下问题:Lorg / apache / commons / logging / LogFactory;           在com.amazonaws.util.VersionInfoUtils。(VersionInfoUtils.java:41)           在com.amazonaws.util.VersionInfoUtils.getUserAgent(VersionInfoUtils.java:77)           在com.amazonaws.ClientConfiguration。(ClientConfiguration.java:43)           在com.amazonaws.mobile.auth.core.IdentityManager。(IdentityManager.java:212)           在com.amazonaws.mobile.client.AWSMobileClient.fetchCognitoIdentity(AWSMobileClient.java:280)           在com.amazonaws.mobile.client.AWSMobileClient.initializeWithBuilder(AW​​SMobileClient.java:186)           在com.amazonaws.mobile.client.AWSMobileClient.access $ 100(AWSMobileClient.java:74)           在com.amazonaws.mobile.client.AWSMobileClient $ InitializeBuilder.execute(AWSMobileClient.java:446)           在com.mammothvr.checkarafragments.MainActivity.onCreate(MainActivity.kt:60)           在android.app.Activity.performCreate(Activity.java:7136)           在android.app.Activity.performCreate(Activity.java:7127)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6669)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)   2018-07-25 14:20:10.485 22581-22581 /? E / AndroidRuntime:由以下原因引起:   java.lang.ClassNotFoundException:找不到类   路径上的“ org.apache.commons.logging.LogFactory”:DexPathList [[zip文件   “ /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/base.apk”,   压缩文件   “ /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_dependencies_apk.apk”,   压缩文件   “ /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_resources_apk.apk”,   压缩文件   “ /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_0_apk.apk”,   压缩文件   “ /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_1_apk.apk”,   压缩文件   “ /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_2_apk.apk”,   压缩文件   “ /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_3_apk.apk”,   压缩文件   “ /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_4_apk.apk”,   压缩文件   “ /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_5_apk.apk”,   压缩文件   “ /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_6_apk.apk”,   压缩文件   “ /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_7_apk.apk”,   压缩文件   “ /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_8_apk.apk”,   压缩文件   “ /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.mammothvr.checkarafragments-T8bCup9/W6KQQPP   /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]]           在dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)           在java.lang.ClassLoader.loadClass(ClassLoader.java:379)           在java.lang.ClassLoader.loadClass(ClassLoader.java:312)             ...另外24个         禁止:java.io.IOException:找不到用于dex位置的原始dex文件   /data/app/com.mammothvr.checkarafragments-T8bCup9H6KQ50PJscwADAQ==/split_lib_resources_apk.apk           在dalvik.system.DexFile.openDexFileNative(本机方法)           在dalvik.system.DexFile.openDexFile(DexFile.java:354)           在dalvik.system.DexFile。(DexFile.java:101)           在dalvik.system.DexFile。(DexFile.java:75)           在dalvik.system.DexPathList.loadDexFile(DexPathList.java:394)           在dalvik.system.DexPathList.makeDexElements(DexPathList.java:354)           在dalvik.system.DexPathList。(DexPathList.java:164)           在dalvik.system.BaseDexClassLoader。(BaseDexClassLoader.java:74)           在dalvik.system.BaseDexClassLoader。(BaseDexClassLoader.java:65)           在dalvik.system.PathClassLoader。(PathClassLoader.java:64)   2018-07-25 14:20:10.485 22581-22581 /? E / AndroidRuntime:位于   com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:73)           在com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:88)           在android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:74)           在android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:40)           在android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:727)           在android.app.LoadedApk.getClassLoader(LoadedApk.java:810)           在android.app.LoadedApk.getResources(LoadedApk.java:1032)           在android.app.ContextImpl.createAppContext(ContextImpl.java:2345)           在android.app.ActivityThread.handleBindApplication(ActivityThread.java:5749)           在android.app.ActivityThread.access $ 1100(ActivityThread.java:199)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1650)                 ...还有6个

这是我的MainActivity Kotlin代码:

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
}

这是我的成绩依赖性:

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 }

}

这是我的第一个Android项目,主要来自c#/ swift背景。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

找不到类org.apache.commons.logging.LogFactory。您需要将commons-logging添加到依赖项。