Including Library in AOSP/frameworks/base/core

1,362 views
Skip to first unread message

Yolo Pucky

unread,
Jun 8, 2021, 10:26:53 PM6/8/21
to android-...@googlegroups.com
Dear Android Building Group,
 
I am currently trying to extend the network security configuration to apply certificate transparency by default. Therefore I am extending the AOSP/frameworks/base/core/java/android/net/config directory.
I would also like to use some external libraries within this directory, which turned out to be a big problem for me. For a concrete example, let's say I want to use the gson library.
 
First I will provide my way of implementing the gson library for AOSP/frameworks/base/core/java/android/net/config (1). The provided way should be the closest to a working implementation, still, it breaks on boot. After that, I will provide a very similar implementation for using gson in AOSP/frameworks/base/services/core/java/com/android/server (2), which works fine.
 
Questions:
1. Why does (1) break and how can I fix it?
 
Specifications:
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=11
TARGET_PRODUCT=aosp_x86_64
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_ARCH=x86_64
TARGET_ARCH_VARIANT=x86_64
TARGET_2ND_ARCH=x86
TARGET_2ND_ARCH_VARIANT=x86_64
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.19.0-10-amd64-x86_64-Debian-GNU/Linux-10-(buster)
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=RQ2A.210405.005
OUT_DIR=out
PRODUCT_SOONG_NAMESPACES=device/generic/goldfish device/generic/goldfish-opengl hardware/google/camera
 hardware/google/camera/devices/EmulatedCamera device/generic/goldfish device/generic/goldfish-opengl
 
Implementation (1):
+ AOSP/prebuilts/misc/common/gson
  + Android.pb
  + gson-2.8.7.jar
 

AOSP/prebuilts/misc/common/gson/Android.pb
__________
+ java_import {
+     name: "gson",
+     installable: false,
+     host_supported: true,
+     jars: ["gson-2.8.7.jar"],
+ }
__________
 
 
AOSP/frameworks/base/Android.bp
__________
...
java_library {
    name: "framework-minus-apex",
    defaults: ["framework-defaults"],
    srcs: [":framework-non-updatable-sources"],
    installable: true,
    javac_shard_size: 150,
    required: [
        "framework-platform-compat-config",
        "libcore-platform-compat-config",
        "services-platform-compat-config",
        "documents-ui-compat-config",
    ],
+    libs: ["framework-updatable-stubs-module_libs_api","gson",],
...
__________
 
 
AOSP/frameworks/base/core/java/android/net/config/NetworkSecurityTrustManager.java
__________
...
import com.google.gson.Gson;
...
public void checkserverTrusted(...)...{
  ...
+  android.util.Log.d(TAG,"About to instantiate Gson object.");
+  Gson gson = new Gson();
+  android.util.Log.d(TAG,"Gson object has been successfully instantiated.");
  ...
}
...
__________
 
 
Building the source is no problem. However, running the artifacts crashes the system:
...
05-31 12:25:23.842 792 1374 D TAG: About to instantiate Gson object.
--------- beginning of crash
05-31 12:25:23.847 792 1374 E AndroidRuntime: FATAL EXCEPTION: Thread-4
05-31 12:25:23.847 792 1374 E AndroidRuntime: Process: com.android.networkstack.process, PID: 792
05-31 12:25:23.847 792 1374 E AndroidRuntime: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
...
 
 
 
 
Implementation (2):
+ AOSP/prebuilts/misc/common/gson
  + Android.pb
  + gson-2.8.7.jar
 

AOSP/prebuilts/misc/common/gson/Android.pb
__________
+ java_import {
+     name: "gson",
+     installable: false,
+     host_supported: true,
+     jars: ["gson-2.8.7.jar"],
+ }
__________
 
 
AOSP/frameworks/base/services/core/Android.bp
__________
 
java_library_static {
    name: "services.core.unboosted",
    ...
    static_libs: [
+        "gson",
        "time_zone_distro",
        "time_zone_distro_installer",
        "android.hardware.authsecret-V1.0-java",
...
__________
 
 
AOSP/frameworks/base/services/core/java/com/android/server/am/ActiveServices.java
__________
...
import com.google.gson.Gson;
...
public ActiveServices(ActivityManagerService service) {
    mAm = service;
    int maxBg = 0;
    try {
        maxBg = Integer.parseInt(SystemProperties.get("ro.config.max_starting_bg", "0"));
    } catch(RuntimeException e) {
    }
    mMaxStartingBackground = maxBg > 0
            ? maxBg : ActivityManager.isLowRamDeviceStatic() ? 1 : 8;
+
+   android.util.Log.d(TAG,"About to instantiate Gson object.");
+   Gson gson = new Gson();
+   android.util.Log.d(TAG,"Gson object has been successfully instantiated.");
}
...
 
For (2) everything works as expected. And both logs can be observed in logcat.
 
Can you please help me to fix (1) or at least to understand the problem?
 
Thanks a lot!
 
 
Regards,
John

Yolo Pucky

unread,
Jun 23, 2021, 1:10:07 AM6/23/21
to Android Building
So I actually got a solution to my problem mentioned on 09.06.2021.
However, I have another problem now, that is closely related to the aforementioned.

-------------------------------------------------------------------------------------------------------------------------------------------
BEGINNING OF SOLUTION
-------------------------------------------------------------------------------------------------------------------------------------------
First here is my solution to my problem from 09.06.2021:
The blueprint in AOSP/prebuilts/misc/common/gson/Android.bp remains.

AOSP/frameworks/base/Android.bp
...
java_library {
name: "framework-minus-apex",
...

libs: [
"framework-updatable-stubs-module_libs_api",
"gson",
],
static_libs: [
// If MimeMap ever becomes its own APEX, then this dependency would need to be removed
// in favor of an API stubs dependency in java_library "framework" below.
"mimemap",
"gson",
],
...
}
...

AOSP/build/make/core/tasks/check_boot_jars/package_allowed_list.txt
# Boot jar package name allowed list.
# Each line is interpreted as a regular expression.
...
com\.google\.gson\..*
com\.google\.gson.*
...
-------------------------------------------------------------------------------------------------------------------------------------------
END OF SOLUTION
-------------------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------------
BEGINNING OF SUBSEQUENT PROBLEM
-------------------------------------------------------------------------------------------------------------------------------------------
First, note that I exchanged the Gson library with the Jackson library by now.
Building custom Android and using the Jackson library is no problem anymore. Though I only want to use the library within the AOSP i.e. I do not want third-party apps installed on my custom Android to see those libraries.
The way I include the library leads to the problem that third-party apps that import classes or methods from these libraries will always try to import them from the Android library instead of the library the apps bring themselves.
Since the library I include within my custom Android might differ from the library brought by the third-party apps, it does not necessarily contain all required classes or methods for those apps. Subsequently, these apps break:
// Caused by: java.lang.NoSuchMethodError: No interface method getterVisibility()LX/15g; in class Lcom/fasterxml/jackson/annotation/JsonAutoDetect; or its super classes (declaration of 'com.fasterxml.jackson.annotation.JsonAutoDetect' appears in /system/framework/framework.jar!classes4.dex)

How can I make my included library within the framework.jar only visible to AOSP internals?

-------------------------------------------------------------------------------------------------------------------------------------------
END OF SUBSEQUENT PROBLEM
-------------------------------------------------------------------------------------------------------------------------------------------

Thanks again!

Regards,
John

Dan Willemsen

unread,
Jun 23, 2021, 1:46:58 AM6/23/21
to android-...@googlegroups.com
AOSP/build/make/core/tasks/check_boot_jars/package_allowed_list.txt

Be careful if you're planning on being compatible with the CDD, particularly section 3.6. Adding new classes to the bootclasspath can cause compatibility issues with apps, which is why that check exists (and is your subsequent problem).

One method mentioned in the CDD to work around this is to add your new packages to a separate library that apps can opt into. That doesn't really work if you're using it in modified framework classes that get loaded into every app though. There's one example with bouncycastle that gets repackaged to be inside a different namespace in order to not conflict with apps. That may be possible with jarjar too (which the build system has support for), I'm not sure what the scripts used for bouncycastle are doing differently.

- Dan

--
--
You received this message because you are subscribed to the "Android Building" mailing list.
To post to this group, send email to android-...@googlegroups.com
To unsubscribe from this group, send email to
android-buildi...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-building?hl=en

---
You received this message because you are subscribed to the Google Groups "Android Building" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-buildi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-building/014b3755-da4e-4cfc-ad23-623957c6dda6n%40googlegroups.com.

Yolo Pucky

unread,
Jun 29, 2021, 7:31:28 PM6/29/21
to Android Building
This is exactly what I did and it works. Thanks for the help!
Reply all
Reply to author
Forward
0 new messages