1.更新后Gradle Plugin 3.0 遇到的问题
问题
Error:(101, 0) Cannot set the value of read-only property 'outputFile' for A ...
解决
升级为 gradle plugin 3.0 后,打包的文件输出就出了问题:
原始为:
//配置apk路径
applicationVariants.all { variant ->
variant.outputs.each { output ->
if (variant.buildType.name == 'release') {
variant.mergedFlavor.versionCode = getVersionCode(false)
variant.mergedFlavor.versionName = getVersionName(false)
// release
def apkName = "${project.getName()}_${variant.flavorName}_${buildType.name}_v${variant.versionCode}.apk"
output.outputFile = new File(output.outputFile.parent, apkName)
} else {
variant.mergedFlavor.versionCode = getVersionCode(true)
variant.mergedFlavor.versionName = getVersionName(true)
// debug
def apkName = "${project.getName()}_${buildType.name}.apk"
output.outputFile = new File(output.outputFile.parent, apkName)
}
}
}
更新为:要输出的文件名称必须命名为 outputFileName
//配置apk路径
applicationVariants.all {
variant ->
def buildType = variant.buildType.name
if(buildType=='release'){
// variant.getPackageApplication().outputDirectory = new File(rootProject.rootDir,"releaseApk")
variant.getPackageApplication().outputDirectory = new File("F:/apk/frameDemoApk","releaseApk")
}else {
//debug版本
variant.getPackageApplication().outputDirectory = new File("F:/apk/frameDemoApk","debugApk")
}
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 输出apk名称为XXX20160328_v1.0.0_vc10_XXXX_test.apk
def fileName = "frame_${releaseTime()}_v${rootProject.ext.android.versionName}.apk"
// output.outputFile 改为 outputFileName
output.apkData.outputFileName = fileName
}
}
}
打包签名apk出错问题:
image.png
解决:
方法1:
点击Android Studio侧边栏的Gradle按钮,刷新下Gradle的配置.
image.png
方法2:
如果是编译好了APK名称冲突,可以先屏蔽 build配置的打包文件输出.如上配置的apk输出路径.
方法3:
Android Studio 3.0会在debug apk的manifest文件application标签里自动添加 android:testOnly="true"属性,导致apk手机上甚至安装不了.
解决方法:
在gradle.properties(项目根目录或者gradle全局配置目录 ~/.gradle/)文件中添加android.injected.testOnly=false
gradle中用的一些路径环境变量
新建一个工程名为InspectGradleDir,路径为
D:\study\android\projects\InspectGradleDir
那么rootDir对应
D:\study\android\projects\InspectGradleDir
projectDir对应
D:\study\android\projects\InspectGradleDir\app
buildDir对应
D:\study\android\projects\InspectGradleDir\app\build
2. 出现 AAPT2 错误
Error:(1875) resource color/blue (aka com.smartahc.android.insurance.beta:color/blue) not found.
Error:(1880) resource color/white (aka com.smartahc.android.insurance.beta:color/white) not found.
Error:(1882) resource color/white (aka com.smartahc.android.insurance.beta:color/white) not found.
Error:(1885) resource color/white (aka com.smartahc.android.insurance.beta:color/white) not found.
Error:failed linking references.
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 ':eartag_main:processBetaReleaseResources'.
> Failed to execute aapt
解决
gradle.properties
中添加下面内容
#Tue Oct 17 10:08:09 CST 2017
android.enableAapt2=false
3. flavor dimension 配置问题
Error:A problem occurred configuring project ':eartag_main'.
> All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html
解决
android{
...
defaultconfig{
...
...
flavorDimensions "tier"
}
...
productFlavors {
beta {
dimension "tier"
...
]
}
production {
dimension "tier"
....
]
}
}
}
4. Unable to resolve dependency for compileClasspath
类似于下面错误
Unable to resolve dependency for ':eartag_main@betaProductionPyger/compileClasspath': Could not resolve project :eartag_base. Open File Show Details
解决
library
库依赖的时候,需要注意 build variant
使用方式:
- 当 library 为 debug 的时候,使用 debugImplementation 引入;
- 当 library 为 release 的时候,使用 implementation 引入;
5.依赖改变
-
implementation
: 依赖 libs 和 第三方库 -
debugImplementation
: 依赖 debug 版本 -
testImplementation
: 依赖测试库 -
androidTestImplementation
: 依赖 andrioid 测试库 -
api
: 以前的 complie 用法;
implementation project(':eartag_base')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
重点
看看 api 与 implementation 的使用对比,不然在项目中依赖 module 后,怎么都找不到 libs 中依赖的内容:
image.png最后保持 build variants
一直就行了
5.kotlin 配置
项目使用 databinding
了,然后更新后就不能用了,使用 kapt
进行引入 databinding
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
...
dependencies {
// kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation libs.pyger
kapt libs.databinding
// implementation smartahc.coreui
implementation project(':eartag_base')
}
6.aapt 依赖库重复问题
Error:org.gradle.process.internal.ExecException: Process 'command '/Users/yuan/Library/Android/sdk/build-tools/26.0.2/aapt'' finished with non-zero exit value 1
如果自己配置 android 相关的库,就会出现重复依赖的情况,看这个是否重复依赖了相关库,如果自己使用第三方的已经配置了,那么久不需要自己进行配置了,或者自己配置成相同的版本;
image.png
网友评论