Exclude folder from APK in Android Studio - android

I've included a production folder in the root of my project, after building the APK and opening it as an archive, I've noticed that the production folder has not been included.
Am I right/safe to assume that any un-referenced files/folders are automatically excluded from the APK or should I be using some sort of configuration file to specify an exclusion list?

I would phrase it more as "only stuff in src/ might ever get included in an APK, for a conventional Android Studio project".
If you create random other directories, those will be ignored, unless you specifically do something in build.gradle to try using them (e.g., as an alternative location for assets/).
IOW, the contents of an APK come from a whitelist, not a blacklist.

Related

File 'root/lib/META-INF/MANIFEST.MF' uses reserved file or directory name 'lib'

I tried to apply for app bundle but I failed.
I can build apk with spilit option but I couldn't make aab.
I put the following gradle option.
bundle {
abi {
enableSplit = true
}
}
The error message in the console is below.
File 'root/lib/META-INF/MANIFEST.MF' uses reserved file or directory name 'lib'.
The file structure that was built is like below.
I heard I have to add the below library, but I didn't.
Actually, there is no difference either I add the play core library or not.
implementation "com.google.android.play:core:$play_version"
I wanna change some options to avoid alias problem but I don't have any idea about that.
Anyone who can handle this problem simply?
Make sure you don't have a directory called "lib/" in your project, since this directory name is reserved in the APK format for storing native libraries.
If not in your project, one your library dependencies must have it and is being copied into the APK.
The reason it works for APKs but not AABs is that the AAB format is more strict and will prevent you from embedding unnecessary files in your app.

Include files with different prefixes in apk

I have a project with native libraries that I want to use, files with this format: lib<name>.so do get included into apk. But files with <name>.so format does not.
Is there a way to include the later type into apk in lib directory?
If not, is there a way to include the files into a directory inside apk, where I can load it from my native code?
The short answer is "no". The native binaries will only be packed into APK, and extracted to executable files upon installation, if their names follow the lib….so pattern.
Note that these libraries will be extracted to files according to the ABI of the target system. The installer does not check the actual properties of the file. The decision is based on the name of the folder under lib in the APK structure.
If you add the attribute extractNativeLibs=false to the application tag in AndroidManifest.xml of your APK, the installer (on Android Nougat and higher) will not extract the native libraries.
You can trick the system and have files that don't follow the above rule to the lib folder of APK, but there is very little sense in it, because they will never be extracted by the loader (it may also extract file gdbserver if the file is there).
The common practice is to put the arbitrary files in the assets folder of your APK, and extract them programmatically when the app runs for the first time after install. You cannot extract these files to the secured location where the usual native libraries go. You should not extract the native libraries to sdcard (e.g. getExternalFilesDir()), because the system may not allow execution of the files there, regardless of the execute access flag on the file. Make sure that you use the correct ABI flavour.
You can peek at the source code of Nougat native lib loader that can load native libraries from the APK without extraction, and use it to load your custom libraries directly from the assets folder of your APK.

Load google-services.json from local file system

The google-services.json file used by the Firebase Android plugin is usually put in the app's module folder.
What I do want to do instead is put this file somewhere on the filesystem completely outside the project folder and let gradle / the Firebase plugin read it from there.
I thought about putting it's location in the local.properties and somehow 'tell' the Firebase plugin to get the location of the google-services.json from there.
How can I achieve this?
According to offical documentation, the google-services.json file is generally placed in the app/ directory (at the root of the Android Studio app module). As of version 2.2.0 the plugin supports build type and product flavor specific JSON files. All of the following directory structures are valid:
// dogfood and release are build types.
app/
google-services.json
src/dogfood/google-services.json
src/release/google-services.json
...
Note: Providing a google-services.json file in the release directory allows you to maintain a separate Firebase project for your production APKs.
When product flavors are in use these more complicated directory structures are also valid.
// free and paid are product flavors.
app/
google-services.json
src/dogfood/paid/google-services.json
src/release/free/google-services.json
...
As a conclusion, you cannot add google-services.json file outside your project.
Hope it helps.

Excluding files from compilation

I have a number of files in a project that I don't want to include in the compiled apk.
For source files, right-click -> Build Path -> Exclude does the trick.
How about for excluding other files, for example certain ones in the assets folder? If my understanding is correct, all of these are included in the apk by default.
Also, if I create a custom folder in my project structure (e.g. inside the assets onee) it will show in the package explorer. Would its contents be added to the apk?
Apparently, folders named _pre_production are not added to the APK.
I cannot take credit for this. Here is where I found the answer:
Ignoring files from Android APK
I'm duplicating the answer here in case someone comes by.

Android - Secure place to store sensitive files inside the same project

Let's say I have an Android Project set up and a git repository for it. Where can I put my keystore (and other sensitive files) inside my project so I can be guaranteed that it will not end up inside the .apk when I export the app (but will be inside the git repository)?
For instance, I can't put sensitive files inside the assets folder because that will end up inside the .apk.
Is there a place to store these kind of files in the Android file hierarchy I am missing?
Don't put your keystore anywhere in your source control or project folder even if you have .gitignored it. It is a personal document, to be treated as you would your SSH keys.
It is better to keep it in an external location, such as your documents folder or personal keystore folder, which you manage yourself. As you have mentioned, these don't need to end up in the APK so they are purely supporting documents and files.
Is there a place to store these kind of files in the Android file hierarchy I am missing?
Put them in the project root. The only file from the project root that is incorporated directly into the APK is AndroidManifest.xml, and even that is moving with the new build system and project directory structure.
Or, create some other directory that does not collide with any existing directories and will not be used by Android, like _stuff/.
Or, have your project itself be in a subdirectory, putting other related files in a peer directory.
Also, make sure you set up your .gitignore properly such that files only go into the Git repo that you actually want in the Git repo.

Categories

Resources