How to add extra permission to a prebuilt application (no source code) in AOSP
<p>I have an application that doesn't have a specific android permission(for example <code>android.permission.CHANGE_CONFIGURATION</code>).</p>

<ol>
<li>I don't have its source code.</li>
<li>I'm working on an AOSP.</li>
</ol>

<p>I prebuilt this application like:</p>

<ol>
<li>Put APK in <code>/device/model/apps/HERE</code></li>
<li>Add these snippet codes in Android.mk:  </li>
</ol>

<p><code>define PREBUILT_templateByMe
    LOCAL_MODULE := $(1)
    LOCAL_MODULE_CLASS := APPS
    LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
    LOCAL_CERTIFICATE := PRESIGNED
    LOCAL_SRC_FILES := $$(LOCAL_MODULE).apk
    LOCAL_REQUIRED_MODULES := $(2)
    include $(BUILD_PREBUILT)
endef</code></p>

<p><code>define PREBUILT_APP_templateByMe
    include $$(CLEAR_VARS)
    LOCAL_MODULE_TAGS := optional
    $(call PREBUILT_templateByMe, $(1), $(2))
endef</code></p>

<p><code>prebuilt_appsByMe := \
    myapp</code></p>

<p><code>$(foreach app,$(prebuilt_appsByMe), \
    $(eval $(call PREBUILT_APP_templateByMe, $(app),)))
include $(call all-makefiles-under,$(LOCAL_PATH))</code></p>

<p>It's work very well, and myapp prebuilt to OS.</p>

<p>Now I want to add that specific android permission(<code>android.permission.CHANGE_CONFIGURATION</code>) to myapp.  </p>

<p>I read <a href="https://stackoverflow.com/questions/27889193/adding-own-framework-or-library-to-aosp">this</a>, <a href="http://karthiksden.blogspot.de/2011/03/copying-data-files-using-androidmk.html" rel="nofollow noreferrer">this</a> and many other documents, but I don't know the content of this XML file for an application; Or is it even possible?!<br>
(Does these links helpful to direct me in the right direction about content of XML file? <a href="https://github.com/InsomniaAOSP/platform_external_sepolicy/blob/master/tools/setool/tests/res/com/seandroid/tools/1.xml" rel="nofollow noreferrer">this</a> and <a href="https://android.googlesource.com/platform/external/sepolicy/+/b19665c39da76c0e24c8cd9583e30c4a50567510%5E!/" rel="nofollow noreferrer">this</a>)</p>

<hr>

<p>I tried another way, but didn't work(preinstall application and add permission by shell script:<br>
<strong>Note:</strong> First of all, I should say it worked before, on another custom AOSP, but didn't work on this one!</p>

<ol>
<li>Put APK in <code>/device/model/apps/HERE</code></li>
<li>Add this snippet code in Android.mk</li>
</ol>

<p><code>include $(CLEAR_VARS)
    LOCAL_MODULE := myapp.apk
    LOCAL_MODULE_TAGS := optional
    LOCAL_MODULE_CLASS := ETC
    LOCAL_MODULE_PATH := $(TARGET_OUT)/preinstall
    LOCAL_SRC_FILES := myapp.apk
include $(BUILD_PREBUILT)</code></p>

<p><code>include $(CLEAR_VARS)
    LOCAL_MODULE := preinstall.sh
    LOCAL_MODULE_TAGS := optional
    LOCAL_MODULE_CLASS := ETC
    LOCAL_MODULE_PATH := $(TARGET_OUT)/preinstall
    LOCAL_SRC_FILES := preinstall.sh
include $(BUILD_PREBUILT)</code></p>

<ol start="3">
<li>Content of preinstall.sh:  </li>
</ol>

<p><code>#!/system/bin/sh</code><br>
<code>MARK=/data/local/symbol_thirdpart_apks_installed
PKGS=/system/preinstall/</code><br>
<code>if [ ! -e $MARK ]; then
echo "booting the first time, so pre-install some APKs."</code><br>
<code>busybox find $PKGS -name "*\.apk" -exec sh /system/bin/pm install {} \;</code><br>
<code>touch $MARK
echo "OK, installation complete."
fi</code><br>
<code>busybox sh /system/bin/pm grant com.example.myapp android.permission.CHANGE_CONFIGURATION;</code></p>

<ol start="4">
<li>Call this shell script as service on boot in init.rc file, like:<br>
<code>on boot
start preinstallByMe</code></li>
</ol>

<p><code>service preinstallByMe /system/bin/sh /system/preinstall/preinstall.sh
    class main
    user root
    group root
    disabled
    oneshot</code></p>

<p>But seems it's not call.</p>

<ol start="4">
<li><p>Even these snippet codes in init.rc file not working too:  </p>

<ol>
<li><p><code>service installapk /system/preinstall/preinstall.sh
class main
oneshot</code></p></li>
<li><p><code>on boot
exec /system/preinstall/preinstall.sh</code></p></li>
<li><code>busybox /system/preinstall/preinstall.sh</code></li>
<li><code>pm grant com.example.myapp android.permission.CHANGE_CONFIGURATION;</code></li>
</ol></li>
</ol>

<p><strong>Note:</strong> If I call preinstall from shell manually, it's work.<br>
<strong>P.S:</strong> If your not allowed to call your script,  you can add permission to it by adding something like this in <code>/system/core/include/private/android_filesystem_config.h</code> :  </p>

<p><code>{ 00755, AID_ROOT,      AID_ROOT,      0, "system/preinstall/preinstall.sh"},</code></p>

<p>Cause second way(preinstall and add permission by shell), in this custom AOSP, doesn't work, I'm going to add that specific android permission to my app, from beginning,  via prebuilt; <strong>But</strong> if anyone knows what's wrong with the second solution, I'm appreciate it.</p>