Android 应用程序升级到 5.0 需要注意的问题
Android 5.0,代号 Lollipop,源码终于在2014年12月3日放出,国内一大批厂商跟进。最大的改变是默认使用 ART(Android Runtime) ,替换了之前的 Dalvik 虚拟机,提出了 Material Design 界面风格。之前发布的 app 可能需要作一些改动,暂时收集了一些问题,希望对大家有所帮助。
1. Intent/Service
在低于 Android 5.0 版本,程序运行正常。用户抱怨在新的 Android 5.0 设备上崩溃,我们还没有最新的设备,所以暂时用 Android 模拟器调试。
在输出的 log 中可以看到这样的记录:
E/AndroidRuntime(26479): java.lang.RuntimeException: Unable to start activity ComponentInfo{PACKAGE_NAME/.ACTIVITY_NAME}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.bda.controller.IControllerService }
E/GameActivity(18333): Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.bda.controller.IControllerService }
E/GameActivity(18333): at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1982)
E/GameActivity(18333): at android.app.ContextImpl.startServiceCommon(ContextImpl.java:2020)
E/GameActivity(18333): at android.app.ContextImpl.startService(ContextImpl.java:1995)
E/GameActivity(18333): at android.content.ContextWrapper.startService(ContextWrapper.java:533)
E/GameActivity(18333): at com.bda.controller.a.d(Unknown Source)
通过查看堆栈崩溃信息,我们看到使用了第三方的 controller.jar 包导致错误。Controller 是在设备屏幕上模拟游戏手柄功能的包,下载最新的 Moga developers SDK ,下载了 controller-sdk-std-1.3.1.zip,2013 Feb 01 发布的,有点旧了。里面有 com.bda.controller.jar,没有源码。
尝试 zip 解压 controller.jar 文件,反编译 .class 文件 com/bda/controller/BaseController.class
想查看 bytecode,使用 javap -c BaseController.class
public final boolean init();
Code:
: aload_0
: getfield # // Field mIsBound:Z
: ifne
: new # // class android/content/Intent
: dup
: ldc # // class com/bda/controller/IControllerService
: invokevirtual # // Method java/lang/Class.getName:()Ljava/lang/String;
: invokespecial # // Method android/content/Intent."<init>":(Ljava/lang/String;)V
: astore_1
: aload_0
: getfield # // Field mContext:Landroid/content/Context;
: aload_1
: invokevirtual # // Method android/content/Context.startService:(Landroid/content/Intent;)Landroid/content/ComponentName;
: pop
: aload_0
: getfield # // Field mContext:Landroid/content/Context;
: aload_1
: aload_0
: getfield # // Field mServiceConnection:Lcom/bda/controller/Controller$ServiceConnection;
: iconst_1
: invokevirtual # // Method android/content/Context.bindService:(Landroid/content/Intent;Landroid/content/ServiceConnection;I)Z
: pop
: aload_0
: iconst_1
: putfield # // Field mIsBound:Z
: aload_0
: getfield # // Field mIsBound:Z
: ireturn
init
你当然想查看源代码,用反编译工具 jad,或者临时用网络在线版 Show My Code,这个网站可以查看 Zend Guard 加密过的 .php 文件、Java 的 .class 文件、Adobe Flash 的 .swf 文件、.NET 程序 .exe, .dll 或者 QR 二维码,可以收藏一下。
public final boolean init()
{
if(!mIsBound)
{
Intent intent = new Intent(com.bda.controller.IControllerService.getName());
mContext.startService(intent);
mIsBound = mContext.bindService(intent, this, Context.BIND_AUTO_CREATE);
}
return mIsBound;
}
根据上面的错误和代码看出,这里需要使用显式的 Intent(通过 setComponent(ComponentName) 或者 setClass(Context, Class) 设置了 Component 的 Intent),上面的一句需要改成 Intent intent = new Intent(mContext, IControllerService.class);
或者 Intent intent = new Intent("com.bda.controller.IControllerService").setPackage("com.bda.controller");
官方文档 也提到使用显式的 Intent 来 startService/bindService 以确保安全。
Caution: To ensure your app is secure, always use an explicit intent when starting a Service and do not declare intent filters for your services. Using an implicit intent to start a service is a security hazard because you cannot be certain what service will respond to the intent, and the user cannot see which service starts. Beginning with Android 5.0 (API level 21), the system throws an exception if you call bindService() with an implicit intent.
Note: When starting a Service, you should always specify the component name. Otherwise, you cannot be certain what service will respond to the intent, and the user cannot see which service starts.
很多第三方的库都暴露出这种问题,需要更新一下。我们也用了 Google 的 Analytics tracking 库 libGoogleAnalyticsV2.jar。
E/GameActivity( 1137): java.lang.RuntimeException: Unable to start activity ComponentInfo{PACKAGE_NAME/ACTIVITY_NAME}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.google.android.gms.analytics.service.START (has extras) }
尝试更新了到 v3 (现在有 v4 了)解决问题,需要改动一些代码。这里有文档迁移需要修改什么,EasyTracker: v2.x to v3。
2. MD5 符号找不到了。
MD5_CTX context;
MD5_Init(&context);
const char* text = "Hello, world!";
MD5_Update(&context, text, sizeof(text));
MD5_Final(md5_result, &context);
崩溃的 log 如下
E/art(21678): dlopen("/data/app/PACKAGE_NAME/lib/arm/libsixguns.so", RTLD_LAZY) failed: dlopen failed: cannot locate symbol "MD5_Final" referenced by "libXYZ.so"...
E/GAME(21678): native code library failed to load.
E/GAME(21678): java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "MD5_Final" referenced by "libXYZ.so"...
这是因为 Google 修改了底层 bionic libc 库的实现 ,一些隐藏的 API 移除了。一些依赖这些函数的代码可能无法运行。
修改方案,可以自行导入 MD5 库,反正代码也简短。
或者添加 --whole-archive 静态链接 crypto 库。因为 OpenSSL 也提供了 MD5 的实现,可以借用。openssl/crypto/md32_common.h
为了在最终的 .so 库中包含这些定义,添加 ld 链接命令 -Wl,-whole-archive crypto -Wl,-no-whole-archive。
--whole-archive 将在链接中包含归档文件 .a 所有的 .o 文件,而不是只在需要时才搜索,用来转 .a 文件成 .so 文件。gcc 不识别这个命令,所以需要使用 -Wl,-whole-archive,用好后需要添加 -Wl,-no-whole-archive 结束,因为 gcc 会在链接中会添加自己的 .a,以免受影响。
3. ART 模式下随机崩溃。
Dalvik 对于 native 代码和 Java 代码提供各自的栈,默认 native 栈有 1MB、Java 栈有 32KB。而在 ART 模式下,提供统一的栈。按说,ART 线程栈的大小应该与 Dalvik 的一样。如果你显式设置栈的大小,你可能需要在 ART 模式下运行的 app 里重新访问这些值。
Java 的 Thread 类有一个构造函数 Thread(ThreadGroup group, Runnable runnable, String threadName, long stackSize) 提供栈大小参数的设置,如果运行中出现 StackOverflowError 错误,可能需要手动增大 stackSize 值了。
C/C++ 需要调用 POSIX thread 的函数 pthread_attr_setstack() 和 pthread_attr_setstacksize()。如果 pthread 栈太小, 调用 JNI AttachCurrentThread() 方法会打印如下 log:
F/art: art/runtime/thread.cc:435] Attempt to attach a thread with a too-small stack (16384 bytes)
使用 JNI 的时候,出于效率因素,可能需要缓存一些方法 FindClass/GetFieldID/GetMethodID 返回的 ID,千万不要缓存 JNIEnv*,也不要在应用程序的整个生命周期将 native 线程附加到 Java 线程。用 adb shell setprop debug.checkjni 1 命令可以调试一些 JNI 错误,它不会影响已经运行的应用程序。
ART 模式下的 JNI 可能会抛出一些 Dalvik 不会抛的错误,可以用 CheckJNI,也就是上面的命令行捕捉错误。比如,在 proguard 混淆代码的时候脱掉了一些 native 方法,运行时候会抛 NoSuchMethodError 异常。现在 GetFieldID() 和 GetStaticFieldID() 方法抛出 NoSuchMethodError 异常,而不是返回 null。
E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: java.lang.NoSuchMethodError: no static or non-static method "Lcom/foo/Bar;.native_frob(Ljava/lang/String;)I"
E/AndroidRuntime: at java.lang.Runtime.nativeLoad(Native Method)
E/AndroidRuntime: at java.lang.Runtime.doLoad(Runtime.java:)
E/AndroidRuntime: at java.lang.Runtime.loadLibrary(Runtime.java:)
E/AndroidRuntime: at java.lang.System.loadLibrary(System.java:)
参考:
Verifying App Behavior on the Android Runtime (ART)
Will my Android App still run with ART instead of Dalvik?
Android 应用程序升级到 5.0 需要注意的问题的更多相关文章
- Android studio 程序升级和sdk manager 升级方法
在中国使用android有点郁闷,经常被屏蔽.常遇到2个升级问题,现在总结如下: 1.android studio升级时提示 Connection failed. Please check your ...
- android apk程序升级
1 .设置apk版本号 Androidmanifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/ ...
- Android应用程序的自动更新升级(自身升级、通过tomcat)(转)
Android应用程序的自动更新升级(自身升级.通过tomcat) http://blog.csdn.net/mu0206mu/article/details/7204746 刚入手android一个 ...
- CMS .NET 程序框架 从2.0/3.5升级到4.0 版本后 需要调整的地方
问题一: document.forms1.action 不可使用 需要修改程 document.forms[0] .NET 程序框架 从2.0/3.5升级到4.0 版本后,document.forms ...
- Android Studio 升级到3.0 提示 java.lang.NoClassDefFoundError
Android Studio 升级到3.0 提示 java.lang.NoClassDefFoundError 这个问题折腾了2个小时,最后解决了,Stack Overflow 上也有一次类似的问题, ...
- Android Studio 升级到3.0后出现编译错误\.gradle\caches\transforms-1\files-1.1\*****-release.aar
Android Studio 升级到3.0后出现各种编译问题,其中有一个问题是关于资源找不到的问题,百度了半天,也没有相关的文章 C:\Users.gradle\caches\transforms-1 ...
- ArcGIS for Android入门程序之DrawTool2.0
来自:http://blog.csdn.net/arcgis_mobile/article/details/8084763 GISpace博客<ArcGIS for Android入门程序之Dr ...
- Android笔记——数据库升级与降级
一.概述 SQLite是Android内置的一个很小的关系型数据库.SQLiteOpenHelper是一个用来辅助管理数据库创建和版本升级问题的抽象类.我们可以继承这个抽象类,实现它的一些方法来对数据 ...
- 【转】android应用程序签名
概述 Android系统要求,所有的程序经过数字签名后才能安装.Android系统使用这个证书来识别应用程序的作者,并且建立程序间的信任关系.证书不是用于用户控制哪些程序可以安装.证书不需要授权中心来 ...
随机推荐
- bzoj2683
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 1018 Solved: 413[Submit][Status][Discuss] ...
- 从基层容器类看万变不离其宗的JAVA继承体系
以容器类为例子,可以观一叶而知秋,看看以前的前辈们是如何处理各种面向对象思想下的继承体系的.读的源代码越多,就越要总结这个继承关系否则读的多也忘得快. 首先摆上一张图片: 看到这张图很多人就慌了,难道 ...
- NSUserDefaults:熟悉与陌生(转)
转载自:http://swiftcafe.io/2016/04/04/nsuserdefaults/?hmsr=toutiao.io&utm_medium=toutiao.io&utm ...
- UnixC学习小结
1.malloc工作原理: malloc使用一个数据结构(链表)维护分配空间 链表的构成:分配的空间/上一个空间数据/下一个空间/空间大小等信息. 对malloc分配的空间不要 ...
- IP地址,子网掩码、默认网关,DNS服务器是什么意思?
(一) 问题解析001. 问: IP地址,子网掩码,默认网关,DNS服务器,有什么区别呀?我知道没有IP地址就不能上网,我也知道没设DNS就不能上外网,可它们都有什么功能,有什么区别呢?还有真 ...
- C# 调用WebService的3种方式 :直接调用、根据wsdl生成webservice的.cs文件及生成dll调用、动态调用
1.直接调用 已知webservice路径,则可以直接 添加服务引用--高级--添加web引用 直接输入webservice URL.这个比较常见也很简单 即有完整的webservice文件目录如下图 ...
- %我的 tex 模版
%我的 tex 模版 \documentclass[UTF8,a1paper,landscape]{ctexart}%UTF8 中文支持,a1paper 纸张大小,landscape 横向版面,cte ...
- 笔记:MAC OS X下配置PHP开发、调试环境
操作系统:MAC OS X 工具:MAMP.PhpStorm.xdebug.chrome 1.下载MAMP 2.安装比较简单,安装完成后,应用程序中会增加如下4个应用 MacGDBp是PHP调试器,使 ...
- 在Application中集成Microsoft Translator服务之获取访问令牌
我在这里画了一张图来展示业务逻辑 在我们调用microsoft translator server之前需要获得令牌,而且这个令牌的有效期为10分钟.下表列出所需的参数和对于的说明 参数 描述 clie ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...