介绍Android SDK在开发过程中遇到的常见问题和解决方法。

Q:登录App或注册时,提示网络不顺畅

A:检查以下内容是否正确。

  • 确认手机时间是否设置正确,如果有偏差,则会被安全监测拦截而导致无法登录。
  • 确认安全图片是否在src/drawable目录下,如果在初始化代码中设置了图片后缀为 114d ,请务必保证安全图片的名称为yw_1222_114d
    //设置图片后缀
    ConfigManager.getInstance().setSecGuardImagePostfix("114d");
  • 确认初始化中是否有抛出异常。
    • 其中ErrorCode=110的异常码可以忽略
    • 以下几种Warn可以忽略(用来检查社交账号SDK)
      W/oa_Oauth: [] : Umeng is not available, Umeng Oauth Service Provider is disabled
      W/oa_Oauth: [] : Taobao MemberSDK is not available, Taobao 3rd Oauth Service Provider is disabled
      W/oa_Oauth: [] : Taobao login4android SDK is not available, Taobao 2nd Oauth Service Provider is disabled
      W/oa_Oauth: [] : Alipay sdk is not available, Alipay Oauth Service Provider is disabled
      							
    • 如果是用Demo中提供的OALoginAdapter,下面异常日志可以忽略
      java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
      at com.aliyun.iot.aep.sdk.login.oa.OALoginAdapter.a(OALoginAdapter.java:343)

无法打开登录页面

A:检查以下内容是否正确。

  • 确认日志中是否有以下异常输出。
    init failed code = 10010 message = 发生错误,消息为 null, 请使用 LogCat 查看更多细节
    或者
    Failed resolution of: Lcom/ut/mini/UTHitBuilders@UTCustomHitBuilder
    这种情况说明主工程缺少UT SDK的依赖,请在主工程里增加如下两个依赖,或者在平台上重新下载SDK依赖文件。
    compile 'com.aliyun.ams:alicloud-android-utdid:1.1.5.4'
    compile 'com.aliyun.ams:alicloud-android-ut:5.1.0'
    						
  • 如果启动直接Crash,查看日志有提示XML相关的报错,请检查Manifest.xml中的packageName和build.gradle中的applicationID必须保持一致。

Q:API通道SDK初始化时,出现ErrorCode=103错误

A:查看logcat中是否有类似如下的报错日志:

12-16 16:26:11.430 10486-10486/? W/System.err: ErrorCode = 103
12-16 16:26:11.430 10486-10486/? W/System.err: com.alibaba.wireless.security.open.SecException: java.lang.UnsatisfiedLinkError: dlopen failed: "/data/data/com.aliyun.iot.demo.wuxi.demoapp/app_SGLib/app_1513412765/libsgmainso-5.3.38.so" is 32-bit instead of 64-bit
12-16 16:26:11.430 10486-10486/? W/System.err:     at com.alibaba.wireless.security.mainplugin.SecurityGuardMainPlugin.onPluginLoaded(Unknown Source)
12-16 16:26:11.430 10486-10486/? W/System.err:     at com.alibaba.wireless.security.framework.e.a(Unknown Source)
12-16 16:26:11.430 10486-10486/? W/System.err:     at com.alibaba.wireless.security.framework.e.c(Unknown Source)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.alibaba.wireless.security.framework.e.c(Unknown Source)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.alibaba.wireless.security.open.initialize.b.a(Unknown Source)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.alibaba.wireless.security.open.initialize.a.loadLibrarySync(Unknown Source)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.alibaba.wireless.security.open.initialize.a.initialize(Unknown Source)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.alibaba.wireless.security.jaq.SecurityInit.Initialize(Unknown Source)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.aliyun.iot.demo.wuxi.demoapp.DemoApplication.onCreate(DemoApplication.java:34)
12-16 16:26:11.431 10486-10486/? W/System.err:     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1025)
12-16 16:26:11.431 10486-10486/? W/System.err:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5403)
12-16 16:26:11.431 10486-10486/? W/System.err:     at android.app.ActivityThread.-wrap2(ActivityThread.java)
12-16 16:26:11.431 10486-10486/? W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
12-16 16:26:11.431 10486-10486/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
12-16 16:26:11.431 10486-10486/? W/System.err:     at android.os.Looper.loop(Looper.java:154)
12-16 16:26:11.431 10486-10486/? W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6119)
12-16 16:26:11.431 10486-10486/? W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
			

如果出现以上日志,一般是so文件加载失败导致的,可以在build.gradle文件中添加如下配置:

android {
    compileSdkVersion 26
    defaultConfig {

        ...

        ndk {
            abiFilters "armeabi", "x86"//过滤除 armeabi 和 x86 以外的 so 文件
        }
    }
}
			

Q:API通道SDK初始化时,出现app key or app secret must be initialed错误

A:查看logcat中是否有类似如下的报错日志如下。

10-22 03:03:37.555 18552-18552/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.rnd.kx, PID: 18552
    java.lang.RuntimeException: Unable to create application com.rnd.kx.MyApplication: com.alibaba.cloudapi.sdk.exception.SdkException: app key or app secret must be initialed
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4710)
        at android.app.ActivityThread.-wrap1(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: com.alibaba.cloudapi.sdk.exception.SdkException: app key or app secret must be initialed
			

如果出现以上日志,检查以下内容是否正确。

  • 检查是否出现203错误码,并根据导致错误的可能原因进行详细排查。
  • 请检查libsgmain.so是否被打到apk包内,要确保lib/armeabi和lib/x86文件夹内包含该文件。

Q:API 通道 SDK 初始化的时候,出现ErrorCode=202错误

A:查看logcat中是否有类似如下的报错日志如下。

12-16 13:19:06.586 9344-9344/com.aliyun.iot.demo W/System.err: ErrorCode = 202
12-16 13:19:06.587 9344-9344/com.aliyun.iot.demo W/System.err: com.alibaba.wireless.security.open.SecException:
12-16 13:19:06.594 9344-9344/com.aliyun.iot.demo W/System.err:     at com.taobao.wireless.security.adapter.JNICLibrary.doCommandNative(Native Method)
12-16 13:19:06.595 9344-9344/com.aliyun.iot.demo W/System.err:     at com.alibaba.wireless.security.mainplugin.a.doCommand(Unknown Source:0)
12-16 13:19:06.595 9344-9344/com.aliyun.iot.demo W/System.err:     at com.alibaba.wireless.security.a.j.a.getAppKeyByIndex(Unknown Source:21)
12-16 13:19:06.596 9344-9344/com.aliyun.iot.demo W/System.err:     at com.alibaba.sdk.android.push.securitybox.alipush.a.getAppKey(Unknown Source:7)
12-16 13:19:06.597 9344-9344/com.aliyun.iot.demo W/System.err:     at com.alibaba.sdk.android.push.vip.AppRegister.h(Unknown Source:17)
12-16 13:19:06.597 9344-9344/com.aliyun.iot.demo W/System.err:     at com.alibaba.sdk.android.push.vip.AppRegister.a(Unknown Source:34)
12-16 13:19:06.597 9344-9344/com.aliyun.iot.demo W/System.err:     at com.alibaba.sdk.android.push.impl.j.a(Unknown Source:30)
12-16 13:19:06.597 9344-9344/com.aliyun.iot.demo W/System.err:     at com.alibaba.sdk.android.push.impl.j.register(Unknown Source:1)
12-16 13:19:06.599 9344-9344/com.aliyun.iot.demo W/System.err:     at com.aliyun.iot.aep.demo.DemoApplication.initPush(DemoApplication.java:115)
12-16 13:19:06.599 9344-9344/com.aliyun.iot.demo W/System.err:     at com.aliyun.iot.aep.demo.DemoApplication.onCreate(DemoApplication.java:27)
12-16 13:19:06.599 9344-9344/com.aliyun.iot.demo W/System.err:     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1118)
12-16 13:19:06.599 9344-9344/com.aliyun.iot.demo W/System.err:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5791)
12-16 13:19:06.599 9344-9344/com.aliyun.iot.demo W/System.err:     at android.app.ActivityThread.-wrap1(Unknown Source:0)
12-16 13:19:06.601 9344-9344/com.aliyun.iot.demo W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661)
12-16 13:19:06.601 9344-9344/com.aliyun.iot.demo W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:105)
12-16 13:19:06.601 9344-9344/com.aliyun.iot.demo W/System.err:     at android.os.Looper.loop(Looper.java:164)
12-16 13:19:06.601 9344-9344/com.aliyun.iot.demo W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6541)
12-16 13:19:06.601 9344-9344/com.aliyun.iot.demo W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-16 13:19:06.604 9344-9344/com.aliyun.iot.demo W/System.err:     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
12-16 13:19:06.604 9344-9344/com.aliyun.iot.demo W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
			

如果出现以上日志,通常是安全图片与当前apk的签名不匹配导致的,可以修改当前apk签名或者重新上传apk生成新的安全图片。

Q:API通道SDK初始化时,出现ErrorCode=203错误

A:查看logcat中是否有类似如下的报错日志如下。

12-16 13:29:59.005 10232-10267/? W/System.err: ErrorCode = 203
12-16 13:29:59.005 10232-10267/? W/System.err: com.alibaba.wireless.security.open.SecException:
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.taobao.wireless.security.adapter.JNICLibrary.doCommandNative(Native Method)
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.alibaba.wireless.security.mainplugin.a.doCommand(Unknown Source:0)
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.alibaba.wireless.security.a.j.a.getAppKeyByIndex(Unknown Source:21)
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.alibaba.sdk.android.push.securitybox.alipush.a.getAppKey(Unknown Source:7)
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.alibaba.sdk.android.push.securitybox.alipush.a.a(Unknown Source:8)
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.alibaba.sdk.android.push.securitybox.alipush.a.getMpsDeviceId(Unknown Source:2)
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.alibaba.sdk.android.push.vip.AppRegister$a.b(Unknown Source:68)
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.alibaba.sdk.android.push.vip.AppRegister$a.a(Unknown Source:0)
12-16 13:29:59.006 10232-10267/? W/System.err:     at com.alibaba.sdk.android.push.vip.b.handleMessage(Unknown Source:54)
12-16 13:29:59.006 10232-10267/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:105)
12-16 13:29:59.006 10232-10267/? W/System.err:     at android.os.Looper.loop(Looper.java:164)
12-16 13:29:59.006 10232-10267/? W/System.err:     at android.os.HandlerThread.run(HandlerThread.java:65)
			

如果出现以上日志,检查以下内容是否正确。

  • 没有找到图片文件,请确保安全图片在res\drawable目录下,并检查安全图片的名称的末四位和当前authCode是否一致(authCode 默认为 114d)。
  • 安卓环境下可能是因为资源优化被优化成了0,请检查APK中的图片。
    • 如果开启混淆,需要检查发布包335大小不为0,shrinkResources true会导致yw_1222_0335和yw_1222图片大小为0。
      release {
                 minifyEnabled true // 是否混淆
                 shrinkResources true //会导致335或者122图片大小为0
                 proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
               }

      可通过放弃资源压缩或新建res/raw/keep.xml文件,并在文件中添加以下内容。

      <?xml version="1.0" encoding="UTF-8"?>
      resources xmlns:tools="http://schemas.android.com/tools"
          tools:keep="@drawable/yw_1222_0335, @drawable/yw_1222"/>
    • 如果是在android studio下调试发现找不到图片,但是确认过图片是正常的,请关闭Android studio的 instant run功能,在instant run下apk并非一个完整bundle,其资源文件被拆分到特定bundle中,图片路径发生改变会导致保镖找不到图片。

Q:无法初始化登录SDK,提示发生错误且消息为null

init failed code = 10010 message = 

A:可以使用LogCat查看更多日志细节。可能原因为缺少SDK依赖,请在主工程的build.gradle中添加以下2个依赖,或者在平台上重新生成一份dependency.gradle文件。

compile 'com.aliyun.ams:alicloud-android-utdid:1.1.5.4'
compile 'com.aliyun.ams:alicloud-android-ut:5.1.0'

Q:访问API时,API网关出现以下报错

code:403, message:request forbidden, localizedMsg:请求被禁止

A:可能原因为该项目APPKEY没有访问API的权限,请提工单,备注APPKEY以及请求API的名称。

Q:出现以下报错

unsupported auth type iotAuth, maybe you forgot to register IoTAuthProvider

A:参看身份认证SDK文档初始化SDK,初始化IoTCredentialProviderImpl模块代码也放在应用内。如需要主动调用接口还需添加setAuthType("iotAuth")

Q:demoApp切换国际站或更换安全图片后,提示网络不顺畅或无法连接

A:解决办法如下。

  • BuildConfig.BUILD_COUNTRY=OVERSEA
  • build.gradle里面的CHINA更改为OVERSEA
  • DemoApplication里面的SINGAPORE都更改为OVERSEA
  • drawable里面的安全图片需要更改为国际站的
  • EnvConfigure.putEnvArg(RNContainerComponentDelegate.KEY_RN_CONTAINER_PLUGIN_ENV, "test");更改为EnvConfigure.putEnvArg(RNContainerComponentDelegate.KEY_RN_CONTAINER_PLUGIN_ENV, "release");
  • RNContainerComponentDelegate.java里面的BoneConfig.set("region", "china");更改为BoneConfig.set("region", "singapore");
  • 如采用release.gradle里面打包apk方式,更改src/oversea/res/configure/oversea/里的安全图片