Gradle常见问题

All flavors must now belong to a named flavor dimension

指定配置一个默认的构建渠道

android {
...
flavorDimensions "默认的渠道"

}

Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated...

将gradle的 tools.build classpath 'com.android.tools.build:gradle:3.3.0' 由 2.3.3 升级为3.3.0 ,gradle版本升级为4.6

  • 首先compile需要替换为 implementation或者api

  • output.outputFile 替换为 outputFileName
    img

  • 渠道最好有默认的配置

 productFlavors.all { flavor ->
        flavor.resValue("string", "flavor_name", name)
        switch (name) {
        case "dd":
        //do something
            break;
            ...
        default:
            //do something
        break
        }
        }

ndk 配置错误

no toolchains found in the ndk toolchains folder for abi with prefix: mips64el-linux-android

  • 首先检查gradle配置,加载熟悉的gradle版本 防止踩坑,如4.6.0

  • 检查sdk配置,必要时重新下载或者清除缓存重新加载
    img

  • 检查项目的sdk配置(local.properties 文件)

ndk.dir=/home/misty/Android/Sdk/ndk-bundle

sdk.dir=/home/misty/Android/Sdk

下载依赖库很慢-使用阿里云的数据源

buildscriptallprojects 节点下添加如下配置

repositories {
       // jcenter()
       // google()
        maven { url'http://maven.aliyun.com/nexus/content/repositories/google' }

        maven { url'http://maven.aliyun.com/nexus/content/groups/public/' }

        maven { url'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
    }

贴一段比较常用的配置

local.properties

ndk.dir=/home/misty/Android/Sdk/ndk-bundle
sdk.dir=/home/misty/Android/Sdk
keyAlias=misty
keyPassword=123456
storePassword=123456

project buildscript

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        maven { url "https://jitpack.io" }
        maven{
            url 'https://dl.google.com/dl/android/maven2/'
            name 'Google'
        }
        google()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
    }
}

allprojects {
    repositories {
        mavenCentral()
        jcenter()
        maven { url "https://jitpack.io" }
        maven{
            url 'https://dl.google.com/dl/android/maven2/'
            name 'Google'
        }
        google()

    }

}

task clean(type: Delete) {
    delete rootProject.buildDir
}

module script

apply plugin: 'com.android.application'

//加载配置文件
Properties local_properties = new Properties()
local_properties.load(project.rootProject.file('local.properties').newDataInputStream())

android {
    compileSdkVersion 25
    buildToolsVersion '28.0.3'
    useLibrary 'org.apache.http.legacy'
    flavorDimensions "dd"
    defaultConfig {
        applicationId "com.misty"
        minSdkVersion 19
        targetSdkVersion 21
        //23 有动态权限
        versionCode 5
        //每次只升高最后一个值即可
        versionName '10.0.0.1'
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    repositories { flatDir { dirs 'libs' } }

    packagingOptions {
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'proguard.cfg'
        exclude 'proguard-project.txt'
        exclude 'project.properties'
        exclude 'META-INF/LICENSE'
    }
    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }

    //签名配置,配置多个签名
   signingConfigs {
        debug {
            def storeFilePath = local_properties.getProperty("storeFile")
            storeFile(storeFilePath != null ? file(storeFilePath) : null)
            storePassword local_properties.getProperty("storePassword")
            keyPassword local_properties.getProperty("keyPassword")
            keyAlias local_properties.getProperty("keyAlias")
        }
        release {
            def storeFilePath = local_properties.getProperty("storeFile")
            storeFile(storeFilePath != null ? file(storeFilePath) : null)
            storePassword local_properties.getProperty("storePassword")
            keyPassword local_properties.getProperty("keyPassword")
            keyAlias local_properties.getProperty("keyAlias")
        }
    }
    //配置编译的jdk版本
    compileOptions {
        sourceCompatibility org.gradle.api.JavaVersion.VERSION_1_7
        targetCompatibility org.gradle.api.JavaVersion.VERSION_1_7
    }
    dexOptions {
        jumboMode true
        javaMaxHeapSize "4g"
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
            zipAlignEnabled false                            //Zipalign优化
            shrinkResources false                            //移除无用的resource文件
            minifyEnabled false //是否混淆
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
            debuggable true
        }

    }

    productFlavors {

        dd {}
        //jpz{}

    }
    // 批量配置渠道
    productFlavors.all { flavor ->
        flavor.resValue("string", "oem_name", name)
        switch (name) {
                case "dd":
                //正式服务器
                flavor.buildConfigField("String", "HOST_ROOT", "\"http://imisty.cn/\"")
                flavor.buildConfigField("String", "SLAVE_HOST", "\"http://imisty.cn/\"")

                flavor.buildConfigField("String", "HOST_NETTY", "\"http://imisty.cn/\"")
                flavor.buildConfigField("String", "BUCKET", "\"oss-misty\"")
                flavor.buildConfigField("String", "REGONHOST", "\"https://oss-cn-shenzhen.aliyuncs.com\"")
                flavor.resValue("string", "bucket", "oss-misty")
                flavor.resValue("string", "regonhost", "https://oss-cn-shenzhen.aliyuncs.com")
                break
                //国外
            case "jpz":
                flavor.buildConfigField("String", "HOST_ROOT", "\"http://imisty.cn/\"")
                flavor.buildConfigField("String", "SLAVE_HOST", "\"http://imisty.cn/\"")

                flavor.buildConfigField("String", "HOST_NETTY", "\"3http://imisty.cn/\"")
                flavor.buildConfigField("String", "BUCKET", "\"oss-misty\"")
                flavor.buildConfigField("String", "REGONHOST", "\"https://oss-cn-hongkong.aliyuncs.com\"")
                flavor.resValue("string", "bucket", "oss-misty")
                flavor.resValue("string", "regonhost", "https://oss-cn-hongkong.aliyuncs.com")
                break

            default :
                //正式服务器
                flavor.buildConfigField("String", "HOST_ROOT", "\"http://imisty.cn/\"")
                flavor.buildConfigField("String", "SLAVE_HOST", "\"http://imisty.cn/\"")

                flavor.buildConfigField("String", "HOST_NETTY", "\"http://imisty.cn/\"")
                flavor.buildConfigField("String", "BUCKET", "\"oss-misty\"")
                flavor.buildConfigField("String", "REGONHOST", "\"https://oss-cn-shenzhen.aliyuncs.com\"")
                flavor.resValue("string", "bucket", "oss-misty")
                flavor.resValue("string", "regonhost", "https://oss-cn-shenzhen.aliyuncs.com")
                break
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.all {
            def fileName
            if (variant.buildType.name == buildTypes.release.name) {
                fileName = "${variant.productFlavors[0].name}@v${defaultConfig.versionName.split("\\.")[3]}.apk"
            } else {
                fileName = "${variant.productFlavors[0].name}@v${defaultConfig.versionName.split("\\.")[3]}_debug.apk"
            }
            outputFileName =  fileName
        }
    }

}

//获取时间戳
def packageTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("GMT+8"))
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation(name: 'shadowsockss', ext: 'aar')
    //    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    //        exclude group: 'com.android.support', module: 'support-annotations'
    //    })
    implementation 'com.tencent.bugly:crashreport:latest.release'
    implementation 'com.alibaba:fastjson:1.1.68.android'
    implementation 'com.aliyun.dpa:oss-android-sdk:+'
    implementation files('libs/netty-all-4.1.5.Final.jar')
    implementation files('libs/android-support-v4.jar')
    // compile 'cn.bingerz.android:fastlocation:1.1.1'
    //compile files('libs/android-common.jar')

    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    implementation 'com.squareup.okio:okio:2.1.0'
    implementation 'com.luffykou:android-common-utils:1.1.3'
    implementation 'eu.chainfire:libsuperuser:1.0.0.+'
    implementation 'com.google.zxing:core:3.3.2'
    implementation 'com.google.zxing:android-core:3.3.0'
    implementation 'com.google.zxing:javase:3.3.2'
    implementation 'com.github.kenglxn.QRGen:android:2.4.0'
    /*compile 'com.h2database:h2-mvstore:1.4.196'*/
    //    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.4'
    //    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
    implementation 'com.github.zcweng:switch-button:0.0.3@aar'
    implementation 'com.amap.api:location:latest.integration'
    implementation 'org.litepal.android:core:2.0.0'
    implementation files('libs/jackson-core-2.8.11.jar')
    implementation files('libs/fbprotocol.jar')
    implementation 'org.bouncycastle:bcprov-jdk15on:1.57'
}