AndroidStudio3.4.1+Gradle5.1.1+productFlavors声明+gradlew编译,多渠道多任务打包,适合小白快速构建Android gradle工程,快速拉取后直接编译即可,可套用自身新项目直接构建 ,github地址:《AndroidStudioGradle》
如果新项目的同学无法编译过的,可以采用提交的快速编译模板,带多渠道多任务打包task。
每次升级总会报几个错误,现在都很害怕gradle和Androidstudio了,加上各个远程引用,一大堆错误在所难免,只能奉劝各位,别太早去更新…
-
题外话:如果引发AAPT2 的错误,请在gradle.properties中关闭APPT2 编译。
android.enableAapt2=false
-
此次更新 编译是 AS3.4.2 + Gradle 5.1.1 的环境,引发了几个错误:
-
classpath 'com.android.tools.build:gradle:3.4.1'
-
ERROR: All flavors must now belong to a named flavor dimension.
-
Gradle sync failed: Could not find method buildType() for arguments[] on ApkVariantOutputImpl_Decorated
其实大部分升级 AndroidStudio3.x 之后出现的错误都是由 productFlavors 引发的,除了buildType ,因为flavors 要保持统一性,所以buildType构建的参数就要使用子的,可能有的还会遇到以下这种错误
Error:All flavors must now belong to a named flavor dimension. The flavor 'flavor_name' is not assigned to a flavor dimension.
先来看看谷歌的解释:
提示:插件会尝试匹配应用与本地库依赖项的变体。由于 variant-aware
依赖项匹配取决于您命名风格维度的方式,因此请谨慎命名您的风格维度。这样可让您加强对本地依赖项中与各版本应用匹配的代码和资源的控制。
以下代码示例将创建名为“version”的风格维度,并添加“demo”和“full”产品风格。这些风格可自行提供其 applicationIdSuffix 和 versionNameSuffix:
android { ... defaultConfig {...} buildTypes { debug{...} release{...} } // Specifies one flavor dimension. flavorDimensions "version" productFlavors { demo { // Assigns this product flavor to the "version" flavor dimension. // This property is optional if you are using only one dimension. dimension "version" applicationIdSuffix ".demo" versionNameSuffix "-demo" } full { dimension "version" applicationIdSuffix ".full" versionNameSuffix "-full" } } }
上述的任务报错后,在多任务的情况下,还会引发这种错误,
Error: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.
种种原因,只是你没有发现flavorDimensions和productFlavors 的关系,从官方的配置来看,只要按照这个配置,基本都能编译过去了。
android { ... buildTypes { debug {...} release {...} } // Specifies the flavor dimensions you want to use. The order in which you // list each dimension determines its priority, from highest to lowest, // when Gradle merges variant sources and configurations. You must assign // each product flavor you configure to one of the flavor dimensions. flavorDimensions "api", "mode" productFlavors { demo { // Assigns this product flavor to the "mode" flavor dimension. dimension "mode" ... } full { dimension "mode" ... } // Configurations in the "api" product flavors override those in "mode" // flavors and the defaultConfig block. Gradle determines the priority // between flavor dimensions based on the order in which they appear next // to the flavorDimensions property above--the first dimension has a higher // priority than the second, and so on. minApi24 { dimension "api" minSdkVersion 24 // To ensure the target device receives the version of the app with // the highest compatible API level, assign version codes in increasing // value with API level. To learn more about assigning version codes to // support app updates and uploading to Google Play, read Multiple APK Support versionCode 30000 + android.defaultConfig.versionCode versionNameSuffix "-minApi24" ... } minApi23 { dimension "api" minSdkVersion 23 versionCode 20000 + android.defaultConfig.versionCode versionNameSuffix "-minApi23" ... } minApi21 { dimension "api" minSdkVersion 21 versionCode 10000 + android.defaultConfig.versionCode versionNameSuffix "-minApi21" ... } } } ...
构建变体:[minApi24, minApi23, minApi21][Demo, Full][Debug, Release]
对应 APK:app-[minApi24, minApi23, minApi21]-[demo, full]-[debug, release].apk
变体配置:
android { ... buildTypes {...} flavorDimensions "api", "mode" productFlavors { demo {...} full {...} minApi24 {...} minApi23 {...} minApi21 {...} } variantFilter { variant -> def names = variant.flavors*.name // To check for a certain build type, use variant.buildType.name == "<buildType>" if (names.contains("minApi21") && names.contains("demo")) { // Gradle ignores any variants that satisfy the conditions above. setIgnore(true) } } } ...
编译通过:
- 此次更新 编译是 AS3.4.2 + Gradle 5.1.1+ proguard-gradle6.0.3 的环境,引发了几个错误:
- Gradle DSL method not found: 'abiFilter()'
- The project 'MyApplication' may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0).
- The project 'MyApplication' may be using a version of Gradle that does not contain the method.
- The build file may be missing a Gradle plugin.
- Could not find build of variant which supports density 320 and an ABI in arm64-v8a, armeabi-v7a, armeabi
- Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
其实主要还是手机的cpu引发的,修改Gradle后也就是NDK上的写法有问题,可能以前的也就设置一下就可以了。
abiFilter("arm64-v8a", "armeabi-v7a", "armeabi")
首先打开tools,把NDK升级一下,然后参考一下写法。
defaultConfig中 ndk{ moduleName "app" abiFilters 'armeabi',"x86" }
此篇文章会持续更新AS的错误,后续持续补充……
欢迎各位投稿补充 疑难杂症 篇。