eclipse轉Android studio遇到的那些坑


       公司項目有導入10多個libray,還有涉及ndk,轉Android studio時碰到不少問題。前后大概花費5個工作日,中間各種奇葩bug,各種編譯出錯,非常多還有沒錯誤提示。一度想過放棄,如不是沒有選擇,可能真要放棄了。最后成功轉型還是非常值得的。現記錄下遇到的問題,給有須要的人。
1.

解決:
在manifest里面,某個activity下多寫了一句intent-filter,里面沒有內容,刪了之后就沒錯誤了。


2.

錯誤例如以下:
解決:

使用NDK時。NDK not configured

http://stackoverflow.com/questions/20674650/how-to-configure-ndk-with-android-gradle-plugin-0-7
在NDK libproject下的 build.gradle中加入
1
2
3
4
5
6
7
8
9
10
11
12
productFlavors {
        arm {
            ndk {
            abiFilters "armeabi" , "armeabi-v7a"
            }
        }
        x86 {
            ndk {
            abiFilter "x86"
            }
        }
        }

或者

1
2
3
4
5
6
7
buildTypes {
      debug {
      ndk {
          abiFilters "armeabi" , "armeabi-v7a"
      }
      }
 }

3.

File > Settings > Build, Execution, Deployment > Compiler

and see "Command-line-options" and check if anything is give. (In your case -x). If so remove it and click Apply and Ok. Now restart your android studio and try building
解決:
配置錯誤把這里的配置刪掉

4.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_51\bin\java.exe'' finished with non-zero exit value 1
解決:

電腦配置可能不夠用,所以開始不斷嘗試改動占用內存相關的東西。並最終取得了進展。關鍵代碼是build.gradle中的:

dexOptions {
    javaMaxHeapSize "xx"
}
xx為合理的內存大小(如4g)。假設你也遇到了這樣的問題,最好還是試試~

5.
Error:(24, 1) A problem occurred evaluating root project 'MyApplication2'.
> Could not find method dexOptions() for arguments [build_6h7x2ds382w4ozyuts7q05l4x$_run_closure3@2e96270a] on root project 'MyApplication2'.
解決:
不能寫成一行
dexOptions { javaMaxHeapSize "xx" }

6.


7.

解決:
改成相相應的版本  compile 'com.android.support:appcompat-v7:19.+'


8.
Error:Error: File path too long on Windows, keep below 240 characters : C:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\unspecified\res\drawable-xhdpi-v4\battle_navigation_button_signout_default.png
解決:
把文件甲路徑縮短


9.
Error:Execution failed for task ':wonderDroid:processDebugAndroidTestManifest'.
> java.lang.RuntimeException: Manifest merger failed : Attribute application@theme value=(@style/Theme.Sherlock) from [debug] AndroidManifest.xml:22:9-46
is also present at [branch_20151216_1.5.0_01_copy:emuUtils:unspecified] AndroidManifest.xml:15:9-40 value=(@style/AppTheme).
Suggestion: add 'tools:replace="android:theme"' to <application> element at manifestMerger3943799027194821591.xml:7:5-9:19 to override.

解決方式:
在Manifest.xml的application標簽下加入tools:replace="android:icon, android:theme"(多個屬性用,隔開,而且記住在manifest根標簽上加入xmlns:tools="http://schemas.android.com/tools",否則會找不到namespace哦)

Suggestion: add 'tools:replace="android:theme"' to <activity> element at AndroidManifest.xml:1120:9-1128:20 to override.
這個錯誤就是<activity>合並時沖突了。和 application一樣,依據沖突提示加入就可以,如:
<activity
   
tools :replace= "android:configChanges"
   
android :name= "xxxxx"
   
android :configChanges= "mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|fontScale|uiMode|screenSize|smallestScreenSize"
   
android :label= "@string/N64_Name"
   
android :process= ":game"
   
android :theme= "@style/appTheme.Black" >
</activity>

10.
AAPT err(Facade for 1813194376): libpng error: Not a PNG file
Error:Execution failed for task ':xxxx:mergeDebugResources'.
> Some file crunching failed, see logs for details
原因是jpg格式的圖片后綴為png,AS校驗嚴格,eclipse不校驗
使用美圖工具將圖片轉為png圖片



11.
Error:Execution failed for task ':xxx:processDebugManifest'.
> F:\xxx\src\main\AndroidManifest.xml:329:9-335:20: Error: Invalid instruction 'targetApi', valid instructions are : REMOVE,REPLACE,STRICT

解決:
<activity
   
android :name= "xxxx"
   
android :configChanges= "mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|fontScale|uiMode|screenSize|smallestScreenSize"
   
android :label= "@string/app_name"
   
android :theme= "@android:style/Theme.Holo.Light.Dialog"
   
tools :targetApi= "11" >
</activity>
把Manifest中tools:targetApi="11"去掉




12.

把定義的接口類單獨提出來,不要寫在類里面,不然會以為循環繼承

13.
Error:Execution failed for task ':xxxx:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/maven/com.squareup.okio/okio/pom.xml
    File1: xxx\libs\com.umeng.message.lib_v2.3.0.jar
    File2: xxx\build\intermediates\exploded-aar\xxx\unspecified\jars\classes.jar
友盟的sdk和其它jar沖突
解決:
1)添加配置(好像沒用)
packagingOptions {
    exclude
'META-INF/NOTICE' // will not include NOTICE file
   
exclude 'META-INF/LICENSE' // will not include LICENSE file
}
2)更新友盟新的sdk

Error:Execution failed for task ':xxx:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/maven/com.squareup.okio/okio/pom.xml
    File1: C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\com.squareup.okio\okio\1.2.0\c0b52915a48fa91b1b94a28d4a2997bac5f524df\okio-1.2.0.jar
    File2: xxx\build\intermediates\exploded-aar\branch_20151216_1.5.0_01_copy\emuUtils\unspecified\jars\classes.jar
解決:配置
packagingOptions {
  exclude 'META-INF/maven/com.squareup.okio/okio/pom.xml'
exclude 'META-INF/maven/com.squareup.okio/okio/pom.properties'
}
友盟推送sdk使用了okio包,我的項目libray中也調用了。合並時沖突。

我友盟pushsdk中的okio包去掉。引用libray就可以。


14.
Error:Execution failed for task ':xxx: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 'C:\Program Files\Java\jdk1.8.0_73\bin\java.exe'' finished with non-zero exit value 2
解決:
主project和libray引用的包反復了,這個問題比較麻煩,因為項目中有10多個libray,要細致排插反復的jar。


15.
最終成功后,桌面出現多個圖標
解決:把每一個libray的配置MAIN刪掉
<intent-filter>         
    <action
android :name= "android.intent.action.MAIN" />
    <category
android :name= "android.intent.category.LAUNCHER" />           
</intent-filter>
16.
打包時出現的錯誤
Error:(2) Error: In Gradle projects, always use http://schemas.android.com/apk/res-auto for custom attributes [ResAuto]
解決:
<ScrollView xmlns: app = "http://schemas.android.com/apk/res/xx.xxx.xxx"
   xmlns:android="http://schemas.android.com/apk/res/android"
改為:
<ScrollView xmlns: app = "http://schemas.android.com/apk/res-auto"
   xmlns:android="http://schemas.android.com/apk/res/android"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM