Android 3.6多渠道打包遇到的坑,你遇到了吗?
2020-04-02 本文已影响0人
逍遥散人_095
报错如下:
Caused by: com.android.build.gradle.internal.crash.ExternalApiUsageException: groovy.lang.GroovyRuntimeException: Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=xiaomiDebug, filters=[], versionCode=1, versionName=1.0}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
报错截图:
error.png
大家可以注意看一下,AS升级到3.0以上版本后,截图上的红框处的代码都要改动,否则是无法正常打包的。那要改成什么样呢,如下图所示:
success.png
代码如下:
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.sprsoft.history"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
flavorDimensions "default"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFile
def fileName
if (outputFile != null && outputFile.name.endsWith('.apk')) {
if (variant.buildType.name.equals('release')) {//如果是release包
fileName = "cbx_release_v${defaultConfig.versionName}.apk"
} else if (variant.buildType.name.equals('debug')) {//如果是debug包
fileName = "cbx_debug_v${defaultConfig.versionName}.apk"
}
outputFileName = fileName
}
}
}
}
productFlavors {
wangdoujia {}
xiaomi {}
yingyongbao {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL: name]
}
lintOptions {
checkReleaseBuilds false
abortOnError false
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
补充:
还需要在AndroidManifest.xml文件中添加:“<meta-data />”标签代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sprsoft.history">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL}" />
</application>
</manifest>