问题
I recently noticed a new file generated at <module>/release/output.json
by Android Studio 3 Canary 1 each time I run Build
-> Generate Signed APK...
, which has contents that look like following.
Can anyone confirm seeing this behavior as well? Or is it due to some local configuration on my laptop?
And can anyone explain the purpose of this file? Is it safe to add to .gitignore
?
[{
"outputType": {
"type": "APK"
},
"apkInfo": {
"type": "MAIN",
"splits": [],
"versionCode": 32
},
"outputFile": {
"path": "/path/to/the/generated/release/filename.apk"
},
"properties": {
"packageId": "com.example.android",
"split": ""
}
}]
回答1:
Android studio 3.0 is responsible for this file. You don't need to worry about the output.json file.
Let me explain this to you:
For older versions, what Android Studio did was generate a signed APK and put it in the "output" folder. Even If you had multiple flavour dimensions for your APK, all of them could be located at the same directory, which was the output folder. From the latest release of Android Studio 3.0 (canary and stable), they have organized this file structure. For every flavour dimension, whenever you sign an APK, it will have a separate folder with a corresponding output.json file in it. This file is actually nothing but a description of the source APK. As you can see, the file you shared here is describing the released APK.
回答2:
ouput.json
file is kind of a metadata file for your generated APK. This file is generated due to various reasons. I have found some of them, which might not list all of the use cases, but here's the list:
- Generated when
Generate Signed APK
is executed Generated for
AndroidManifest.xml
file under{module}/build/intermediates/manifest/androidTest/debug/ouput.json
It is not generated for Unit tests, but only generated for AndroidTests (which depends on Android framework to be executed)
- The file
output.json
generated forAndroidManifest.xml
at above specified location is slightly different from the one generated for APK as you have mentioned. - As you can see the properties described by
output.json
file is very similar to the properties that we usually specify in ourbuild.gradle
file, so it must be used & required for build process to work successfully (or it might be getting generated as a result of successful build and extracting of required properties frombuild.gradle
).
Upon this, we can conclude that it surely depends upon Android framework to be generated & it is related to be describing the details/info about the APK or Manifest file.
I have personally tried to Google & find a proper answer to this even on Android Developers website, but it seems nothing is documented about this file in detail.
I have checked several projects on GitHub & checked
.gitignore
file for the same, I couldn't find any of the similaroutput.json
file in any of the projects hosted on GitHub. So it should be a good practice to exclude them in your commits.In short, this file is a descriptive file containing important metadata about project. It must be there for a reason. I would suggest you not to mess with it as we don't know what it may result in.
回答3:
For anyone who want to disable this feature, here is my trick.
It simply delete output.json
after generation.
applicationVariants.all { variant ->
variant.assemble.doLast {
def buildType = variant.buildType.name
def outputPath = ""
// If you use separated output path for both condition.
if (buildType == "debug") {
outputPath = "${buildDir}/outputs/apk"
}
if (buildType == "release") {
outputPath = "${rootDir}/apk"
}
println "outputPath:" + outputPath
delete "${outputPath}/yourFlavor1/${buildType}/output.json"
delete "${outputPath}/yourFlavor2/${buildType}/output.json"
delete "${outputPath}/yourFlavor.../${buildType}/output.json"
}
}
回答4:
In response to the elaborate answer by @wonsuc, you can have gradle remove the file upon a successful build by adding the following code to the android
section:
android {
...
applicationVariants.all { variant ->
variant.assemble.doLast {
variant.outputs.each { output ->
delete "${output.outputFile.parent}/output.json"
}
}
}
}
回答5:
With previous versions of Android Studio, I had attempted to automatically name the output apk based on the output of git describe
. However, while "Syncing Project with Gradle Files" Android Studio would run the gradle script once and capture the build config, including the output filename, and then assume that every build would continue to use the same name.
So whenever I created a new commit, the actual output filename would change. But Android Studio would either install the old version or fail if it had been cleaned up.
I believe that output.json has been added to allow Android Studio to load everything it needs to know about the last build, even if you have customised your gradle script to change something in an unexpected way.
This change isn't specifically mentioned in the gradle plugin release notes (https://developer.android.com/studio/releases/gradle-plugin.html#3-0-0). Though they did make a bunch of drastic changes for performance reasons. It makes sense to me that they would prefer to run less of your gradle script when syncing. Instead capturing information about the build outputs directly from the build process.
回答6:
I add ../../
to the beginning of the outputFileName
, and the APK file is put into the output
folder
android{
applicationVariants.all { variant ->
variant.outputs.all { output ->
output.outputFileName = "../../output_name.apk"
}
}
}
来源:https://stackoverflow.com/questions/44202176/what-is-the-module-release-output-json-generated-by-android-studio