在 app 的 build.gradle 中通过outputDirectory和outputFileName修改目标 apk 文件的输出目录后,使用菜单"Run"->"Debug app..."时就会失败,提示 apk 文件无效(提示的是仍然默认的输出文件目录和文件名)。
错误提示如下:
Installation did not succeed.
The application could not be installed.
List of apks:
[0] 'C:\Android\Test\app\build\outputs\apk\debug\app-debug.apk'
Installation failed due to: 'Invalid File: C:\Android\Test\app\build\outputs\apk\debug\app-debug.apk'
Retry
修改版本输出目录的脚本如下:
applicationVariants.all { variant ->
variant.outputs.all { output ->
def app = applicationId
def type = variant.buildType.name
def releaseApkName = "${app}-${versionCode}.apk"
variant.packageApplicationProvider.get().outputDirectory = new File("build/apk/${type}")
outputFileName = releaseApkName
}
}
}