最近几天完成了对公司项目从Eclipse到Studio的迁移,两者的对比相信都用过的人会懂。简单地列举几点:
- Studio看源码更加方便,无论是Android的还是Moudel的;
- Studio下载插件更加方便,相信用过Eclipse的"Install New Software"的人都懂;
- Studio支持gradle、ant、maven更加彻底,而Eclipse还要去下载gradle插件;
- 官方支持Studio的发展,我刚开始准备移植的时候用的Studio还是1.5.1正式版,现在都出到2.2 Preview1了(顺便吐槽下hotpatch确实快但是不够稳定啊);【手动笑哭】
- 便于解决65536问题,便于及时清除重复的、不需要的依赖和permission;
……
开始正题,我秉着“先解决最难的,简单的就是小菜一碟”的原则,移植的是公司用到最多第三方组件的用户端(使用maven编译),共涉及百度地图搜索定位模块、支付宝支付、微信支付、银联支付、jpush、async-http、ksoap、leancloud的聊天组件(顺便引入了一系列组件)、ormlite、universal-image-loader等。
大致实践步骤:
- 新建项目
- 获取到公司仓库里的包并尝试获取Model
- 获取其他jar包
- 调用数据获取接口尝试获取数据
- 将项目代码导入
- 将Moudel导入,我这里导入了leanchatlib
花式出错:
1.UNEXPECTED TOP-LEVEL EXCEPTION:
com.Android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version
解决办法
2.app:transformResourcesWithMergeJavaResForDebug
出现原因:主app和moudel中引用了相同的包 且两个包中也包含相同的包
Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/maven/包名/proxy/pom.xml
File1: C:\Users\Kerchin\.gradle\caches\modules-2\files-2.1\包名\proxy\0.0.1-SNAPSHOT\b6ace00dfdbae1d6d37314fbf51efe562eaae301\proxy-0.0.1-SNAPSHOT.jar
File2: C:\Users\Kerchin\.gradle\caches\modules-2\files-2.1\包名\proxy\0.0.1-SNAPSHOT\b6ace00dfdbae1d6d37314fbf51efe562eaae301\proxy-0.0.1-SNAPSHOT.jar
最佳解决办法:删除主app中的compile,保留moudel中的,并两边的build.gradle中都添加如下代码
packagingOptions {
exclude 'META-INF/maven/包名/proxy/pom.xml'
exclude 'META-INF/maven/包名/proxy/pom.properties'
exclude 'META-INF/maven/包名/util/pom.xml'
exclude 'META-INF/maven/包名/util/pom.properties'
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
}
3.Android Studio中的.so文件导入方法
- 一开始使用的是
在app/libs
下放入所有需要的文件再在app/build.gradle
的android{}中添加如下代码
task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {
destinationDir file("$projectDir/libs")
baseName "Native_Libs2"
extension "jar"
from fileTree(dir: "libs", include: "**/*.so")
into "lib"
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
}
原理是将libs中的.so文件打包在Native_Libs2.jar
中再一同放进app中
- 后来发现一种更直接的方式
在````app/src/main中新建名为
jniLibs```的文件夹并放入即可
4.com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes
leancloud的聊天模块的verifyError
这个bug很奇怪,在Eclipse并不会出现
java.lang.NoClassDefFoundError: com.avos.avoscloud.PostHttpResponseHandler
W/System.err: at com.avos.avoscloud.PaasClient.createPostHandler(PaasClient.java:288)
W/System.err: at com.avos.avoscloud.PaasClient.postBatchSave(PaasClient.java:533)
W/System.err: at com.avos.avoscloud.AVObject.saveObjectToAVOSCloud(AVObject.java:1916)
W/System.err: at com.avos.avoscloud.AVObject._saveObject(AVObject.java:1875)
W/System.err: at com.avos.avoscloud.AVObject.saveObject(AVObject.java:1812)
W/System.err: at com.avos.avoscloud.AVObject.saveInBackground(AVObject.java:2266)
W/System.err: at com.avos.avoscloud.AVObject.saveInBackground(AVObject.java:2234)
W/System.err: at com.avos.avoscloud.PushService.onStartCommand(PushService.java:140)
W/System.err: at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2897)
W/System.err: at android.app.ActivityThread.access$2400(ActivityThread.java:139)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378)
5.之前以apklib的形式导入的leanchatlib,studio不再支持
在 leanchatlib的build.gradle中将apply plugin: 'com.android.application'
改成apply plugin: 'com.android.library'
在app的build.gradle中将apk project(':leanchatlib')
改为compile project(':leanchatlib')
6.Error:Execution failed for task ':app:processDebugManifest'
7.Android libpng error: Not a PNG file错误解决
8.Error:Execution failed for task ':ProjectName:mergeDebugResources'. > Crunching Cruncher some file failed, see logs
6..so
的导入
尚未解决的一个问题
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.baidu.mapapi.map.m) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
共261个类似错误,涵盖baidumap、unionpay、alipay、tencent的资源
而且由于是编译时错误,导致编译很卡,每次3分钟以上T.T