首页 > 解决方案 > Flutter .apk 已构建但未安装在设备/模拟器中

问题描述

我在模块级 gradle 文件中设置了自定义 outputFileName。当我尝试通过按下播放按钮(或使用 debu/release 构建的菜单命令)来运行应用程序时,apk 正在构建,但应用程序没有安装在连接的设备上。

收到以下错误。

以错误结束:Gradle 构建未能生成 .apk 文件。该文件很可能是在 C:\Users\BnayA\Work\Office\sangrahapp\build 下生成的,但该工具找不到它。

我用谷歌搜索了它的解决方案,但没有任何帮助我解决它。

下面是我的应用程序/模块级别的 Gradle 配置。

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

ant.condition(property: 'os', value: 'windows') {
    os(family: 'windows')
}
ant.condition(property: 'os', value: 'unix') {
    os(family: 'unix')
}

// Based on http://stackoverflow.com/questions/17097263#24121734
def getMasterCommitCount = { ->
    try {
        def stdout = new ByteArrayOutputStream()
        exec {
            switch (ant.properties.os) {
                case 'windows':
                    commandLine 'cmd', '/c', 'git', 'rev-list', '--first-parent', '--count', 'master'
                    break
                case 'unix':
                    commandLine 'git', 'rev-list', '--first-parent', '--count', 'master'
                    break
            }
            standardOutput = stdout
        }
        return Integer.parseInt(stdout.toString().trim())
    } catch (ignored) {
        return -1
    }
}

def getVersionName = { ->
    try {
        def stdout = new ByteArrayOutputStream()
        exec {
            switch (ant.properties.os) {
                case 'windows':
                    commandLine 'cmd', '/c', 'git', 'describe', '--tags', '--dirty', '--always'
                    break
                case 'unix':
                    commandLine 'git', 'describe', '--tags', '--dirty', '--always'
                    break
            }
            standardOutput = stdout
        }
        return stdout.toString().trim()
    } catch (ignored) {
        return null
    }
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

android {
    compileSdkVersion 29

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        applicationId "abc.def.ghi"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode getMasterCommitCount()
        versionName getVersionName()
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        archivesBaseName = 'abc'
    }

    buildTypes {
        debug {
            applicationIdSuffix '.debug'
            versionNameSuffix '_debug'
        }

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

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            outputFileName = new File(outputFileName.replace(".apk", "-${defaultConfig.versionName}.apk"))
        }
    }

    splits {
        abi {
            universalApk true
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
}

标签: androidfluttergradlegroovy

解决方案


尝试更换

outputFileName = new File(outputFileName.replace(".apk", "-${defaultConfig.versionName}.apk"))

使用以下代码

 outputFileName = new File(
             output.outputFile.parent,
             outputFileName.replace(".apk", "-${defaultConfig.versionName}.apk"))

更新:替换这个

splits {
        abi {
            universalApk true
        }
    }

与以下

splits {
        abi {
            enable true
            reset()
            universalApk false
        }
    }

请同时添加签名配置debug

signingConfig signingConfigs.debug

推荐阅读