android - Flutter .apk 已构建但未安装在设备/模拟器中

标签 android flutter gradle groovy

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

收到以下错误。

Finished with error: Gradle build failed to produce an .apk file. It's likely that this file was generated under C:\Users\BnayA\Work\Office\sangrahapp\build, but the tool couldn't find it.



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

下面是我的应用程序/模块级别的 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'
}

最佳答案

尝试更换

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

关于android - Flutter .apk 已构建但未安装在设备/模拟器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62113662/

相关文章:

flutter - 如何将 Flutter 标签栏指示器更改为与 google play store 完全相同

dart - 在Flutter中构建期间维护StatefulWidget的状态

java - 在Android Studio中添加Facebook依赖项后出现Gradle构建错误

Android Gradle 库构建 : Include & exclude files from the jar inside the generated aar

java - Android XmlPullParser 是如何处理漏洞的?

android - android 中的数字选择器无法正确显示

firebase - 如何在身份验证时获取Idtoken

unit-testing - 在升级到JDK 8后,为什么我的Spock单元测试在Gradle构建中失败?

android - 根据sqlite中的日期检索条目时出错

java - 我的程序将无法继续播放音频