Home  >  Article  >  Backend Development  >  Why is my Go Conditional Code Not Appending a Library to Apex_Defaults?

Why is my Go Conditional Code Not Appending a Library to Apex_Defaults?

DDD
DDDOriginal
2024-10-28 13:21:30834browse

Why is my Go Conditional Code Not Appending a Library to Apex_Defaults?

Go Conditional Code Not Appending Library to Apex

A developer has encountered an issue where a Go conditional implementation is not appending a library to apex_defaults. Below is the provided code and the proposed solution:

Go Code:

<code class="go">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("Condition 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)
}</code>

Issue:

The conditional code is not appending the library libabcextractor to apex_defaults. The logs indicate that the conditional statement is being executed, but the shared object file is not being generated.

Solution:

The issue lies in the unexported field native_shared_libs in the First struct within the props type used for reflection. In Go, reflection can only access exported struct fields, which are those starting with an uppercase letter.

To resolve this, the developer can change the native_shared_libs field to:

<code class="go">type props struct {
    Multilib struct {
        First struct {
           Native_shared_libs  []string
        }
    }
}</code>

The above is the detailed content of Why is my Go Conditional Code Not Appending a Library to Apex_Defaults?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn