尝试构建apk-错误:任务':app:packageDebug'执行失败

时间:2019-02-19 11:46:58

标签: android android-gradle

  

错误:任务':app:packageDebug'的执行失败。   无法创建目录>> E:\ workspace_android_studio_SaudeMovel_0_7_0_1 \ app \ build \ outputs \ apk \ debug \ E:\ workspace_android_studio_SaudeMovel_0_7_0_1 \ app \ build \ outputs \ apk \ debug \ app-debug.ap

我一直试图找出问题所在,并在stackoverflow中阅读了多个问题,但是我无法解决该问题。

此代码很旧,而我的android studio是最新的。将gradle更新为最新版本时,我无法再生成apk(是否已签名),即使尝试使用社区中的替代方法,也会始终出现此错误。

我尝试了所有这些方法,但没有一个成功。

        if (outputFile != null && outputFile.name.endsWith('.apk')) {
            def fileName = "agente-" + variant.buildType.name + ".apk"
            output.outputFileName = new File(outputFile, fileName)
        }


if (outputFile != null && outputFile.name.endsWith('.apk')) {
                def fileName = outputFile.name.replace('.apk', "-${variant.versionName}.apk")
                output.outputFile = new File(outputFile.parent, fileName)

             output.outputFileName = new File(
                    output.outputFile.parent,
                    output.outputFile.name.replace(".apk", "-${variant.versionName}.apk"))
                    //output.outputFileName = new File(output.outputFile.parent, output.outputFile.name.replace("app-release.apk", "Your_name.apk"))*//*
            }

这是我的礼物:

import java.text.SimpleDateFormat

apply plugin: 'com.android.application'

android {
    signingConfigs {
        debug {
            keyAlias 'debug'
            keyPassword '123456'
            storePassword '123456'
            storeFile file("../keystore_debug.jks")
        }
        config {
            keyAlias 'debug'
            keyPassword '123456'
            storeFile file('../keystore_debug.jks')
            storePassword '123456'
        }
    }
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    compileOptions.encoding = 'ISO-8859-1'
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        applicationId "br.com.egoverne.saudemovel"
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 4
        versionName "0.7.0.1"
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }
        multiDexEnabled true
        signingConfig signingConfigs.config
    }
    buildTypes {

        debug {
            debuggable true
            buildConfigField "String", "DOMAIN", '"http://192.168.12.15:8081/"'
            buildConfigField "String", "VERSION_DATE", "\"" + getDateBuild() + "\""
            buildConfigField "String", "EPORTAIS_DOMAIN", '"200.186.92.115:80"'
            buildConfigField "String", "APONTAMENTO_SERVIDOR", '"Debug http://192.168.12.15:8081/"'
           // signingConfig signingConfigs.debug
        }
        debugDevjBoss {
            debuggable true
            buildConfigField "String", "DOMAIN", '"http://192.168.12.15:8082/"'
            buildConfigField "String", "VERSION_DATE", "\"" + getDateBuild() + "\""
            buildConfigField "String", "EPORTAIS_DOMAIN", '"200.186.92.115:80"'
            buildConfigField "String", "APONTAMENTO_SERVIDOR", '"Debug jBoss http://192.168.12.15:8082/"'
            signingConfig signingConfigs.debug
        }

        debugHomologjBoss {
            debuggable true
            buildConfigField "String", "DOMAIN", '"http://10.115.4.74:8081/"'
            buildConfigField "String", "VERSION_DATE", "\"" + getDateBuild() + "\""
            buildConfigField "String", "EPORTAIS_DOMAIN", '"200.186.92.115:80"'
            buildConfigField "String", "APONTAMENTO_SERVIDOR", '"Debug Homolog http://10.115.4.74:8081/"'
            signingConfig signingConfigs.debug
        }
        debugHomologIPjBoss {
            debuggable true
            buildConfigField "String", "DOMAIN", '"http://191.242.241.178:8081/"'
            buildConfigField "String", "VERSION_DATE", "\"" + getDateBuild() + "\""
            buildConfigField "String", "EPORTAIS_DOMAIN", '"200.186.92.115:80"'
            buildConfigField "String", "APONTAMENTO_SERVIDOR", '"Debug Homolog  IP http://191.242.241.178:8081/"'
            signingConfig signingConfigs.debug
        }
        releaseIp {
            debuggable true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            buildConfigField "String", "DOMAIN", '"http://191.242.241.168:8082/"'
            buildConfigField "String", "VERSION_DATE", "\"" + getDateBuild() + "\""
            buildConfigField "String", "EPORTAIS_DOMAIN", '"eportal.osasco.sp.gov.br"'
            buildConfigField "String", "APONTAMENTO_SERVIDOR", '"191.242.241.168:8082"'
            signingConfig signingConfigs.config
        }
        releaseDebug {
            debuggable true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            buildConfigField "String", "DOMAIN", '"http://saudemovelagentews.osasco.sp.gov.br:8082/"'
            buildConfigField "String", "VERSION_DATE", "\"" + getDateBuild() + "\""
            buildConfigField "String", "EPORTAIS_DOMAIN", '"eportal.osasco.sp.gov.br"'
            buildConfigField "String", "APONTAMENTO_SERVIDOR", '"osasco.sp.gov.br"'
            signingConfig signingConfigs.config
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            buildConfigField "String", "DOMAIN", '"http://saudemovelagentews.osasco.sp.gov.br:8082/"'
            buildConfigField "String", "VERSION_DATE", "\"" + getDateBuild() + "\""
            buildConfigField "String", "EPORTAIS_DOMAIN", '"eportal.osasco.sp.gov.br"'
            buildConfigField "String", "APONTAMENTO_SERVIDOR", '"osasco.sp.gov.br"'
            signingConfig signingConfigs.config
        }
    }
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                def fileName = "agente-" + variant.buildType.name + ".apk"
                output.outputFileName = new File(outputFile, fileName)
            }
/*            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                def fileName = outputFile.name.replace('.apk', "-${variant.versionName}.apk")
                output.outputFile = new File(outputFile.parent, fileName)
                */
             /*output.outputFileName = new File(
                    output.outputFile.parent,
                    output.outputFile.name.replace(".apk", "-${variant.versionName}.apk"))
                    //output.outputFileName = new File(output.outputFile.parent, output.outputFile.name.replace("app-release.apk", "Your_name.apk"))*//*
            }*/
        }
    }
    configurations.all {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            def requested = details.requested
            if (requested.group == 'com.android.support') {
                if (!requested.name.startsWith("multidex")) {
                    details.useVersion '26.0.2'
                }
            }
        }
    }
    dexOptions {
        maxProcessCount 4 // this is the default value
        javaMaxHeapSize "2g"
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
        pickFirst 'META-INF/license.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/MANIFEST.MF'
    }
}

dependencies {
    compile files('libs/android-java-air-bridge.jar')
    compile files('libs/commons-collections4-4.0.jar')
    compile files('libs/commons-io-2.4.jar')
    compile files('libs/commons-lang-2.6-javadoc.jar')
    compile files('libs/commons-lang-2.6-sources.jar')
    compile files('libs/commons-lang-2.6.jar')
    compile files('libs/java_websocket.jar')
    compile files('libs/gson-2.8.0.jar')
    compile 'com.google.android.gms:play-services:+'
    compile 'com.android.support:multidex:1.0.0'
    compile 'com.android.support:appcompat-v7:26.0.2'
    compile 'com.android.support:mediarouter-v7:26.0.2'
    compile 'com.android.support:support-v13:26.0.2'
    compile 'com.android.support:design:26.0.2'
    compile 'com.android.support:cardview-v7:26.0.2'
}

def getDateBuild() {
    def df = new SimpleDateFormat("dd/MM/yyyy")
    return df.format(new Date())
}

我正在尝试调试我的应用。

1 个答案:

答案 0 :(得分:1)

查看此link

打开app.graddle文件,并替换为applicationVariants.all ...

android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}-${variant.versionName}.apk"
    }
}

构建->创建项目

然后生成您的签名APK