Android Studio 错误汇总

gradle

gradle sync太慢的问题

原因是国内访问 jcenter 太慢,甚至连接不上,就会报各种关于依赖更新失败的错误。
以前oschina(也就是开源中国)提供了 jcenter 的镜像地址,然而不幸的是因为各种原因,最近停止服务了。
开源中国 Maven 镜像库关闭访问
幸运的是阿里云提供了 jcenter 镜像 http://maven.aliyun.com/
可以在工程 gradle 中如下修改,加速 gradle sync

buildscript {  
    repositories {  
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public'}  
        //jcenter()  
    }  
    dependencies {  
        classpath 'com.android.tools.build:gradle:2.2.2'  

        // NOTE: Do not place your application dependencies here; they belong  
        // in the individual module build.gradle files  
    }  
}  

allprojects {  
    repositories {  
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public'}  
        //jcenter()  
    }  
}  
MissingTranslation

在Android编译时发现这个错误,见下图

最终在StackOverFlow上找到了解决方法,大概有这么几种方法,现整理如下
1、尝试添加translatable="[true/false]"

<string name="junkchen" translatable="false">Junk Chen!</string>

2、在 resources 中添加属性

<resources 
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="MissingTranslation" >
    ...
</resources>

3、指定语言

<resources 
    xmlns:tools="http://schemas.android.com/tools"
    tools:locale="en" >
    ...
</resources>

4、使用 Android Studio 可以在 build.gradle 中的android中添加lintOptions

lintOptions {
    disable 'MissingTranslation'
}

或者

lintOptions {
    checkReleaseBuilds false
    abortOnError false
}

release打包

Error:A problem was found with the configuration of task ':app:packageRelease'.

今天打包时出现Error:A problem was found with the configuration of task ':app:packageRelease'.的错误提示。想了一下才发现,在gradle里面添加了这个属性:shrinkResources true.,表示在打包的时候会去删除一些不必要的文件,结果就打包失败了。

Error:The number of method references in a .dex file cannot exceed 64K.

当打包时出现Error:The number of method references in a .dex file cannot exceed 64K.的错误提示。具体解决方法参考这里
1.在app的 build.gradle 中添加配置

...
android {
    ...
    defaultConfig {
        ...
        multiDexEnabled true
    }
}
dependencies {
    ...
    compile 'com.android.support:multidex:1.0.1'
    ...
}

2.在AndroidManifest.xml中的application标签中添加继承MultiDexApplication的类。当然,也可以不用继承MultiDexApplication类,而是在继承的Application类中加入下面的代码

    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

至于原因嘛,看下MultiDexApplication的源码就明白了

package android.support.multidex;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

public class MultiDexApplication extends Application {
    public MultiDexApplication() {
    }

    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
混淆XmlPullParser报错

我们知道,android sdk是自带org.xmlpull.v1的依赖包的,那么就不应该存在混淆的问题,那么之所以会出现,就是因为我们引用的library包中存在相同的依赖!通过排查,终于发现问题出在ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar 包中
最简单的解决方法,就是移除该library的依赖即可。

Robolectric

当使用Robolectric框架进行测试准备时,通常出现下面的情况

什么情况,就这样不动了?!要解决这个问题,最直接的办法就是翻墙。然而,这里,我却要记载一种取巧的办法。出现上图的情况时,这时可以打开下图的文件路径

随便打开其中的一个文件夹

果然,不翻墙的话,就下载不了,这时,就可以通过其它途径下载相关的文件,然后将这些文件放入到这些对应的文件夹中去。下载链接https://oss.sonatype.org/content/groups/public/org/robolectric/

当然,如果在其它电脑上有现成的依赖,也可以复制过来

都复制过来之后,再运行之前的单元测试文件

额,好吧,还有其它依赖,不过,从另一个层面来说,上面的取巧确实可行,那么就根据上面的取巧原理再进行拷贝,最后运行成功!

其实在运行成功之前,还报了一个错误

而解决办法就是将相关依赖工程设置为release即可,至于原因至今还没想通,目前先在这里做个记录

今天发现一位同僚也对这个问题做了记录,而且更加丰富详细,可以去这里看看。

Android Studio更新至2.3版本后,gradle采用的是3.3版本,之前用Robolectric做的单元测试会报错,目前还没有好的解决方案,暂时先用Android Studio 2.2.3版本比较好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值