【问题标题】:Android failed to rename aar file in buildAndroid 无法在构建中重命名 aar 文件
【发布时间】:2015-06-12 03:18:46
【问题描述】:

您好,我正在尝试使用 gradle 构建和重命名我的 aar 文件,但出现上述错误:

A problem occurred evaluating project ':app'.
> Could not find property 'outputs' on BuildType_Decorated{name=variant, debuggable=false, testCoverageEnabled=false, jniDebuggable=false, pseudoLocalesEnabled=false, renderscriptDebuggable=false, renderscriptOptimLevel=3, applicationIdSuffix=null, versionNameSuffix=null, minifyEnabled=false, zipAlignEnabled=true, signingConfig=null, embedMicroApp=true, mBuildConfigFields={}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={}}.

这是我在 gradle 上的完整构建脚本

 apply plugin: 'com.android.library'

android.libraryVariants.all { variant ->
    def alignedOutputFile = output.outputFile
    def unalignedOutputFile = output.packageApplication.outputFile

    logger.warn('You got to variant: ' + variant + ' and output: ' + output)
    // Customise APK filenames (to include build version)
    if (variant.buildType.zipAlignEnabled) {
        // normal APK
        output.outputFile = new File(alignedOutputFile.parent, alignedOutputFile.name.replace(".aar", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".aar"))
    }
    // 'unaligned' APK
    output.packageApplication.outputFile = new File(unalignedOutputFile.parent, unalignedOutputFile.name.replace(".aar", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".aar"))

}

    android {
        compileSdkVersion 21
        buildToolsVersion '21.1.2'

        repositories {
            flatDir {
                dirs 'libs'
            }
        }



        defaultConfig {
            minSdkVersion 14
            targetSdkVersion 21
            versionCode 1
            versionName "1.0"
        }
        buildTypes {

            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    /*
      Both Joda-Time imports have these 2 files and they conflict with each other.  'exclude' is
      the workaround.
    */
    android.packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:21.0.3'
        compile(name: 'etsdk-3.5.0', ext: 'aar')
        // 3rd Party Libraries Required for SDK integration
        compile 'com.fasterxml.jackson.core:jackson-annotations:2.4.4'
        compile 'com.fasterxml.jackson.core:jackson-databind:2.0.6'
        compile 'org.joda:joda-convert:1.7'
        compile 'joda-time:joda-time:2.6'
        compile 'com.j256.ormlite:ormlite-android:4.48'
        compile 'com.j256.ormlite:ormlite-core:4.48'
        compile 'com.belladati:httpclientandroidlib:4.3.0'
        compile 'com.radiusnetworks:AndroidIBeaconLibrary:0.7.6'
        compile 'com.google.android.gms:play-services:4.0.30'
    }

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.1.+'

            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }

    allprojects {
        repositories {
            jcenter()
        }
    }

【问题讨论】:

    标签: android android-gradle-plugin


    【解决方案1】:

    您首先在此处引用output
    def alignedOutputFile = output.outputFile

    但是,您尚未声明它,这就是您收到 Gradle Could not find property 'output' 的警告的原因。

    您需要遍历属于您的variantoutputs,就像在this post 中一样:

    android.libraryVariants.all { variant ->
        variant.outputs.each { output ->
            output.outputFile = ...        
        }
    }
    

    或完全限定它,例如
    def alignedOutPutFile = variant.outputs[0].outputFile

    【讨论】:

    • 希望我也可以将您的答案标记为正确答案,但我将 Daniels 标记为一个,因为他帮助我更多地了解问题所在。还是谢谢
    【解决方案2】:

    我认为您的变体代码可能放错了位置。我尝试了以下方法,它对我有用:

    android.applicationVariants.all { variant ->
        variant.outputs.each{ output ->
            logger.warn('You got to variant: ' + variant + ' and output: ' + output)
        }
    }
    

    我把它放在 build.gradle 的顶层,而不是 buildTypes 或其他任何东西下,我可以看到:

    你必须变体: com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@3ab912fb 和输出: com.android.build.gradle.internal.api.ApkVariantOutputImpl_Decorated@6441365a

    你必须变体: com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@dc7d23 和输出: com.android.build.gradle.internal.api.ApkVariantOutputImpl_Decorated@b9b8546

    所以它确实找到了输出变量并对其进行迭代。

    编辑:由于您需要官方资源,请转到此处的“操作任务”部分:http://tools.android.com/tech-docs/new-build-system/user-guide

    来自网站:

    在 Android 项目中,这有点复杂,因为有 可能是大量相同的任务并生成它们的名称 基于构建类型和产品风味。

    为了解决这个问题,android 对象有两个属性:

    applicationVariants (only for the app plugin)
    libraryVariants (only for the library plugin)
    testVariants (for both plugins)
    

    【讨论】:

    • 还是不行。我收到此错误:> 在 com.android.build.gradle.internal.api.LibraryVariantImpl_Decorated@f10d055 上找不到属性“输出”。将更新我上面的帖子
    • 你错过了定义输出变量,记得在我的回复中做第二行:“variant.outputs.each{ output ->”
    • 没问题!另外,我不知道这是否只是您的错误,或者您对 Groovy 和 Gradle 感到迷茫,但如果是后者,我建议您查看此视频:youtube.com/watch?v=fHhf1xG0pIA 它解释了 Groovy 是什么,它与 Java 的关系,以及 Gradle 如何使用 Groovy。它真的帮助我理解了这一切。
    • 太好了。是的,我会检查出来。对 groovy 语法一无所知!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    相关资源
    最近更新 更多