【问题标题】:Flutter .apk is built but is not getting installed in the device/emulatorFlutter .apk 已构建但未安装在设备/模拟器中
【发布时间】:2020-09-18 15:48:04
【问题描述】:

我在模块级 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'
}

【问题讨论】:

  • 你能分享flutter doctor的输出吗?
  • @SanjaySharma "医生总结(要查看所有详细信息,请运行 flutter doctor -v):[√] Flutter(通道稳定,v1.12.13+hotfix.9,在 Microsoft Windows [版本 10.0.18363.657 ], locale en-IN) [√] Android 工具链 - 为 Android 设备开发 (Android SDK 版本 29.0.3) [√] Android Studio (版本 3.6) [√] VS Code (版本 1.45.1) [√] 连接的设备(1 个可用) • 未发现问题!”

标签: android flutter gradle groovy


【解决方案1】:

尝试替换

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

【讨论】:

  • 谢谢,但没有成功,并抛出了一个新错误stackoverflow.com/a/49558880/6254350
  • 你试过this
  • 请运行flutter upgrade和flutter clean
  • 只有在我设置自定义输出文件名时才会发生,否则它可以正常工作。
  • 所以apk正在生成。不是吗?
猜你喜欢
  • 1970-01-01
  • 2021-12-10
  • 2019-10-21
  • 2019-11-06
  • 1970-01-01
  • 1970-01-01
  • 2019-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多