Debian Codegraph @

internal/binres/sdk.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package binres
6
7 import (
8 "archive/zip"
9 "bytes"
10 "compress/gzip"
11 "fmt"
12 "io"
13 "os"
14 "path/filepath"
15
16 "golang.org/x/mobile/internal/sdkpath"
17 )
18
19 // MinSDK is the targeted sdk version for support by package binres.
20 const MinSDK = 16
21
22 func apiResources() ([]byte, error) {
23 apiResPath, err := apiResourcesPath()
24 if err != nil {
25 return nil, err
26 }
27 zr, err := zip.OpenReader(apiResPath)
28 if err != nil {
29 if os.IsNotExist(err) {
30 return nil, fmt.Errorf(`%v; consider installing with "android update sdk --all --no-ui --filter android-%d"`, err, MinSDK)
31 }
32 return nil, err
33 }
34 defer zr.Close()
35
36 buf := new(bytes.Buffer)
37 for _, f := range zr.File {
38 if f.Name == "resources.arsc" {
39 rc, err := f.Open()
40 if err != nil {
41 return nil, err
42 }
43 _, err = io.Copy(buf, rc)
44 if err != nil {
45 return nil, err
46 }
47 rc.Close()
48 break
49 }
50 }
51 if buf.Len() == 0 {
52 return nil, fmt.Errorf("failed to read resources.arsc")
53 }
54 return buf.Bytes(), nil
55 }
56
57 func apiResourcesPath() (string, error) {
58 platformDir, err := sdkpath.AndroidAPIPath(MinSDK)
59 if err != nil {
60 return "", err
61 }
62 return filepath.Join(platformDir, "android.jar"), nil
63 }
64
65 // PackResources produces a stripped down gzip version of the resources.arsc from api jar.
66 func PackResources() ([]byte, error) {
67 tbl, err := OpenSDKTable()
68 if err != nil {
69 return nil, err
70 }
71
72 tbl.pool.strings = []string{} // should not be needed
73 pkg := tbl.pkgs[0]
74
75 // drop language string entries
76 for _, typ := range pkg.specs[3].types {
77 if typ.config.locale.language != 0 {
78 for j, nt := range typ.entries {
79 if nt == nil { // NoEntry
80 continue
81 }
82 pkg.keyPool.strings[nt.key] = ""
83 typ.indices[j] = NoEntry
84 typ.entries[j] = nil
85 }
86 }
87 }
88
89 // drop strings from pool for specs to be dropped
90 for _, spec := range pkg.specs[4:] {
91 for _, typ := range spec.types {
92 for _, nt := range typ.entries {
93 if nt == nil { // NoEntry
94 continue
95 }
96 // don't drop if there's a collision
97 var collision bool
98 for _, xspec := range pkg.specs[:4] {
99 for _, xtyp := range xspec.types {
100 for _, xnt := range xtyp.entries {
101 if xnt == nil {
102 continue
103 }
104 if collision = nt.key == xnt.key; collision {
105 break
106 }
107 }
108 }
109 }
110 if !collision {
111 pkg.keyPool.strings[nt.key] = ""
112 }
113 }
114 }
115 }
116
117 // entries are densely packed but probably safe to drop nil entries off the end
118 for _, spec := range pkg.specs[:4] {
119 for _, typ := range spec.types {
120 var last int
121 for i, nt := range typ.entries {
122 if nt != nil {
123 last = i
124 }
125 }
126 typ.entries = typ.entries[:last+1]
127 typ.indices = typ.indices[:last+1]
128 }
129 }
130
131 // keeping 0:attr, 1:id, 2:style, 3:string
132 pkg.typePool.strings = pkg.typePool.strings[:4]
133 pkg.specs = pkg.specs[:4]
134
135 bin, err := tbl.MarshalBinary()
136 if err != nil {
137 return nil, err
138 }
139
140 buf := new(bytes.Buffer)
141
142 zw := gzip.NewWriter(buf)
143 if _, err := zw.Write(bin); err != nil {
144 return nil, err
145 }
146 if err := zw.Flush(); err != nil {
147 return nil, err
148 }
149 if err := zw.Close(); err != nil {
150 return nil, err
151 }
152 return buf.Bytes(), nil
153 }