Android开发各类常见错误解决方案

本文属于个人平时项目开发过程遇到的一些问题,记录下来并总结解决方案,希望能帮到大家解决问题,有些问题的解决方案是在StackoverFlow上找到的,建议大家遇到问题多去上面找,基本上都能找到解决方案的。

(1)将Eclipse项目导入到Android studio 中 很多点9图出现问题解决方法:
在build.gradle里添加以下两句:

aaptOptions.cruncherEnabled = false     
aaptOptions.useNewCruncher = false

用来关闭Android Studio的PNG合法性检查的,直接不让它检查。

(2)Android Studio 错误: 非法字符: '\ufeff' 解决方案|错误: 需要class, interface或enum

  • 原因:
    Eclipse可以智能的把UTF-8+BOM文件转为普通的UTF-8文件,Android Studio还没有这个功能,所以使用Android Studio编译UTF-8+BOM编码的文件时会出现” 非法字符: '\ufeff' “之类的错误

  • 解决方法:
    手动将UTF-8+BOM编码的文件转为普通的UTF-8文件。用EdItPlus打开.java文件依次:文档》文本编辑》转换文本编码》选择UTF-8编码即可

(3)将项目导入到AS中出现以下问题:

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. > com.android.bui
  • 解决方法:
    在build.grade中添加以下代码:
    android{
    packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
            }
    }

(4)未知错误

Error:Timeout waiting to lock cp_proj class cache for build file '/Users/Mr.xiao/Desktop/AndroidShopNC2014MoblieNew/androidShopNC2014Moblie/build.gradle' 
(/Users/Mr.xiao/.gradle/caches/2.10/scripts/build_3cyr7hzjurcc62ge3ixidshos/cp_proj).
It is currently in use by another Gradle instance.
Owner PID: unknown
Our PID: 1412
Owner Operation: unknown
Our operation: Initialize cache
Lock file: /Users/Mr.xiao/.gradle/caches/2.10/scripts/build_3cyr7hzjurcc62ge3ixidshos/cp_proj/cache.properties.lock
  • 解决方案
    以上是错误提示。
    解决的思路很简单只需要把cache.properties.lock文件删除了就可以了。当时我们删除的时候会被占用这时候需要进入任务管理器结束关于java的进程就行比如 java 的jdk 删除后重启让java jdk启动 启动Android Studio就能启动APK了。

(5)修改了Android项目的最小SDK版本之后出现很多stysle文件找不到

  • 解决方案
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
    applicationId "net.mmloo2014.android"
    minSdkVersion 14
    targetSdkVersion 23
    }

compileSdkVersion 是多少版本的

那么compile 'com.android.support:appcompat-v7:23.2.1’ 就是啥版本的。

(6)Android studio 编译问题:finished with non-zero exit value 2

  • 问题:
Error:Execution failed for task ':androidShopNC2014Moblie:transformClassesWithDexForDebug'.
>
com.android.build.api.transform.TransformException: 
com.android.ide.common.process.ProcessException: 
java.util.concurrent.ExecutionException: 
com.android.ide.common.process.ProcessException: 
org.gradle.process.internal.ExecException: 
Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2
  • 解决方案
    这个错误在app的build.gradle里面添加下面这句就好了。
android { 
defaultConfig { 
multiDexEnabled true
      }
}

(7)Android studio 编译问题:finished with non-zero exit value 1(由于导入的依赖出现重复造成的)

  • 问题:
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.

> com.[Android](http://lib.csdn.net/base/15).build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'F:\Program Files (x86)\[Java](http://lib.csdn.net/base/17)\jdk1.8.0_31\bin\java.exe'' finished with non-zero exit value 1
  • 解决方案
    这个是因为依赖包重复了 (像v4和nineoldandroids),app中实现了对easeUI的依赖,但是app和easeUI都添加了对这个包的依赖。所以就报这个错误,修改之后再报,就clean,rebuild一下。

(8)问题

Error:Execution failed for task 
':app:transformClassesWithJarMergingForDebug'.> 
com.android.build.api.transform.TransformException: 
java.util.zip.ZipException:
 duplicate entry: org/apache/http/ConnectionClosedException.class
  • 解决方案
    这个是在我们启动的时候报错的,而不是在编译的时候,原因是这样的,报这个错是因为有2个库中存在相同的类。大家可以看到stackoverflow上有人也提了这样的问题。只需要删除其中的一个就可以解决了。

(9)添加第三方依赖出现的问题

Error:Execution failed for task ':app:processDebugManifest'.
> 
Manifest merger failed :
 uses-sdk:minSdkVersion 14 cannot be smaller than version 19 declared in library [com.github.meikoz:basic:2.0.3] 
/AndroidStudioCode/EnjoyLife/app/build/intermediates/exploded-aar/
com.github.meikoz/basic/2.0.3/AndroidManifest.xml
Suggestion: use tools:overrideLibrary="com.android.core" to force usage
  • 错误原因
    出现这个错误的原因是我引入的第三方库最低支持版本高于我的项目的最低支持版本,异常中的信息显示:我的项目的最低支持版本为14,而第三方库的最低支持版本为19,所以抛出了这个异常。

  • 解决方案
    在AndroidManifest.xml文件中标签中添加

<uses-sdk tools:overrideLibrary="xxx.xxx.xxx"/>

其中的xxx.xxx.xxx为第三方库包名,如果存在多个库有此异常,则用逗号分割它们,例如:

<uses-sdk tools:overrideLibrary="xxx.xxx.aaa, xxx.xxx.bbb"/>

这样做是为了项目中的AndroidManifest.xml和第三方库的AndroidManifest.xml合并时可以忽略最低版本限制。

(10)Android studio 编译问题:finished with non-zero exit value 1(由于buildtools版本太高造成的)

  • 错误
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.ide.common.process.ProcessException: 
org.gradle.process.internal.ExecException: 
Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
  • 错误原因
    buildToolsVersion版本太高,我原来的 buildToolsVersion "24.0.0” 需要jdk1.8,而我的是jdk1.7,所以一直报这个错,刚开始以为是v4包和V7包冲突,因为之前遇到这样的问题,而这次删除V4包之后依然报这个错,上stackoverflow搜了一下,把buildTools版本降下来就好了。

  • 解决方案

android {

compileSdkVersion 23

buildToolsVersion "23.0.3"

}

(11)Android studio 编译问题:Gradle DSL not found 'android()'

  • 问题

clipboard.png
  • 解决方案
  • 配置build.gradle:
buildscript {
repositories {   
jcenter()
}

dependencies {   
classpath 'com.android.tools.build:gradle:2.1.2'
   }
}

allprojects {  
repositories {    
jcenter()
   }
}
buildscript {
repositories {      
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
    }
}


allprojects {
repositories {
jcenter()
    }
}
  • 配置app/build.gradle:
apply plugin: 'com.android.application'android { 
compileSdkVersion 23
 buildToolsVersion '23.0.3' 
defaultConfig { 
minSdkVersion 9 
targetSdkVersion 23 
versionCode 1 
versionName '1.0' 
             }
}
dependencies { 
compile 'com.android.support:appcompat-v7:23.2.1'
}

最后再同步一下sync即可。

(12)Android studio 编译问题:Gradle DSL not found 'android()'

  • 问题描述
Error:(51, 52) 错误: -source 1.6 中不支持 diamond 运算符
(请使用 -source 7 或更高版本以启用 diamond 运算符)
  • 解决方案
  • 方案一

将标红处设置为1.7.png

修改soure为1.7.png
  • 方案二
    在build gradle中进行配置如下代码:
    android {
    compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
    }
    }
    最后同步一下即可

(13)Glide使用问题:使用Glide加载圆角图片,第一次显示占位图

  • 问题描述
    最近在项目中使用Glide加载圆形图片,并且设置placehloder和error两个占位图,运行发现,第一次加载图片只显示占位图,需要第二次进入的时候才会正常显示。
    如果你刚好使用了这个圆形Imageview库或者其他的一些自定义的圆形Imageview,而你又刚好设置了占位的话,那么,你就会遇到第一个问题。如何解决呢?

  • 方案一
    不设置占位图

  • 方案二
    使用Glide的Transformation API自定义圆形Bitmap的转换

   /**
     * Glide圆形图片处理
     */
    static class CircleTransform extends BitmapTransformation {
        public CircleTransform(Context context) {
            super(context);
        }

        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            return circleCrop(pool, toTransform);
        }

        private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
            if (source == null) return null;

            int size = Math.min(source.getWidth(), source.getHeight());
            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;

            Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);

            Bitmap result = pool.get(size, size, Bitmap.Config.RGB_565);
            if (result == null) {
                result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
            }

            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            float r = size / 2f;
            canvas.drawCircle(r, r, r, paint);
            return result;
        }

        @Override
        public String getId() {
            return getClass().getName();
        }
    }

使用方法:

 Glide.with(context).load(imageUrl).placeholder(placeholder).error(errorImage).transform(new CircleTransform(context)).into(imageView);

方案三
重写Glide的图片加载监听方法,具体如下:

Glide.with(mContext) 
.load(url) 
.placeholder(R.drawable.loading_drawable) 
.into(new SimpleTarget<Bitmap>(width, height) {
 @Override public void onResourceReady(Bitmap bitmap, GlideAnimation anim) { 
// setImageBitmap(bitmap) on CircleImageView  
} 
});

注意事项:
该方法在listview上复用有问题的bug,如果在listview中加载CircleImageView,请不要使用该方法。

方案四:不使用Glide的默认动画:

Glide.with(mContext)
    .load(url) 
    .dontAnimate()
    .placeholder(R.drawable.loading_drawable)
    .into(circleImageview);

(14)json数据解析问题:json串头部出现字符:"\ufeff" 解决方法
异常信息

org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject

解析服务器返回 的json格式数据时,我们可能会发现,数据格式上是没有问题的,但是仔细对比会发现,在json串头部发现字符:"\ufeff"

客户端解决方案:

/** 
* 异常信息:org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject 
* json串头部出现字符:"\ufeff" 解决方法
* @param data 
* @return 
*/
public static final String removeBOM(String data) {    
if (TextUtils.isEmpty(data)) {        
         return data;    
}    
if (data.startsWith("\ufeff")) {        
        return data.substring(1);    
       } 
else {        
        return data;    
        }
}

服务器端解决方案:
将输出此json的php源码重新用editplus之类用utf-8无BOM的编码保存。不要用windows系统自带的记事本编辑php源码,这个BOM就是记事本这些windows自带的编辑器引入的。

(15)Android studio编译问题:not found ndk()
问题

Error:(15, 0) Gradle DSL method not found: 'ndk()' method-not-found-ndk

解决方案
出现该问题,可能是由于ndk配置在build.gradle配置文件中位置弄错导致的

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.guitarv.www.ndktest"
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        ndk {
            moduleName = "HelloJNI"
        }
        sourceSets.main {
            jni.srcDirs = []
            jniLibs.srcDir "src/main/libs"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

(16)Android studio导入其他的项目:UnsupportedMethodException
问题

UnsupportedMethodException
        Unsupported method: AndroidProject.getPluginGeneration().
        The version of Gradle you connect to does not support that method.
        To resolve the problem you can change/upgrade the target version of Gradle you connect to.
        Alternatively, you can ignore this exception and read other information from the model.

错误截图

解决方案

将根目录中的build.gradle文件中的gradle版本号,出现错误之前,我的是1.3.0,修改成2.2.0之后重新编译一下就可以运行了。

dependencies {    
classpath 'com.android.tools.build:gradle:1.3.0'   
}

将这个版本号改成你其他项目能够运行成功的版本号即可



文/jxnk25(简书作者)
原文链接:http://www.jianshu.com/p/c5db81cbc438
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值