简体   繁体   中英

How to develop Android System/Platform Apps

I'm working on a system application that is signed using the platform keys that I'm using to make my own ROM (from AOSP) but I have to do some workarounds in my code in order to use the platform classes and functions, when I try to use them directly Android Studio tells me that I'm trying to use a hidden API.

I want to write code like the Settings, Launcher, framework. I know their source that is how I'm writing my own but I have to invoke classes using reflection, trying to do the way they are written not even builds on Android Studio.

I don't want to use root commands to archive the functionality of my application that is why I'm doing this. The ROM I'm building will be installed without root access to the user (user build).

For example in Settings app, to enable MTP storage:

UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MTP, true);

But if I use that code in Android Studio, It is not even recognized as valid:

http://i.stack.imgur.com/R86OW.png (screenshot)

I have to do this way in order to archive that (it currently works this way):

UsbManager UsbManagerP = (UsbManager)getSystemService(Context.USB_SERVICE);
String setCurrentFunctionMethodName = "setCurrentFunction";
Method setCurrentFunctionMethod = null;
try {
    setCurrentFunctionMethod = UsbManagerP.getClass().getMethod(setCurrentFunctionMethodName, String.class, boolean.class);
    setCurrentFunctionMethod.invoke(UsbManagerP, "mtp",true);
    Log.d("OBS", "MTP ACTIVO");
} catch (NoSuchMethodException e) {
    Log.d("OBS", "Error No Existe Metodo");
} catch (InvocationTargetException e) {
    Log.d("OBS", "Error No Existe Objetivo");
} catch (IllegalAccessException e) {
    Log.d("OBS", "Error No Se puede acceder al metodo (illegal access)");
}

I really need help, maybe someone that has previously worked for Samsung/Sony/Motorola, etc. and share some knowledge about creating system apps.

As your application will be included in the ROM (and should be installed in /system + signed with the platform key) it would probably be simpler to compile the application in the AOSP source tree itself.

Doing this is pretty simple:

  • Copy the sources of your app into '$BUILD_ROOT/packages/apps/' (or add a reference to it into your own local_manifest to have 'repo sync' handle it)
  • Add an Android.mk file to your application. An example can be found here: https://github.com/adrian-bl/android_jpolly
  • Include the name of your app (Android.mk: LOCAL_PACKAGE_NAME) into your build $PRODUCT_PACKAGES (eg: device.mk -> PRODUCT_PACKAGES += UsbWhatever)

That's it: The Android build system will now automatically build your app as part of the normal build process, giving you access to all system features. The app will also automatically be stored in /system + signed with the platform key.

Note that you can do incremental builds of your app by running 'mm'.

Example:

$ . build/envsetup.sh
$ lunch <your device config>
$ cd packages/app/UsbWhatever
$ mm  # <-- this will rebuild your package

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM