Use another set of resources in Android (NOT runtime, but compiletime) - android

I want to build two different APK's from my Android application. The differance between the two are images in the res/drawable directory. I know that it is possible to use a different set of resources during runtime depending on language settings, hardware or other, but how to do it compiletime? (is it possible to specify another directory then res/drawable?)
(currently, i'm copying/replacing all resources each time i want to build a new version)

Sound like you need to use Ant build tools.
Create in your project a custom folder that contains to different assets folders for each APK,
then, use ANT build with a proper configuration file that u need to create to build the project with the appropriate assets and files before it's beening compiled.
Good luck!
Update:
Here is a tutorial for using the Ant build tool with android:
Android Ant build tutorial

Perhaps you should use the assets? I find a link that talks about the resources and assets!: here

One possibility could be to use 2 different library project and put in these project your images / values. you would have to change the properties of the project each time you need to compile with another library project
It's a bit inconvenient but would work...
Hope it helps

Related

How to copy resources files from other directory into Android project during sync or build?

The question
When you need to use resource files, such as images, in your Android project, typically you put them in the res directory (e.g. res/drawable), and the Android build process picks them up by default. But what if your resource files come from a directory outside of your Android project? How do you copy them into a folder like res/drawable during sync or build?
What I have tried
I have a feeling that the solution is to write Gradle code, but I'm not sure how to do it. I tried the following code in my app module build.gradle file:
task copyResources(type: Copy) {
from '../../../images'
into 'res/drawable'
}
But it did not copy anything as far as I can tell. This is probably just my ignorance of how to use Gradle, and I could use some help.
I realize I could manually copy from ../../../images to res/drawable, but let's say those files change often, and I want to get the current version automatically whenever I sync or maybe whenever I build. If this is possible, I would appreciate knowing how to do it. If it is not possible, I would like to know that as well.
See How to run copy task with android studio into assets folder
Can do with adding
preBuild.dependsOn copyResources
after task copyResources(){...}
in your build.gradle.
But, it's not a good way for copying resources.
Generally you don't want to do that. The reason is that it makes what would otherwise be a clean source control system confused by the copied files.
A better idea would be to build an Android library (AAR) housed in a separate project, then consume them by adding that AAR as a dependency to the parent project.

How to create an android library project with no resources

I want to create a library project with just java/Android classes - but no resources. Basically it will contain helpers for IO etc. I have manually deleted the resources etc - the project is here - but I wonder if this can be done out of the box - or is my way not proper
NB: I need Android classes - so creating a regular java project is not an option (?)
Edit : if there is no out of the box way is there any catch in deleting the (edit: contents of the) res/ folder and the support library ? I would appreciate a 1-2-3 procedure
You need to keep project structure, incl. having res/ foder with drawable/ etc, but these folders can be empty. There's no requirement for your library to reference any drawable.
Others suggest it's mandatory for "app icon" but your library does not need any <application> entry in manifest, so it does not need any icon. The same applies to values/ and layout/ folders. Your library project have to have these folders (as required by build process), but having no file in them (so basically keeping them empty) is perfectly fine and valid and meets your requirements.
I believe you should keep all the required folders (including res) but you can leave them empty. Here is a guide for it.
http://developer.android.com/tools/projects/projects-eclipse.html#SettingUpLibraryProject
I believe you cannot delete the res folder completely since the app icon is contained in it. However you can delete the default layout if you do not need it and any other content that is required for the app to work.
If you would like to access the Android classes you will have to create an Android project.
I hope this helps.
I have created a git repository to illustrate - I created a vanilla eclipse android library project (screens) added a git ignore (bin/) and then proceeded to delete unneeded resources (all the contents of the res/ folder) along with libs/ and assets/ and edit the manifest. The changes are in this commit for reference. Cleaning the project removed R.java along.
Will be adding more (.gitattributes and eclipse settings for android) but this is for now.

How do I use Ant to build multiple Android APKs with the same code base, but with different resource files?

I am trying to build a branded Android app, that will have different resource files (mainly drawables) for different customers. The Java code in the apps will be the same, but the different apps will have a different look--meaning different color schemes and different logos etc. I have been told, and my research suggests that Ant is the best way to achieve this.
Where should I change the Android 'build.xml' file? What sort of Ant task should I use to achieve this?
For debugging, I would like to just use Eclipse and build with the default resources. But for production releases, I would like to run Ant on build.xml to produce multiple APKs from different /res folders. I do not even need any code snippets (although that would be nice), just even some tips from people that have done this before or something similar.
I ended up writing a Python script to reorganize the Android project before each build. I recommend avoiding the use of Ant for stuff like this. My 100-200 lines of Python achieved the equivalent of about 1000+ extra lines of Ant that was required in addition to the default Android build.xml Ant script.
I have encountered a similar issue where I want to compile the same code base with some changes as well as changes in the resource files. The solution I am pursuing at the moment is creating a library with the shared code/resources and separate "regular" projects for each of the different APK releases. Unfortunately, I don't have a functional final solution to the problem yet, so all I can give is these vague hints.
Requirement Setup
1. Go to the URL :
http://ant.apache.org/bindownload.cgi
and download the apache ant and extract it in to the any folder like d:/ant .
2. I think you already having the Android SDK path is like(D:/Android SDK/)
Now go to the Enviroment variables and check whether path variable is there is there add these in path otherwise create a new varible with name path and put it there in vaue
D:/ant/bin;D:/Android SDK/tools
done
Go to command prompt and navigate it to the root directory of your project and run the command
android update project -p /
Note: / if you are in the root directory of project else you need to give the path of root directory like d:/myworkspace/mysampleproject
this will generate and build.xml file in your project.
now run the command for this it is necessary to be in root directory of your project
ant debug
this will generate an apk file in bin folder signed with default debug key.

Share assets among multiple projects

As we all know by now, Android's Eclipse+ADT IDE framework doesn't support (re)using assets from a Library Project.
Instead, any asset resources used by an application must be stored in the assets/ directory of the application project itself.
My problem is that I have 8 (eight!) applications using the same Library Project that uses assets and as a result of the aforementioned limitation I have to copy/duplicate and sync each of the asset files to all 8 (eight!) applications.
This is a maintenance nightmare and it is error prone, too.
Any idea how to work around this by sharing a single copy of each asset among multiple application projects?
Note: I'd like to avoid an OS or filesystem dependent solution like hardlinks or softlinks. I prefer a solution like context.getAssets(), if it indeed does the work.
Maybe things have changed since you posted the question, but I was able to reuse asset files from another project in my Android project. I created my project's assets folder as a linked folder. I posted a sample project to show how it works.

how to reference an asset in a library project

In a class belonging to a Library project I call:
webview.loadUrl("file:///android_asset/info.html", null);
Unfortunately, this only works if I duplicate the file info.html into the Application's project asset folder as well.
Is there a way to tell an Android library code: "look for this file in the library's assets folder, not in the application's assets folder" ?
This answer is out of date, the gradle build system and AAR files support assets.
From the Android Docs:
Library projects cannot include raw assets
The tools do not support the use of raw asset files (saved in the assets/ directory) in a library project. Any asset resources used by an application must be stored in the assets/ directory of the application project itself. However, resource files saved in the res/ directory are supported.
If you want to include files from a Library project, you'll need to put it in the resources instead of the assets. If you're trying to load HTML files from your library project into a WebView, this means that you'll have to go a more roundabout method than the usual asset URL. Instead you'll have to read the resource data and use something like loadData.
This is now possible using the Gradle build system.
Testing with Android Studio 0.5.0 and v0.9 of the Android Gradle plugin, I've found that files such as
MyLibProject/src/main/assets/test.html
are correctly packaged in the final application and can be accessed at runtime via the expected URL:
file:///android_asset/test.html
You can achieve this by creating a symbolic link in the project's asset folder that points to the directory in the library project.
Then you can access as below:
webview.loadUrl("file:///android_asset/folder_in_a_libary_project/info.html", null);
Okay. Ive been stressing out and losing sleep about this for a while. Im the type of person that loves API creation, and HATES complicated integration.
There arent many solutions around on the internet, so im quite proud of what Ive discovered with a bit of Eclipse Hackery.
It turns out that when you put a file in the Android Lib's /assets folder. The target apk will capture this and place it on the root of the APK archive. Thus, making general access fail.
This can be resolved by simply creating a Raw Java Library, and placing all assets in there, ie (JAVALIB)/assets/fileX.txt.
You can in turn then include this as a Java Build Path Folder Source in
Project > Properties > Java Build Path > Source > Link Source.
Link Source
Click on Variables. and Add New Variable, ie VAR_NAME_X. location : ../../(relative_path_to_assets_project)
Click Ok
Now, when you build and run your app, the assets folder in the APK will contain your (GLOBAL Library) files as you intended.
No need to reconfigure android internals or nothing. Its all capable within a few clicks of Eclipse.
I confirm that Daniel Grant's approach works for at least the following situation: target project does NOT have an asset folder (or the folder is empty, so you can safely delete it).
I did not setup any variable.
Simply setup a LinkSource as follows (just an example)
Linked folder location: /home/matthew/workspace_moonblink/assetsForAdvocacy/assets
Folder name : assets
The "assetsForAdvocacy" is a Java project, (created with New- Project - Java Project) with empty src folder, and a new folder named "assets", which now provides the entire assets folder for the target project.
This is a fairly straightforward way within Eclipse to provide assets re-use across many different projects IF they do not already have assets, good enough to get going with. I would probably want to enhance it to become a content provider in the long run, but that is a lot more development.
My project accesses the assets with the following code:
String advocacyFolderInAssets = "no_smoking/"; //a folder underneath assets/
String fn =advocacyFolderInAssets+imageFilename;
Bitmap pristineBitmapForAdvocacy = getBitmapFromAsset(context, fn);
I use Motodev Studio 3.1.0 on Ubuntu. It would not let me 'merge' a new assets folder in the new assets-only project onto an existing assets folder in the target project.
If you want to use a setup where multiple derivate products are created from one library you might consider using svn:externals or similar solution in your SCM system. This will also do the trick that static assets like online help may be versioned seperately from the android source code.
I found this older question, it might help you, too.
This is the official way Google uses to archive this (from the above post): Link

Categories

Resources