Thursday, March 24, 2011

Copying data files using Android.mk

One of the most under-documented areas of Android at present is its makefile. There is of course a ton of those for reference under Android source tree, but they aren't really 'documentation'. THIS is the only one I found that is reasonably adequate. The other day I was trying to find a way to copy an xml file into the image's /system/etc/permissions folder through Android.mk. Upon googling, I realized that many people were looking for ways to run shell commands through the makefile so that they can run copy commands. I was pretty sure that there had to be a better way, just not documented. And then I found it. The template to copy a file is this:

include $(CLEAR_VARS) 
LOCAL_MODULE := my_data_file.cfg 
LOCAL_MODULE_TAGS := optional 
LOCAL_MODULE_CLASS := ETC 
LOCAL_MODULE_PATH := /path/to/the/destination/folder 
LOCAL_SRC_FILES := sources_files_to_copy 
include $(BUILD_PREBUILT)

So if you want to copy an xml file to the permissions folder, you could do this:
include $(CLEAR_VARS)
LOCAL_MODULE := mydata.xml
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_CLASS := ETC
# Copies mydata.xml to /system/etc/permissions
LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/permissions
LOCAL_SRC_FILES := $(LOCAL_MODULE)
include $(BUILD_PREBUILT)

The above example is from one of Android's makefiles (location service) found here.  You should be able to use the above snippet and make it copy any file to any folder. 



16 comments:

Anonymous said...

Hi,
Can you please tell us, what are mk files and how to use them..?

Karthik said...

mk is Android's version of Makefile. You could look at this for some info on how to write it - http://www.netmite.com/android/mydroid/development/pdk/docs/build_system.html

Karthik said...

LOCAL_SRC_FILES specifies where it is copied from. In this case, it just points to mydata.xml, so the assumption is that this xml file is in the same location as the makefile.

Anonymous said...

Hi Karthik,

How can I copy a file such that the file permissions are preserved?

Karthik said...

I think permissions are automatically set using some hardcoded values in one of the header files. I confirmed this by googling - http://stackoverflow.com/questions/10878369/how-do-i-preserve-file-permissions-in-a-custom-external-project-added-to-android . See if that helps.

Anonymous said...

Thanks for the response. I came across this too, but just wanted to check if there was some way to do it in the Makefile itself. Thank you!

Unknown said...

hi karthik,
can you assist with adding a prebuilt apk to the build AOSP?

Karthik said...

Doesn't BUILD_PREBUILT work for APK as well?

Unknown said...

Yes but i want to add my apk into system.img in such way tht loading the sys.img onto device will also install my app.

Karthik said...

Perhaps you can explain the situation better. Do you have your entire project as source code, and is it copied somewhere under the android source tree? When you build the system image, do you see your project compiling into an apk - you should see it go to out/target/products/xxxx/system/app/MyProject.apk (or another name). I'm not sure what is preventing your apk from going into the system image.

Unknown said...

i want to add my prebuilt apk to the img file generated.
i dont want to do it from the source.

Karthik said...

It is similar to my example. See here - http://www.kandroid.org/online-pdk/guide/build_cookbook.html under "Adding a prebuilt APK".

Unknown said...

worked same but failed.

Karthik said...

You mean it didn't end up in the image? I'm not sure then, sorry.

Anonymous said...

Hi Karthiksden,

Your blog helped me getting my issue resolved partially.
We also need to put LOCAL_MODULE name in core.mk of build/target/product.

Thanks,
Sushil

Unknown said...

What is content of permission.xml file? I want to add extra permission to a prebuilt app.