Hi Sir,
I am new to iMX and Android AOSP world and studying the Android System.
I am using iMX8m mini board and i am able to download and build AOSP for Android 9 for this board.
Now i want to add simple custom system service to this AOSP but i don't know how to do this.
I followed the discussion -
https://community.nxp.com/message/606552?commentID=606552#comment-606552
and checked the below link which he was mentioning -
shridutt kothari's blog: Adding New System Service in Android
Android-Adding SystemService - Texas Instruments Wiki
I also checked this link -
AOSP – Creating a System Service – Developers Area
But it seems link these steps are for older Android versions. And I am using Android 9.
Please suggest me how can add a simple system service for Android 9 and for my iMX8m Mini board.
Thanks & Regards,
Maneesh
Hi Diego Adrian Cuevas, and Team NXP,
As I mentioned in my original question [ here ] what all changes and steps I did to add a custom service in Android 9.0 AOSP
I am referring below links for Adding custom service in AOSP but unfortunately they are very old and using AOSP 5 or 6 or 8 version.
I am using AOSP Android 9.0 version and NXP iMX8M Mini EVK board.
Links which i am referring =>
https://community.nxp.com/message/606552?commentID=606552#comment-606552
shridutt kothari's blog: Adding New System Service in Android
This is based on AOSP 6 version
http://haohaochang.cn/2017/10/27/custom-android-system-service/
This is based on AOSP 8 version
https://devarea.com/aosp-creating-a-system-service/#.XXIA1XUzY8o
In all the above link I found that for AOSP Version 6 files and directories mentioned to add selinux policy for custom service is completely different
and not present in AOSP version 9. And files and directories mentioned in AOSP 8 is matching with AOSP 9 but few files are missing in AOSP 9 version.
My Observation -
With All the changes I am able to rebuild the AOSP and I am able to see my App but when i try to access my custom service from my app then it is failed to discover / get my system service. it means My system service is not running or not discoverable to my app.
Please suggest me -
1) Apart from above mentioned steps [ here ] which i performed, is there any other steps which I need to did for my custom service in AOSP ?
2) Steps to add SE Linux Permissions for custom service for Android 9.0 AOSP.
Thanks & Regards,
Maneesh Singh
Hi,
For Adding a system service i did following steps -
NOTE - I am trying to add my own system service named - gea to AOSP Android 9 framework.
Adding system services to AOSP framework
#####################################################
System Services
##############################
1. Copy GeaService.java to
AOSP frameworks/base/services/core/java/com/android/server of your AOSP
GeaService.java
#################
package com.android.server;
import android.content.Context;
import android.os.Handler;
import android.os.IGeaService;;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.util.Log;
public class GeaService extends IGeaService.Stub {
private static final String TAG = "GeaService";
private Context mContext;
private long mNativePointer;
private int mValue = 0;
public GeaService(Context context) {
super();
mContext = context;
Log.i(TAG, "System service initialized");
mNativePointer = init_native();
Log.i(TAG, "test() returns " + test_native(mNativePointer, 20));
write("HelloFromGEAService");
Log.i(TAG, "read() returns " + read(50));
}
protected void finalize() throws Throwable {
finalize_native(mNativePointer);
super.finalize();
}
public String read(int maxLength)
{
int length;
byte[] buffer = new byte[maxLength];
length = read_native(mNativePointer, buffer);
return new String(buffer, 0, length);
}
public int write(String mString)
{
byte[] buffer = mString.getBytes();
return write_native(mNativePointer, buffer);
}
private static native long init_native();
private static native void finalize_native(long ptr);
private static native int read_native(long ptr, byte[] buffer);
private static native int write_native(long ptr, byte[] buffer);
private static native int test_native(long ptr, int value);
public void test(int val) {
Log.i(TAG, "test " + val);
mValue = val;
}
}
2. Add below lines in
AOSP frameworks/base/services/java/com/android/server/SystemServer.java
import com.android.server.GeaService;
import android.os.GeaManager;
try {
Slog.i(TAG, "GEA Service");
ServiceManager.addService(Context.GEA_SERVICE, new GeaService(context));
} catch (Throwable e) {
Slog.e(TAG, "Failure starting Gea Service", e);
}
3. Copy IGeaService.aidl to
AOSP frameworks/base/core/java/android/os/
package android.os;
/**
* {@hide}
*/
interface IGeaService {
String read(int maxLength);
int write(String mString);
void test(int val);
}
4. Copy GeaManager.java to
AOSP frameworks/base/core/java/android/os/
package android.os;
import android.os.IGeaService;
import android.util.Log;
public class GeaManager
{
IGeaService mService;
private static final String TAG = "GeaManager"; // this is for Log only..
public String read(int maxLength) {
try {
return mService.read(maxLength);
} catch (RemoteException e) {
Log.e(TAG, "Excpetion in read: " + e.toString());
return null;
}
}
public int write(String mString) {
try {
return mService.write(mString);
} catch (RemoteException e) {
Log.e(TAG, "Excpetion in write: " + e.toString());
return 0;
}
}
public void test(int val) {
try {
mService.test(val);
return;
} catch (RemoteException e) {
Log.e(TAG, "Excpetion in test: " + e.toString());
return;
}
}
public GeaManager(IGeaService service) {
mService = service;
}
}
5. Add below line to -> AOSP framework/base/Android.bp
"core/java/android/os/IGeaService.aidl",
6. Add below lines to AOSP frameworks/base/core/java/android/app/SystemServiceRegistry.java
import android.os.IGeaService;
import android.os.GeaManager;
registerService(Context.GEA_SERVICE, GeaManager.class,
new CachedServiceFetcher<GeaManager>() {
@Override
public GeaManager createService(ContextImpl ctx) throws ServiceNotFoundException {
IBinder b = ServiceManager.getServiceOrThrow(Context.GEA_SERVICE);
IGeaService service = IGeaService.Stub.asInterface(b);
return new GeaManager(service);
}});
7. Add below lines to AOSP frameworks/base/core/java/android/app/ContextImpl.java
import android.os.IGeaService;
import android.os.GeaManager;
8. Add below lines to AOSP frameworks/base/core/java/android/content/Context.java
/**
* Use with {@link #getSystemService} to retrieve a
* {@link android.os.GeaManager} for using Gea Service.
*
* @see #getSystemService
*/
public static final String GEA_SERVICE = "gea";
But In my Android App (This App I am building inside AOSP / package / app ) when I try to get this
system service i always get toast msg - "FAILED to call system service".
package com.gea.hellointernal;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import android.os.ServiceManager; // Will only work in AOSP
import android.os.IGeaService; // Interface "hidden" in SDK
import android.os.GeaManager;
public class HelloGeaInternalActivity extends Activity {
private static final String DTAG = "HelloGeaInternal";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IGeaService om = IGeaService.Stub.asInterface(ServiceManager.getService("gea"));
try {
Log.d(DTAG, "Going to write to the \"gea\" service");
int ret = om.write("Hello World!");
Log.d(DTAG, "GEAManager.read returned: " + om.read(20) + " ret = " + ret);
Toast.makeText(getApplicationContext(),"Going to write to the service",Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Log.d(DTAG, "FAILED to call service : " + e.toString());
Toast.makeText(getApplicationContext(),"FAILED to call system service : "+e.getMessage(),Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
NOTE - With All the changes I am able to rebuild the AOSP and I am able to see my App but when i open the App i always see
FAILED to call system service toast msg... it means My system service is not working or not discoverable to my app.
I am not understanding what I am missing or what changes i need to do to make my system service available in my app.
Any help or suggestion for what wrong I am doing here or how can i write simple custom system service and test it for Android 9 framework.
Note - I am using iMX8M Mini EVK and Android AOSP 9
Thanks & Regards,
Maneesh
Hello,
I have found the below information. Probably it can be helpful to you.
https://devarea.com/aosp-creating-a-system-service/#.XUC7KehKiUk
https://developer.android.com/guide/components/services
There you can see that is not only adding the java service, it is also needed to be done other things.
Hope this information can help you.
Best regards,
Diego.
Thanks for your reply.
I checked the Link you have provided and as you can see in my post above i have listed all the steps and changes which i did for system services.
i didn't did any changes for selinux as i am not sure how this will work.
please check my steps above and let me know what i am missing except selinux changes.
Also for Android 9 which i am using selinux has 28.0 directory instead of 26.0 directory as mentioned in the link you have provided.
Thanks & Regards,
Maneesh