1. 程式人生 > >Android studio 升級到3.0時專案遇到的問題

Android studio 升級到3.0時專案遇到的問題

第一個問題:

在專案的build.gradle檔案裡面

Cannot set the value of read-only property ‘outputFile’

Error:(56, 0) Cannot set the value of read-only property ‘outputFile’ for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.

之前的程式碼如下

applicationVariants.all { variant ->

          variant.outputs.each { output ->

             if (output.outputFile != null && output.outputFile.name.endsWith('.apk')

                 && 'release'.equals(variant.buildType.name)) {

                     def apkFile = new File(

                             output.outputFile.getParent(),

                     "zh_${variant.flavorName}_v${variant.versionName}_${buildTime()}.apk")

                     output.outputFile = apkFile

        }

    }

講之前的程式碼註釋修改成如下即可,貌似是命名改變了。

applicationVariants.all { variant ->

        /* variant.outputs.each { output ->

           if (output.outputFile != null && output.outputFile.name.endsWith('.apk')

                && 'release'.equals(variant.buildType.name)) {

                       def apkFile = new File(

                  output.outputFile.getParent(),

                "zhemituan_${variant.flavorName}_v${variant.versionName}_${buildTime()}.apk")

      output.outputFile = apkFile

   }

}*/

variant.outputs.all {

outputFileName ="zh_${defaultConfig.versionName}"+"_${variant.productFlavors[0].name}.apk"

    }

}

PS:另外就是記得在defaultConfig 下方加上這麼一句話

flavorDimensions"versionCode"

第二個問題是:

compile ‘com.android.support:multidex:1.0.1’

若專案裡面添加了此依賴,需要您將專案的build.gradle檔案進行修改,也就是配置倉庫的地方

allprojects {

    repositories {

                  jcenter()

                 maven { url"https://jitpack.io"}

                 google()// 加入此行程式碼---

         }

}

同時在module下的build.gradle處將之前的multidex的版本號升級到1.0.2

第三個問題:

Error:java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details

Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details

Error:com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details

Error:Execution failed for task ‘:app:processDebugResources’.

Failed to execute aapt

Information:BUILD FAILED in 8s

這個問題我也是看了別人的部落格才知道如何解決;

在專案的gradle.properties檔案中底部加入

android.enableAapt2=false

第四個問題:

Annotation processors must be explicitly declared now. The following dependenciesonthecompile classpath are foundtocontainannotation processor. Please add themtotheannotationProcessor configuration. - butterknife-7.0.1.jar Alternatively,setandroid.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath =truetocontinuewithprevious behavior. Notethatthis optionisdeprecatedandwill be removedinthefuture.

使用了黃油刀出現的問題,compile'com.jakewharton:butterknife:7.0.1'

在module下的build.gradle中新增如下配置即可

android {

...defaultConfig {

...

           javaCompileOptions {

         annotationProcessorOptions {

          includeCompileClasspath = true }

          }

    }

}

第五個問題:

升級gradle了,之前專案用的是25.0.0的,導致雖然沒有報錯,但是有warning

,所以需要將專案裡面所有使用到的改成26+

第六個問題:

找不到這個類support.v4.animation.AnimatorCompatHelper;

這個應該不是升級導致的,可能是因為專案中依賴導致的,姑且暫時記錄在這裡,因為我是升級之後出現了這個問題,具體解決辦法就是

在build.gradle最後新增,程式碼如下:

configurations.all {

     resolutionStrategy.eachDependency {

      DependencyResolveDetails details ->

     def requested = details.requested

              if (requested.group == 'com.android.support') {

                    if (!requested.name.startsWith("multidex")) {

                            details.useVersion '24.1.1'

                               }

                   }

           }

}

PS:上述問題解決方案,基本上都是看了其他人寫的文章才知道的,謝謝無私者的奉獻。