Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

native library is not getting loaded to apex_defaults from golang conditional implementation?

So I wrote a go file which will dynamically append one library to apex_defaults-> multilib -> first -> native_shared_libs; full code of Android.bp can be checked here. However I can not see the compiled .so file in my out directory like other lib .so files generated.

Long Explanation:

I want to add a library named "libabcxtractor" to the array native_shared_libs; for that matter I wrote a .go file(as recommended by Google) with some condition which looks like this:

package my_apex

import (
    "android/soong/android"
    "android/soong/apex"
    "fmt"
    "strings"
)



func globalFlags(ctx android.BaseContext) []string {
        var native_shared_libs []string
        if(strings.Contains(ctx.AConfig().DeviceName(), "my_apex_device")){
                fmt.Println("Some log to verify condition is getting executed......")
                native_shared_libs = append(native_shared_libs, "libabcextractor")
        }

        return native_shared_libs
}

func myApexFlagsDefaults(ctx android.LoadHookContext) {
    type props struct {
                Multilib struct {
                        First struct {
                                native_shared_libs  []string
                        }
                }
        }
        p := &props{}
        p.Multilib.First.native_shared_libs = globalFlags(ctx)
        ctx.AppendProperties(p)
}

func myApexFlagsDefaultsFactory() android.Module {
        module := apex.DefaultsFactory()
        android.AddLoadHook(module, myApexFlagsDefaults)
        return module
}

func init() {
        fmt.Println("Registering module type....")
        android.RegisterModuleType("my_apex_defaults", myApexFlagsDefaultsFactory)
}

To enable above go condition to get picked at build time, I have updated my Android.bp file as below:

bootstrap_go_package {
   name: "soong-my_apex",
   pkgPath: "frameworks/av/apex/build",
   deps: [ "soong-apex" ],
   srcs: [ "my_apex.go", ],
   pluginFor: ["soong_build"],
}

my_apex_defaults {
   name: "my_apex",
}

apex_defaults {
    name: "com.android.media-defaults",
    java_libs: ["updatable-media"],
    defaults: ["my_apex",] //THIS IS TO INCLUDE GO IMPLEMENTATION 
    multilib: {

When executing make command I can see it enters the conditional statement; the logs are getting printed. I further investigated by verifying whether my .go file gets compiled by checking the path out/soong/.bootstrap/soong-my_apex/pkg/frameworks/av/apex/build.a. It's there. However ".so" file for libabcextractor is not being generated. Any help on this would be great.

Edit: libabcextractor is a vendor library which resides in Prebuilts path. If I try with any other library which is part of AOSP code and is in the existing native_static_libs array already, it is not working for them as well. From here on I am unable to debug further because of unavailability of proper documentations.

like image 789
TheLittleNaruto Avatar asked Dec 24 '19 10:12

TheLittleNaruto


1 Answers

Reflection in Go can only access exported struct fields, which are the ones that start with an uppercase letter. In your First struct, native_shared_libs is unexported and cannot be accessed using reflection.

Since the build process uses reflection to access the props struct, it cannot find information from it. You can change it like this:

type props struct {
    Multilib struct {
        First struct {
           Native_shared_libs  []string
        }
    }
}
like image 56
xarantolus Avatar answered Nov 20 '22 01:11

xarantolus