From 3070d7cb82d5a97ef9cd18fc433b3bf3c9af6012 Mon Sep 17 00:00:00 2001 From: Hai Zhang Date: Fri, 11 Nov 2022 21:48:44 -0800 Subject: [PATCH] Add services.permission java_library_static with Kotlin. The permission and app op subsystems turned out to have a lot more hidden references throughout the system server and it's infeasible to define system APIs for all those references in U. So to allow utilizing the new unified subsystem for them, we need to keep the new subsystem in the non-updatable platform for now. The Kotlin usage for the new subsystem was approved for the APEX system server JAR inside Permission Mainline module, and we can achieve the same criteria with a standalone Java library module in the non-updatable platform, so that Kotlin is still jarjar'ed, shrunk and only available within this module. A caveat is that because R8 converts Java byte code to Dalvik byte code, it can only be run when producing the final services.jar, instead of directly when compiling this new java_library_static module. Fortunately, services.jar has already enabled R8 shrinking when SYSTEM_OPTIMIZE_JAVA is enabled, and when it's disabled due to dependent JARs, we can still safely confine the shrinking within the new module with a negated rule since no other JARs can depend on the new code. The BloatBuster check showed that on AAOS (with SYSTEM_OPTIMIZE_JAVA disabled), the size of services.odex increased by 1 MB (21.3 MB -> 22.4 MB), but looking at installed-files.json from its build output, the size of services.jar actually decreased by 2 MB (21.8 MB -> 19.4 MB). Since both of them are pinned in memory, and the new ODEX will actually have better performance due to better optimization, it's still a net improvement from the current situation. More details about this is available at b/258943585#comment14. An alternative is to create a standalone system server JAR that's also loaded into SSCP. However, that approach is said to be causing a slight boot time regression in b/155631167. Bug: 182523293 Test: presubmit Test: AyeAye BloatBuster Test: CtsStrictJavaPackagesTestCases Change-Id: Id35e19b941e9abbba0d039f993bbf4ec183554ab --- services/Android.bp | 17 ++++++-- services/permission/Android.bp | 40 +++++++++++++++++++ services/permission/OWNERS | 4 ++ services/permission/jarjar-rules.txt | 1 + .../ModernPermissionManagerServiceImpl.kt | 26 ++++++++++++ services/proguard_permission.flags | 9 +++++ 6 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 services/permission/Android.bp create mode 100644 services/permission/OWNERS create mode 100644 services/permission/jarjar-rules.txt create mode 100644 services/permission/java/com/android/server/permission/ModernPermissionManagerServiceImpl.kt create mode 100644 services/proguard_permission.flags diff --git a/services/Android.bp b/services/Android.bp index 76a1484195065..f6570e9d27027 100644 --- a/services/Android.bp +++ b/services/Android.bp @@ -60,9 +60,17 @@ system_optimized_java_defaults { ignore_warnings: false, proguard_flags_files: ["proguard.flags"], }, - // Note: Optimizations are disabled by default if unspecified in - // the java_library rule. - conditions_default: {}, + conditions_default: { + optimize: { + enabled: true, + optimize: false, + shrink: true, + ignore_warnings: false, + // Note that this proguard config is very conservative, only shrinking the + // permission subpackage to prune unused jarjar'ed Kotlin dependencies. + proguard_flags_files: ["proguard_permission.flags"], + }, + }, }, }, } @@ -97,6 +105,7 @@ filegroup { ":services.midi-sources", ":services.musicsearch-sources", ":services.net-sources", + ":services.permission-sources", ":services.print-sources", ":services.profcollect-sources", ":services.restrictions-sources", @@ -131,6 +140,7 @@ java_library { app_image: true, profile: "art-profile", }, + exclude_kotlinc_generated_files: true, srcs: [":services-main-sources"], @@ -152,6 +162,7 @@ java_library { "services.musicsearch", "services.net", "services.people", + "services.permission", "services.print", "services.profcollect", "services.restrictions", diff --git a/services/permission/Android.bp b/services/permission/Android.bp new file mode 100644 index 0000000000000..b03f17b1bef7b --- /dev/null +++ b/services/permission/Android.bp @@ -0,0 +1,40 @@ +package { + // See: http://go/android-license-faq + // A large-scale-change added 'default_applicable_licenses' to import + // all of the 'license_kinds' from "frameworks_base_license" + // to get the below license kinds: + // SPDX-license-identifier-Apache-2.0 + default_applicable_licenses: ["frameworks_base_license"], +} + +filegroup { + name: "services.permission-sources", + srcs: [ + "java/**/*.java", + "java/**/*.kt", + ], + path: "java", + visibility: ["//frameworks/base/services"], +} + +java_library_static { + name: "services.permission", + defaults: ["platform_service_defaults"], + srcs: [":services.permission-sources"], + libs: [ + "services.core", + // Soong fails to automatically add this dependency because all the + // *.kt sources are inside a filegroup. + "kotlin-annotations", + ], + static_libs: [ + "kotlin-stdlib", + ], + jarjar_rules: "jarjar-rules.txt", + kotlincflags: [ + "-Xjvm-default=all", + "-Xno-call-assertions", + "-Xno-param-assertions", + "-Xno-receiver-assertions", + ], +} diff --git a/services/permission/OWNERS b/services/permission/OWNERS new file mode 100644 index 0000000000000..6c6c9fc10d3bf --- /dev/null +++ b/services/permission/OWNERS @@ -0,0 +1,4 @@ +ashfall@google.com +joecastro@google.com +ntmyren@google.com +zhanghai@google.com diff --git a/services/permission/jarjar-rules.txt b/services/permission/jarjar-rules.txt new file mode 100644 index 0000000000000..34af3afabc4f7 --- /dev/null +++ b/services/permission/jarjar-rules.txt @@ -0,0 +1 @@ +rule kotlin.** com.android.server.permission.jarjar.@0 diff --git a/services/permission/java/com/android/server/permission/ModernPermissionManagerServiceImpl.kt b/services/permission/java/com/android/server/permission/ModernPermissionManagerServiceImpl.kt new file mode 100644 index 0000000000000..21ec1593fdeb3 --- /dev/null +++ b/services/permission/java/com/android/server/permission/ModernPermissionManagerServiceImpl.kt @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.permission + +import com.android.internal.annotations.Keep +import com.android.server.pm.permission.PermissionManagerServiceInterface + +/** + * Modern implementation of [PermissionManagerServiceInterface]. + */ +@Keep +class ModernPermissionManagerServiceImpl diff --git a/services/proguard_permission.flags b/services/proguard_permission.flags new file mode 100644 index 0000000000000..15edc61c33b28 --- /dev/null +++ b/services/proguard_permission.flags @@ -0,0 +1,9 @@ +# Only shrink services.permission classes. +# Note that while more aggressive services shrinking is enabled by default (see proguard.flags), for +# cases where that's not yet possible, we still need to shrink the permission package to prune out +# unused Kotlin stdlib dependencies. +-keep class !com.android.server.permission.** { *; } + +# CoverageService guards optional jacoco class references with a runtime guard, so we can safely +# suppress build-time warnings. +-dontwarn org.jacoco.agent.rt.*