Android 13 Framework cropping

Crop application

1. Modify build/core/product.mk

Add declaration of PRODUCT_DEL_PACKAGES variable
Add a new line_product_single_value_vars + = PRODUCT_DEL_PACKAGES

# The first API level this product shipped with
_product_single_value_vars + = PRODUCT_SHIPPING_API_LEVEL
_product_single_value_vars + = PRODUCT_DEL_PACKAGES # Add

_product_list_vars + = VENDOR_PRODUCT_RESTRICT_VENDOR_FILES
_product_list_vars + = VENDOR_EXCEPTION_MODULES

2. Modify build/core/main.mk

Modify the definition of the product-installed-files function and add two lines:

$(eval _pif_dels :=

(

c

a

l

l

g

e

t

?

p

r

o

d

u

c

t

?

v

a

r

,

(call get-product-var,

(callget?product?var,(1),PRODUCT_DEL_PACKAGES))
$(eval _pif_modules := $(filter-out $(_pif_dels), $(_pif_modules))) \

 $(eval ### Filter out the overridden packages and executables before doing expansion) \
  $(eval _pif_overrides := $(call module-overrides,$(_pif_modules))) \
  $(eval _pif_modules := $(filter-out $(_pif_overrides), $(_pif_modules))) \
  $(eval _pif_dels := $(call get-product-var,$(1),PRODUCT_DEL_PACKAGES)) \
  $(eval _pif_modules := $(filter-out $(_pif_dels), $(_pif_modules))) \
  $(eval ### Resolve the :32 :64 module name) \

3. Add the PRODUCT_DEL_PACKAGES attribute to the corresponding device product mk file to control module deletion

The generally edited file path is device/company name/product name/device.mk, for example in rk3399:
device/rockchip/rk3399/device.mk

The simulator I use uses lunch sdk_pc_x86_64-userdebug. The file location is:
aosp/build/target/board/emulator_x86_64/device.mk

PRODUCT_SOONG_NAMESPACES + = device/generic/goldfish # for libwifi-hal-emu
PRODUCT_SOONG_NAMESPACES + = device/generic/goldfish-opengl # for goldfish deps.

ifdef NET_ETH0_STARTONBOOT
  PRODUCT_VENDOR_PROPERTIES + = net.eth0.startonboot=1
endif

# Ensure we package the BIOS files too.
PRODUCT_HOST_PACKAGES + = \
bios.bin \
vgabios-cirrus.bin \

# Application to be cropped
PRODUCT_DEL_PACKAGES + = \
    messaging \
    Dialer \
    Contacts \
    Email \
    Camera2 \
    Browser2 \
    Calendar \
    DocumentsUI \
    Music\
    MusicFX \
    ExactCalculator \

4. Compile

source build/envsetup.sh
# lunch sdk_pc_x86_64-userdebug # Emulator
lunch xxx-userdebug #xxx represents the corresponding product name
make

Tailoring compilation rules for Android12 system

Cutting Service, Printer Service

aosp/frameworks/base/services/java/com/android/server/SystemServer.java

1. SystemServer defines boolean variables and reads configuration from global properties

boolean disablePrinter = SystemProperties.getBoolean("config.disable_printer", false);

2. SystemServer determines whether to start the service based on attributes

if (!disablePrinter & amp; & amp; mPackageManager.hasSystemFeature(PackageManager.FEATURE_PRINTING)) {<!-- -->
    mSystemServiceManager.startService(PRINT_MANAGER_SERVICE_CLASS);
}

3. Add configuration parameters to device.mk

Add config.disable_printer configuration in PRODUCT_PROPERTY_OVERRIDES
Location: aosp/build/target/board/emulator_x86_64/device.mk

# Cropping application
PRODUCT_DEL_PACKAGES + = \
    messaging \
    Dialer\
    Contacts \
    Email \
    Camera2 \
    Browser2 \
    Calendar \
    DocumentsUI \
    Music \
    MusicFX \
    ExactCalculator \

# Is the printer service enabled?
PRODUCT_PROPERTY_OVERRIDES + = \
    config.disable_printer=true

5. Block SystemFeature

Find files containing ,
According to the readAllPermissions method of aosp/frameworks/base/core/java/com/android/server/SystemConfig.java, the printing feature configuration file will be obtained in the following folder.

  • /system/etc/permission
  • /system/etc/sysconfig
  • /vendor/etc/permission
  • /vendor/etc/sysconfig
  • /oem/etc/permission
  • /oem/etc/sysconfig
  • /odm/etc/permission
  • /odm/etc/sysconfig
  • /product/etc/permission
  • /product/etc/sysconfig
  • /system_ext/etc/permission
  • /system_ext/etc/sysconfig

Find the specific location, which is set in the /vendor/etc/permissions/handheld_core_hardware.xml file

cd /vendor/etc
find ./ -type f -exec grep -rn "software.print" {<!-- -->} \;

Search the source file again and find that it is in the frameworks/native/data/etc/handheld_core_hardware.xml file.
Comment out

find ./ -name "*.mk" -exec grep -rn "handheld_core_hardware" {<!-- -->} \;

6. Modification summary


build/target/board
![Insert picture description here](https://img-blog.csdnimg.cn/64c3934f4d8e4ab5b40a11a277c0c6b5.png#pic_center

aosp/build/core
aosp/frameworks/native/data/etc

7. Recompile and flash

Delete build.prop in the out directory and recompile system (or modify build.prop directly and then make snod)

Through the analysis of the build.prop generation process, it can be seen that buildinfo.sh;, system.prop;, ADDITIONAL_BUILD_PROPERTIES or PRODUCT_PROPERTY_OVERRIDES can be modified. It is recommended to change system.prop or PRODUCT_PROPERTY_OVERRIDES

8. Modify system files to change attribute values

emulator-writable-system
adb root
adb remount
adb disable-verity
adb reboot

# Wait until the phone restarts
adb root
adb remount

Modify the config.disable_printer attribute

adb pull /system/build.prop
# Modify the exported build.prop and add it at the end
####################################
# from variable PRODUCT_SYSTEM_DEFAULT_PROPERTIES
####################################
# Auto-added by post_process_props.py
persist.sys.usb.config=adb
config.disable_printer=false # Add
# end of file

adb push build.prop /system/build.prop
adb reboot

Modify the handheld_core_hardware.xml file

Location:/vendor/etc/permissions/handheld_core_hardware.xml

9. Test

adb shell service list # Service list
adb shell service check printer # Check whether the service is enabled

Android attribute build.prop generation process analysis
Cut out the SystemServer service and turn off SystemFeature

adb remount fails – mount: system’ not in /proc/mounts
About how to modify the parameters of the build.prop file in the system folder of the android system

lunch adds a new menu item

Android Framework Tutorial for Application Development – Adding Product to Play with AOSP
How to add a new menu item to AOSP’s lunch

Note that the content of the AndroidProducts.mk file is in rice14 case, otherwise Warning: Cannot display lunch menu.
The file BoardConfig.mk needs to be in the device/jelly/rice14/rice14/ directory.

PRODUCT_MAKEFILES := \
    $(LOCAL_DIR)/rice14.mk

COMMON_LUNCH_CHOICES := \
    rice14-eng \
    rice14-userdebug \
    rice14-user

Add C/C++ and Java executable programs, add C/C++ and Java libraries, and add system App source code

See
Android Framework Tutorial for Application Development
Android 13 has built-in third-party applications

Android #include file not found

The reason is that the JINI_H_INCLUDE definition is removed by default on S. If you want to completely modify it, you need to change the go file in the build path, which is more cumbersome. Another method is to modify the Android.bp or Android.mk file in the jni path.

# android.mk:
LOCAL_HEADER_LIBRARIES := jni_headers
# android.bp:
header_libs: ["jni_headers"],

See

ld.lld: error: undefined symbol: __android_log_print

Added in Android.bp

shared_libs: [
"liblog",
],
host_ldlibs: [
"-llog",
],

error: device/jelly/rice14/JNIApp/jni/Android.bp:1:1: dependency “libmymath” of “libmyjnilib” missing variant:

Because the Android.bp file of libmyjnilib is configured with sdk_version: “current”, but the Android.bp file of libmymath is not configured with sdk_version: “current”.

aosp/device/jelly/rice14/JNIApp/jni/Android.bp

cc_library_shared {<!-- -->
    name: "libmyjnilib",
    srcs: ["native.cpp"],
    static_libs: [
    ],
    shared_libs: [
        "liblog",
        "libmymath"
    ],
    header_libs: ["jni_headers"],
    host_ldlibs: [
        "-llog",
    ],
    cflags: [
        "-Wall",
        "-Werror",
    ],
    stl: "none",
    sdk_version: "current",
    product_specific: true,
}

aosp/device/jelly/rice14/libmymath/Android.bp

cc_library_shared {<!-- -->
    name: "libmymath",
    srcs: ["my_math.cpp"],
    export_include_dirs: ["."],
    sdk_version: "current",
    product_specific: true,
}

the APK is signed using APK Signature Scheme v2 but no such signature was found. Signature stripped?

android_app_import {<!-- -->

    name: "Xxxx",

    apk: "xxxx-5.3.3.apk",
    // presigned: true, // Use original signature
    certificate: "platform", // Specify the APK signature method
    dex_preopt: {<!-- --> // Turn off precompilation and no OAT file will be generated
        enabled: false,
    },
}

When integrating a third-party app, due to the use of presigned: true, there is a problem with the signature of the packaged apk file, which causes the App to fail to load when starting the Android system. The setting can be changed to certificate: “platform”.

# Signature verification
Sdk/build-tools/34.0.0/apksigner verify -v /data/aosp/out/target/product/rice14/system/app/Xxxx/Xxxx.apk

Add custom system services

1. Define AIDL

aosp/frameworks/base/core/java/android/bean/IBeanManager.aidl
aosp/frameworks/base/core/java/android/bean/Person.aidl

2. Generate binder related files

make update-api
make framework

3. Implement Server side

aosp/frameworks/base/services/core/java/com/android/server/bean/BeanManagerService.java

BeanManagerService extends IBeanManager.Stub

You can use Lifecycle to inherit aosp/frameworks/base/services/core/java/com/android/server/SystemService.java to manage the life cycle

4. Define service management class

aosp/frameworks/base/core/java/android/bean/BeanManager.java

5. Configure services to the system

aosp/frameworks/base/core/java/android/content/Context.java

Definition: public static final String BEAN_SERVICE = “bean”;
Add ServiceName: @StringDef(suffix = { “_SERVICE” }, value ={…BEAN_SERVICE…} )

aosp/frameworks/base/core/java/android/app/SystemServiceRegistry.java

6. Register the service and configure startup

Registration service
aosp/frameworks/base/core/java/android/app/SystemServiceRegistry.java

 registerService(Context.BEAN_SERVICE, BeanManager.class,
                new CachedServiceFetcher<BeanManager>() {<!-- -->
                    @Override
                    public BeanManager createService(ContextImpl ctx) throws ServiceNotFoundException {<!-- -->
                        IBinder b = ServiceManager.getServiceOrThrow(Context.BEAN_SERVICE);
                        IBeanManager service = IBeanManager.Stub.asInterface(b);
                        return new BeanManager(ctx, service);
                    }});

boot:
aosp/frameworks/base/services/java/com/android/server/SystemServer.java

Add startup in the startOtherServices method

 mSystemServiceManager.startService(BeanManagerService.Lifecycle.class);

7. Configure Selinux permissions

data/aosp/device/jelly/rice14/sepolicy/common/service.te
Define type:
type bean_service,service_manager_type;

aosp/device/jelly/rice14/sepolicy/common/service_contexts
Define context:
bean u:object_r:bean_service:s0

aosp/device/jelly/rice14/sepolicy/common/system_server.te
Set rules:
allow system_server bean_service:service_manager { add };
allow priv_app bean_service:service_manager { find };

8. Client call

aosp/packages/apps/Launcher3/src/com/android/launcher3/Launcher.java

BeanManager beanManager = (BeanManager) getSystemService(Context.BEAN_SERVICE);

The above service runs in the system_server process. If it needs to run in an independent process,
You need to create an App and add services in Application. There is no need to start the service in startOtherServices.

 MelonManagerService mService = new MelonManagerService(getApplicationContext());
ServiceManager.addService(Context.MELON_SERVICE, mService, false, DUMP_FLAG_PRIORITY_DEFAULT);

Configuration parameters in App manifest

android:sharedUserId="android.uid.system"
coreApp="true"

android:directBootAware="true"
android:persistent="true"

Reference

Android 13 adds custom Java system services

Detailed explanation about Selinux
Introduction to SeLinux permissions
android Selinux platform_app, system_app, priv_app, untrusted_app

After Selinux is compiled, quickly verify

make selinux_policy

adb push $(PRODUCT_OUT)/system/etc/selinux /system/etc/
adb push $(PRODUCT_OUT)/vendor/etc/selinux /vendor/etc/
adb reboot

Others

# Add to whitelist
PRODUCT_ARTIFACT_PATH_REQUIREMENT_ALLOWED_LIST + = \
system/app/Xxxx/Xxxx.apk \

# Join application
PRODUCT_PACKAGES + = \
hello \
busybox \
Hellojava \
FirstSystemApp\
JNIApp \
Xxxx \

Common commands

adb shell pm list package -f # Application and corresponding package
find QSSI.12/out/target/product/qssi/ -name QtiDialer | xargs rm -rf # Find and delete files
The corresponding relationship between Android.bp and Android.mk is in the file:
aosp/build/soong/androidmk/androidmk/android.go
Module make command mmm command
init make init mmm system/core/init
zygote make app_process mmm frameworks/base/cmds/app_process
system_server make services mmm frameworks/base/ services
java framework make framework-minus-apex mmm frameworks/base
res framework make framework-res mmm frameworks/base/core/res
jni framework make libandroid_runtime mmm frameworks/base/core/jni
binder make libbinder mmm frameworks/native/libs/binder

We can also use the allmod command to view all modules, and then use the make command to compile the modules we need.

Android compilation make basics
Android10.0 compilation system
Where is the android device.mk file?