1. 程式人生 > >【AndroidStudio】3.0升級gradle遇到的坑

【AndroidStudio】3.0升級gradle遇到的坑

升級了android studio3.0後,對於檔案的搜尋和方法中引數的使用,是一個重大的改變,但升級gradle後,缺出現很多的問題。個人整理一下,希望對大家有所幫助。

說明:我原來的環境是as2.3+gralde2.14.1+plugin2.2.3

升級後的環境是:as3.0+gradle4.2.1+plugin3.0.0

1、

Error:(82, 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.

<a href="openFile:G:\Work\Svn\BetaSVN\Tech\Beta.Android\Src\BCP\app\build.gradle">Open File</a>
解決:
修改app的檔案輸出格式
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'


            android.applicationVariants.all { variant ->
                variant.outputs.all {
                    outputFileName = "app_V${defaultConfig.versionName}.apk"

                }
//                variant.outputs.each { output ->
//                    def outputFile = output.outputFile
//                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
//                        //這裡修改apk檔名
//                        def fileName = "app_V${defaultConfig.versionName}.apk"

//                        output.outputFile = new File(outputFile.parent, fileName)
//                    }
//                }
            }


2、
Error:Failed to resolve: com.android.support:multidex:1.0.2
<a href="add.google.maven.repository">Add Google Maven repository and sync project</a><br><a href="openFile:G:/Work/Svn/BetaSVN/Tech/Beta.Android/Src/BCP/app/build.gradle">Open File</a><br><a href="open.dependency.in.project.structure">Show in Project Structure dialog</a>
解決:
修改build.gradle指令碼,增加google()


3、
Error:Failed to resolve: com.android.support:multidex-instrumentation:1.0.2
<a href="add.google.maven.repository">Add Google Maven repository and sync project</a><br><a href="openFile:G:/Work/Svn/BetaSVN/Tech/Beta.Android/Src/BCP/app/build.gradle">Open File</a><br><a href="open.dependency.in.project.structure">Show in Project Structure dialog</a>
解決:
同上


4、
Error:(604, 5) error: expected reference but got (raw string) @+android:style/Animation.Translucent.
解決:
沒有找到好的辦法,最後修改gradle的版本,最後使用4.2.1才解決。


5、
Error:com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
解決:
修改gradle.properties檔案,增加以下引數配置
android.enableAapt2=false


6、
Error:(23, 0) Cannot set the value of read-only property 'outputFile' for object of type com.android.build.gradle.internal.api.LibraryVariantOutputImpl.
<a href="openFile:G:\Work\Svn\BetaSVN\Tech\Beta.Android\Src\BAWNew\common\build.gradle">Open File</a>
解決:
修改辦法同第一個


7、
Error:Unable to load class 'org.gradle.api.internal.component.Usage'.
Possible causes for this unexpected error include:<ul><li>Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
<a href="syncProject">Re-download dependencies and sync project (requires network)</a></li><li>The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
<a href="stopGradleDaemons">Stop Gradle build processes (requires restart)</a></li><li>Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.</li></ul>In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
解決:
  a.修改gradle的版本
  b.增加google() 修改版本號
buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        ...
        classpath 'com.novoda:bintray-release:0.5.0'//修改此處版本號為 0.5.0---修改之前是0.3.4
        ...
    }
}


8、
Error:No such property: FOR_RUNTIME for class: org.gradle.api.attributes.Usage
解決:
將gradle的版本從4.3.1降級到4.2.1


9、
Error:Could not find com.android.tools.build:gradle:3.1.0.
Searched in the following locations:
    file:/C:/Program Files/Android/Android Studio3.0/gradle/m2repository/com/android/tools/build/gradle/3.1.0/gradle-3.1.0.pom
    file:/C:/Program Files/Android/Android Studio3.0/gradle/m2repository/com/android/tools/build/gradle/3.1.0/gradle-3.1.0.jar
    https://jcenter.bintray.com/com/android/tools/build/gradle/3.1.0/gradle-3.1.0.pom
    https://jcenter.bintray.com/com/android/tools/build/gradle/3.1.0/gradle-3.1.0.jar
    https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.1.0/gradle-3.1.0.pom
    https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.1.0/gradle-3.1.0.jar
Required by:
    project :
<a href="openFile:G:\Work\Svn\BetaSVN\Tech\Beta.Android\Src\BAWNew_bak\build.gradle">Open File</a>
解決:

沒有3.1.0的版本,修改為3.0.0的版本

10、Error:Unable to find method 'com.android.build.gradle.internal.variant.BaseVariantData.getOutputs()

解決:

buildscript {
    ext.kotlin_version = '1.1.2-3'
    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
將1.1.2-3’ -> ‘1.1.2-4’