【问题标题】:could not get unknown property for 'applicationVariants' for BuildType_Decorated无法为 BuildType_Decorated 获取“applicationVariants”的未知属性
【发布时间】:2017-12-19 10:58:04
【问题描述】:

我已经在我的项目中导入了模块并且正确导入了它。但我收到了这个错误。我已经导入了扫描仪控制应用程序模块(斑马)。我已经搜索了许多替代解决方案,但都没有成功。

Error:(36, 0) Could not get unknown property 'applicationVariants' for BuildType_Decorated{name=release, debuggable=false, testCoverageEnabled=false, jniDebuggable=false, pseudoLocalesEnabled=false, renderscriptDebuggable=false, renderscriptOptimLevel=3, minifyEnabled=false, zipAlignEnabled=true, signingConfig=null, embedMicroApp=true, mBuildConfigFields={}, mResValues={}, mProguardFiles=[/home/custom/AndroidStudioProjects/fireball5/fireball/build/intermediates/proguard-files/proguard-android.txt-2.3.3, /home/custom/AndroidStudioProjects/fireball5/fireball/scannercontrol/proguard-rules.pro], mConsumerProguardFiles=[], mManifestPlaceholders={}} of type com.android.build.gradle.internal.dsl.BuildType.
<a href="openFile:/home/custom/AndroidStudioProjects/fireball5/fireball/scannercontrol/build.gradle">Open File</a>
    apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.0'

    defaultConfig {
        applicationId "com.zebra.scannercontrol.app"
        minSdkVersion 19
        targetSdkVersion 22
        versionCode 71
        versionName "2.0.8.0"
        if (project.hasProperty('ADD_BUILD_TO_VERSION')) {
            versionName = versionName.substring(0,versionName.lastIndexOf(".") + 1) + (System.getenv("BUILD_NUMBER") ?: "0")
        }
    }

    signingConfigs {
        release {
            storeFile file("../../../keys/AndroidScannerSDK.keystore")
            if (project.hasProperty('RELEASE_STORE_PASSWORD')) {
                storePassword RELEASE_STORE_PASSWORD
                keyAlias RELEASE_KEY_ALIAS
                keyPassword RELEASE_KEY_PASSWORD
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            if (project.hasProperty('RELEASE_STORE_PASSWORD')) {
                signingConfig signingConfigs.release
            }
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationVariants.all { variant ->
                appendVersionName(variant, defaultConfig)
            }
        }
    }

    lintOptions {
        // Don't abort if Lint finds an error, otherwise the Jenkins build
        // will be marked as failed, and Jenkins won't analyse the Lint output
        abortOnError false
    }
}
def appendVersionName(variant, defaultConfig) {
    variant.outputs.each { output ->
        if (output.zipAlign) {
            def file = output.outputFile
            def fileName = file.name.replace("scannercontrol-release.apk", "scanner_control_app_v" + defaultConfig.versionName  + ".apk")
            output.outputFile = new File(file.parent, fileName)
        }

        def file = output.packageApplication.outputFile
        def fileName = file.name.replace("scannercontrol-release", "scanner_control_app_v" + defaultConfig.versionName + ".apk")
        output.packageApplication.outputFile = new File(file.parent, fileName)
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.android.support:design:25.0.0'
    compile project(':BarcodeScannerLibrary')
}
enter code here

【问题讨论】:

    标签: android android-studio dependencies build.gradle


    【解决方案1】:

    applicationVariants.all 出现错误。

    buildTypes {
        release {
            applicationVariants.all { variant ->
                appendVersionName(variant, defaultConfig)
            }
        }
    }
    

    修复 1:

    这将不起作用,因为您正在申请 apply plugin: 'com.android.library'

    您必须将其更改为 apply plugin: 'com.android.application' 才能使用 applicationVariants.all

    修复 2:

    如果您想继续使用apply plugin: 'com.android.library'

    applicationVariants.all 更改为libraryVariants.alltestVariants.all

    说明:

    applicationVariants(仅适用于应用插件)

    libraryVariants(仅适用于库插件)

    testVariants(适用于两个插件)

    这三个都分别返回 ApplicationVariant、LibraryVariant 和 TestVariant 对象的 DomainObjectCollection

    希望这会有所帮助。

    编辑:

    apply plugin: 'com.android.library'
    
    android {
        compileSdkVersion 25
        buildToolsVersion '25.0.3'
    
        defaultConfig {
            minSdkVersion 19
            targetSdkVersion 22
            versionCode 71
            versionName "2.0.8.0"
            if (project.hasProperty('ADD_BUILD_TO_VERSION')) {
                versionName = versionName.substring(0,versionName.lastIndexOf(".") + 1) + (System.getenv("BUILD_NUMBER") ?: "0")
            }
        }
    
        signingConfigs {
            release {
                storeFile file("../../../keys/AndroidScannerSDK.keystore")
                if (project.hasProperty('RELEASE_STORE_PASSWORD')) {
                    storePassword RELEASE_STORE_PASSWORD
                    keyAlias RELEASE_KEY_ALIAS
                    keyPassword RELEASE_KEY_PASSWORD
                }
            }
        }
    
        buildTypes {
            release {
                minifyEnabled false
                if (project.hasProperty('RELEASE_STORE_PASSWORD')) {
                    signingConfig signingConfigs.release
                }
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    
        lintOptions {
            abortOnError false
        }
    }
    
    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        compile 'com.android.support:appcompat-v7:25.3.1'
        compile 'com.android.support:design:25.3.1'
        compile project(':BarcodeScannerLibrary')
    }
    

    【讨论】:

    • 错误:库项目无法设置 applicationId。 applicationId 在默认配置中设置为“com.zebra.scannercontrol.app”。 (关于添加 testvariants.all)。 @Greaper
    • 使用apply plugin: 'com.android.application'时只能设置applicationId。
    • 错误:(49, 0) 无法为 com.android.build.gradle.internal.api.LibraryVariantOutputImpl 类型的对象获取未知属性“zipAlign”。 打开文件(关于添加 libraryVaraients.all)@Greaper.
    • 在 if 条件下? @Greaper
    • 错误:(49, 0) 无法为 com.android.build.gradle.internal.api.TestVariantImpl 类型的对象获取未知属性“zipAlign”。 nd 也适用于类型 libraryVariants 的对象。 @Greaper
    猜你喜欢
    • 2018-07-10
    • 2021-01-30
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    相关资源
    最近更新 更多