1. 程式人生 > >Android開發錯誤——Android Studio中遇到過的錯誤問題與解決方案彙總

Android開發錯誤——Android Studio中遇到過的錯誤問題與解決方案彙總

(1)編譯時發生

Error:(25, 0) Gradle DSL method not found: 'compile()'
Possible causes:<ul><li>The project 'AP' may be using a version of Gradle that does not contain the method.
<a href="openGradleSettings">Gradle settings</a></li><li>The build file may be missing a Gradle plugin.
<a href="apply.gradle.plugin">Apply Gradle plugin</a></li>

解決方法:

1、在最外層的Gradle中,刪除如下的語句

dependencies {
    compile files('libs/xUtils-2.6.14.jar')
}

(2)編譯時發生

Error:(25, 0) Gradle DSL method not found: 'android()'
Possible causes:<ul><li>The project 'AP' may be using a version of Gradle that does not contain the method.
<a href="openGradleSettings">Gradle settings</a></li><li>The build file may be missing a Gradle plugin.
<a href="apply.gradle.plugin">Apply Gradle plugin</a></li>

解決方法:

1、在最外層的Gradle中,刪除如下的語句

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'
}

(3)編譯時發生

Error:(1, 0) Your project path contains non-ASCII characters. This will most likely cause the build to fail on Windows. Please move your project to a different directory. See http://b.android.com/95744 for details. This warning can be disabled by using the command line flag -Dcom.android.build.gradle.overridePathCheck=true, or adding the line com.android.build.gradle.overridePathCheck=true' to gradle.properties file in the project directory.
<a href="openFile:D:\workspacebackup\我的框架\AP\app\build.gradle">Open File</a>

解決方法:

1、你的專案中存在中文命名,將專案中對應的中文命名改為英文即可

(4)編譯時發生

Error:Execution failed for task ':app:mergeDebugResources'.
> D:\workspace5\Boke\app\src\main\res\drawable\custom_Animation_progress_bar.xml: Error: 'A' is not a valid file-based resource name character: File-based resource names must contain only lowercase a-z, 0-9, or underscore

解決方法:

1、你的專案中res\drawable下存在大寫字母,將專案中對應的大寫字母改為小寫字母即可

(5)在Aidl檔案進行編譯時發生

Error:Execution failed for task ':app:compileDebugAidl'.
> java.lang.RuntimeException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'E:\Eclipse\android-studio-sdk\android-sdk-windows\build-tools\23.0.2\aidl.exe'' finished with non-zero exit value 1

解決方法:

1、專案的編譯版本和編譯工具的版本不一致,右鍵專案 open Moudle Setting -> app -> 修改Compile Sdk Version 和Build Tools Version 版本一致

2、可能是你的複製進來的Aidl檔案的包名沒有修改成當前的包名

(6)在複製許可權內容,進行編譯時發生

Error:Execution failed for task ':yuyin:processDebugResources'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'E:\Eclipse\android-studio-sdk\android-sdk-windows\build-tools\23.0.2\aapt.exe'' finished with non-zero exit value 1

解決方法:

1、就是xml 可能出現無法識別的資原始檔錯誤, 就是資原始檔 裡面出錯了

2、有時候在uses-permission中多了個空格也會報這個錯誤

(7)run程式時發生

Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.

解決方法:

1、勾選中下圖選項即可



(8)在配置NDK後編譯時發生

Error:Execution failed for task ':app:compileDebugNdk'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'E:\Eclipse\android-studio-sdk\android-sdk-windows\ndk-bundle\ndk-build.cmd'' finished with non-zero exit value 2
解決方法:

1、編譯NDK時最好不要使用SDKManager裡下載的ndk-bundle,否則會報這些錯誤

2、官網下載NDK開發包,r9、r10系列,然後替換掉久的ndk就可以了

(9)在Android Studio->run的時候發生

Unable to create Debug Bridge: Unable to start adb server: error: could not install *smartsocket* listener: cannot bind to 127.0.0.1:5037: 通常每個套接字地址(協議/網路地址/埠)只允許使用一次。 (10048)
could not read ok from ADB Server
* failed to start daemon *
error: cannot connect to daemon
'E:\Eclipse\android-studio-sdk\android-sdk-windows\platform-tools\adb.exe,start-server' failed -- run manually if necessary

解決方法:

1、網上的方案,親測不是這個問題

根據提示檢視adb的埠號5037被誰佔用,通過 netstat -aon|findstr “5037” 找出相應的pid號,再通過工作管理員找到相應pid號的程序,然後將其結束,最後重啟adb

2、將原本選擇的預設adb換成我們自己的sdk裡面的adb,親測成功了

(10)建立了兩個Module,其中一個Module依賴另一個Module而導致了出現該問題,如果在Android Studio中,有ModuleA和ModuleB,我們希望ModuleA依賴ModuleB,執行時候可能會出現該問題

Error:Dependency Boke:zXingProj:unspecified on project app3 resolves to an APK archive which is not supported as a compilation dependency. File: D:\workspace5\Boke\zXingProj\build\outputs\apk\zXingProj-release-unsigned.apk

解決方法:

1、檢視被依賴的ModuleB的build.gradle,裡面可以看到

apply plugin: 'com.android.application'  
將這一句改為即可
apply plugin: 'com.android.library'  
接著會出現另一個錯誤
Error:Library projects cannot set applicationId. applicationId is set to 'com.dtr.zxing' in default config.

解決方法:

1、需要將builde.gradle — android — defaultConfig中的applicationId刪除

(11)這個問題是我在修改Bmob的最新jar包的時候發生的錯誤,從3.4.6換到3.5.0時,編譯出錯

Error:Execution failed for task ':app:transformClassesWithDexForRelease'. > com.android.build.api.tr

解決方案:

1、jar包衝突(finished with non-zero exit value 2) 
主要表現為編譯後出現finished with non-zero exit value 2錯誤,原因是jar包衝突,導致的原因可能是在dependencies中使用compile files()匯入一次jar包,然後有其它jar的引入方式使用compile’com.xxx’方式,正好又引用了這個jar包,所以導致了重複引用jar包的衝突。最常見的是support-v4包的重複引用

(12)當在gradle匯入第三方依賴(sweetalert)之後,編譯出錯

Error:Execution failed for task ':qq:processDebugManifest'.
> Manifest merger failed : Attribute [email protected] value=(@mipmap/ic_launcher) from AndroidManifest.xml:45:9-43
	is also present at [com.pnikosis:materialish-progress:1.0] AndroidManifest.xml:13:9-45 value=(@drawable/ic_launcher).
	Suggestion: add 'tools:replace="android:icon"' to <application> element at AndroidManifest.xml:42:5-122:19 to override.

解決方案:

1、由於匯入的第三方依賴也使用我們application的icon值的屬性,具體修改步驟如下圖,加上下面圈起來的語句


(13)Git Push 提交到Github上發生錯誤

$ git push origin master
To https://github.com/AndroidHensen/NewsTemplate.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/AndroidHensen/NewsTemplate.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解決方案:

1、出現的主要原因是,本地沒有update到最新版本的專案(git上有README.md檔案沒下載下來)

2、先進行程式碼合併,git pull --rebase origin master,再進行程式碼提交,git push -f origin master

(14)升級完Android Studio2.3後,打包release出現的錯誤

Error:Execution failed for task ':qq:lintVitalRelease'.
> Lint found fatal errors while assembling a release target.
To proceed, either fix the issues identified by lint, or modify your build script as follows:
...
android {
    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }
}
...
解決方法:

在app的build.gradle中新增如下程式碼,重新Generate Signed APK即可

android{
    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }
}