Build an Android project: eliminate junk files
Finishing my first Android project, I ran into the problem of not having enough flexibility when building the release.
Initially, ant is supposed to be used for assembly. When creating a project, the platform kindly creates you build.xml . As a rule, the whole configuration consists of specifying the path to the Android-SDK in the local.properties file. You may also want to specify there key.store and key.alias parameters so that the application signs automatically during assembly. Google does not recommend doing this in the documentation, because the password proposed for the key during the assembly process is logged into Shell.
But what if you need to do something additional during assembly? In my case, it was necessary to exclude some files from the final package, located assets / testfolder. These files store the paths to test web services and data for authorization. There is absolutely no need to include them in the final application. The solution was not found quickly, despite its simplicity. The problem is the small amount of documentation. I suggest looking inside to save time in the future.
If you look inside build.xml , you can find some commented out elements for customization there:
After the instruction comes,
it imports the contents of the SDK / tools / ant / main_rules.xml. If you look inside the specified file, you can see the whole process of building the project. Unfortunately, from there we see that using -pre-build, -pre-compile or -post-compile is not suitable for the task at hand. The process of packing assets into an .apk file looks like this: Obviously, you need to change something in this task. Logically, I saw two options: 1. Somehow set exceptions that will filter out unnecessary assets when packing. 2. Add one more step: deleting unnecessary files from an existing apk. The most difficult was to search for documentation on. Obviously, the task relies on the aapt platform tool. But all that is about it is a formal help in Shell and a piece of text in the documentation:
The other platform tools, such as aidl, aapt, dexdump, and dx, are typically called by the Android build tools or Android Development Tools (ADT), so you rarely need to invoke these tools directly. As a general rule, you should rely on the build tools or the ADT plugin to call them as needed.
From the task-def headers in main-rules.xml, we see that this aapt is nothing more than the com.android.ant.AaptExecLoopTask class . Find the source codeclass. From it we see that the task is nothing more than a narrow layer for standard packaging using the aapt command-line utility. You can customize paths and names, but a deeper flexibility is not laid down, as it is not laid down in aapt itself when working with assets. The tool only allows you to specify a directory, but not to manage file lists. If you still see the options - I will be glad to read them in the comments.
Therefore, I decide to leave the package as it is, and go the second way: delete unnecessary files after the package. To do this, inside our build.xml, we redefine the dependencies for the
"-package-release" task, which relies on the packaging task: Now this target will be used instead of the original from main_rules.xml. It adds a new dependency
-package-removetestassets . Add the simplest task that will remove unnecessary assets: The result is achieved, with the minimum possible changes to build.xml. And this is very important, because reduces risks when upgrading to new versions of Android. I hope this post is helpful.
Initially, ant is supposed to be used for assembly. When creating a project, the platform kindly creates you build.xml . As a rule, the whole configuration consists of specifying the path to the Android-SDK in the local.properties file. You may also want to specify there key.store and key.alias parameters so that the application signs automatically during assembly. Google does not recommend doing this in the documentation, because the password proposed for the key during the assembly process is logged into Shell.
But what if you need to do something additional during assembly? In my case, it was necessary to exclude some files from the final package, located assets / testfolder. These files store the paths to test web services and data for authorization. There is absolutely no need to include them in the final application. The solution was not found quickly, despite its simplicity. The problem is the small amount of documentation. I suggest looking inside to save time in the future.
If you look inside build.xml , you can find some commented out elements for customization there:
[This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override ${out.dex.input.absolute.dir}]
-->
After the instruction comes,
it imports the contents of the SDK / tools / ant / main_rules.xml. If you look inside the specified file, you can see the whole process of building the project. Unfortunately, from there we see that using -pre-build, -pre-compile or -post-compile is not suitable for the task at hand. The process of packing assets into an .apk file looks like this: Obviously, you need to change something in this task. Logically, I saw two options: 1. Somehow set exceptions that will filter out unnecessary assets when packing. 2. Add one more step: deleting unnecessary files from an existing apk. The most difficult was to search for documentation on. Obviously, the task relies on the aapt platform tool. But all that is about it is a formal help in Shell and a piece of text in the documentation:
Packaging resources
forces no compression on any files in assets or res/raw -->
forces no compression on specific file extensions in assets and res/raw -->
The other platform tools, such as aidl, aapt, dexdump, and dx, are typically called by the Android build tools or Android Development Tools (ADT), so you rarely need to invoke these tools directly. As a general rule, you should rely on the build tools or the ADT plugin to call them as needed.
From the task-def headers in main-rules.xml, we see that this aapt is nothing more than the com.android.ant.AaptExecLoopTask class . Find the source codeclass. From it we see that the task is nothing more than a narrow layer for standard packaging using the aapt command-line utility. You can customize paths and names, but a deeper flexibility is not laid down, as it is not laid down in aapt itself when working with assets. The tool only allows you to specify a directory, but not to manage file lists. If you still see the options - I will be glad to read them in the comments.
Therefore, I decide to leave the package as it is, and go the second way: delete unnecessary files after the package. To do this, inside our build.xml, we redefine the dependencies for the
"-package-release" task, which relies on the packaging task: Now this target will be used instead of the original from main_rules.xml. It adds a new dependency
-package-removetestassets . Add the simplest task that will remove unnecessary assets: The result is achieved, with the minimum possible changes to build.xml. And this is very important, because reduces risks when upgrading to new versions of Android. I hope this post is helpful.
Excluding test assets from apk