目录
前言
在android framework开发中,OEM 一般会封装一些定制库文件,几乎在系统中所有定制的模块和应用中都会依赖这些库文件,所以在每个Android.mk或Android.bp等编译文件中都需要添加库依赖。这样就比较麻烦,而系统原生framework.jar却是不需要在每个编译文件中进行添加就可以默认依赖。所以可以参照系统framework的处理方式,在编译系统中进行修改来实现此功能。
此功能基于Android12实现。
1.定制库的创建
1.先创建一个自定义的库(我是自己随便创建了一个库,一般oem都是有自己定制库的)
2.然后随便写了一行代码
package com.hoowi.example;
public class MyExample {
public static final int INT = 1;
}
3.编写Android.bp文件
java_library {
name: "hoowi.example",
installable: true,
srcs: [
"com/hoowi/example/MyExample.java",
],
}
2.修改编译工具代码并解决报错。
1.修改build/soong/java/config/config.go文件,
var (
pctx = android.NewPackageContext("android/soong/java/config")
LegacyCorePlatformBootclasspathLibraries = []string{"legacy.core.platform.api.stubs", "core-lambda-stubs"}
LegacyCorePlatformSystemModules = "legacy-core-platform-api-stubs-system-modules"
StableCorePlatformBootclasspathLibraries = []string{"stable.core.platform.api.stubs", "core-lambda-stubs"}
StableCorePlatformSystemModules = "stable-core-platform-api-stubs-system-modules"
//在这里添加自定义库文件 hoowi.example
FrameworkLibraries = []string{"ext", "framework", "hoowi.example"}
DefaultLambdaStubsLibrary = "core-lambda-stubs"
SdkLambdaStubsPath = "prebuilts/sdk/tools/core-lambda-stubs.jar"
DefaultMakeJacocoExcludeFilter = []string{"org.junit.*", "org.jacoco.*", "org.mockito.*"}
DefaultJacocoExcludeFilter = []string{"org.junit.**", "org.jacoco.**", "org.mockito.**"}
InstrumentFrameworkModules = []string{
"framework",
"framework-minus-apex",
"telephony-common",
"services",
"android.car",
"android.car7",
"conscrypt",
"core-icu4j",
"core-oj",
"core-libart",
// TODO: Could this be all updatable bootclasspath jars?
"updatable-media",
"framework-mediaprovider",
"framework-sdkextensions",
"android.net.ipsec.ike",
//在这里添加自定义库文件 hoowi.example
"hoowi.example",
}
)
2.编译会报depends on undefined module的错误
3.这个是因为在go编译系统中的测试代码中没有添加模块定义,
修改build/soong/java/testing.go
func gatherRequiredDepsForTest() string {
var bp string
extraModules := []string{
"core-lambda-stubs",
"ext",
"android_stubs_current",
"android_system_stubs_current",
"android_test_stubs_current",
"android_module_lib_stubs_current",
"android_system_server_stubs_current",
"core.current.stubs",
"legacy.core.platform.api.stubs",
"stable.core.platform.api.stubs",
"kotlin-stdlib",
"kotlin-stdlib-jdk7",
"kotlin-stdlib-jdk8",
"kotlin-annotations",
//这里添加测试模块定义
"hoowi.example",
}
for _, extra := range extraModules {
bp += fmt.Sprintf(`
java_library {
name: "%s",
srcs: ["a.java"],
sdk_version: "none",
system_modules: "stable-core-platform-api-stubs-system-modules",
compile_dex: true,
}
`, extra)
}
//........省略若干行
return bp
}
4.再次编译又会报depends on itself的错。
5.这个时候要重新修改自定义模块的Android.bp文件加入一行 sdk_version: "core_platform",
java_library {
name: "hoowi.example",
installable: true,
sdk_version: "core_platform",//就是这一行代码
srcs: [
"com/hoowi/example/MyExample.java",
],
}
6.再次编译又报错
7.这个时候修改build/soong/java/java_test.go
func TestPatchModule(t *testing.T) {
//......省略若干行
t.Run("Java language level 9", func(t *testing.T) {
// Test with default javac -source 9 -target 9
bp := `
java_library {
name: "foo",
srcs: ["a.java"],
}
java_library {
name: "bar",
srcs: ["b.java"],
sdk_version: "none",
system_modules: "none",
patch_module: "java.base",
}
java_library {
name: "baz",
srcs: [
"c.java",
// Tests for b/150878007
"dir/d.java",
"dir2/e.java",
"dir2/f.java",
"nested/dir/g.java"
],
patch_module: "java.base",
}
`
ctx, _ := testJava(t, bp)
checkPatchModuleFlag(t, ctx, "foo", "")
expected := "java.base=.:out/soong"
checkPatchModuleFlag(t, ctx, "bar", expected)
//在这里添加自定义库(需要注意的是这里的库的顺序要与config.go中添加自定义库的顺序一致,不然也会报错)
expected = "java.base=" + strings.Join([]string{
".", "out/soong", "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework"),
defaultModuleToPath("hoowi.example")}, ":")
checkPatchModuleFlag(t, ctx, "baz", expected)
})
}
8.nice,终于编译通过了。
3.其他模块引用库内变量进行测试
随便找一个库或者app引用下,可以引用到,编译可以通过。
nice,到此功能就实现了。