天天看點

将舊項目遷移到AndroidStudio3.0

1.aapt2編譯不過去

Error:Execution failed for task ':app:mergeDebugResources'.
> Error: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
           

原來的項目用到了kotlin以及kapt,更新到了androidstudio 3.0報了上面的錯誤,需要在項目的gradle.properties中添加:

android.enableAapt2=false
           

關閉aapt2。

2.All flavors must now belong to a named flavor dimension

All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

官方文檔:

Plugin 3.0.0 includes a new dependency mechanism that automatically matches variants when consuming a library. This means an app's debug variant automatically consumes a library's debug variant, and so on. It also works when using flavors—an app's redDebug variant will consume a library's redDebug variant. To make this work, the plugin now requires that all flavors belong to a named flavor dimension —even if you intend to use only a single dimension. Otherwise, you will get the following build error:

Android Plugin3.0的依賴機制:在使用library時會自動比對variant(debug, release),就是說app的debug會自動比對library的debug,相信大多數人也像我一樣,當library多了,不會手動選擇每個Library的variant。現在好了,它會自動比對了。同樣如果使用flavor的時候,比如app的redDebug同樣會自動比對library的readDebug。雖然有這樣的優勢,但是在使用flavor時,必須定義flavor dimension,否則會提示錯誤:

Error:All flavors must now belong to a named flavor dimension.
The flavor 'flavor_name' is not assigned to a flavor dimension.
           

現在使用flavor,必須像下面一樣配置:

// Specifies a flavor dimension.
flavorDimensions "color"

productFlavors {
     red {
      // Assigns this product flavor to the 'color' flavor dimension.
      // This step is optional if you are using only one dimension.
      dimension "color"
      ...
    }

    blue {
      dimension "color"
      ...
    }
}
           
  • 注意:如果library有兩個dimensions:color,shape,但是app隻有color,那麼會如下的編譯錯誤:
Error:Could not resolve all dependencies for configuration ':bar:redDebugCompileClasspath'.
Cannot choose between the following configurations on project :foo:
  - redCircleDebugApiElements
  - redSquareDebugApiElements
  ...
           
  • 在APP使用flavorSelection標明使用某個flavor dimension,注意如下配置:
android {
  ...
  // The flavorSelection property uses the following format:
  // flavorSelection 'dimension_name', 'flavor_name'

  // Chooses the 'square' flavor from libraries that specify a 'shape'
  // dimension.
  flavorSelection 'shape', 'square'
}
           
參考文章

3.Cannot set the value of read-only property 'outputFile’….

Android plugin 3.0 migration guide

建議:

  • 使用 all()代替 each()
  • 使用 outputFileName代替 output.outputFile,如果你隻想更改檔案名稱
// If you use each() to iterate through the variant objects,
// you need to start using all(). That's because each() iterates
// through only the objects that already exist during configuration time—
// but those object don't exist at configuration time with the new model.
// However, all() adapts to the new model by picking up object as they are
// added during execution.
android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}-${variant.versionName}.apk"
    }
}
           

參考如下:

applicationVariants.all { variant ->
    variant.outputs.all { output ->
        def newApkName = applicationId + "-" + variant.versionName + "(" + variant.versionCode + ")" + ".apk";
        outputFileName = new File("${project.projectDir}/../outputs/apks/" + variant.name, newApkName);
    }
}
           

3.You should manually set the same version via DependencyResolution

另一種姿勢:

configurations.all {  
    resolutionStrategy.force 'rubygems:rb-inotify:0.9.5'  
}