在定制rom中内置中文资源时遇到中文乱码的问题,首先是编译遇到编码问题
[ 88% 390/443] Installed file list: out/target/product/ceres-tencent/installed-files-product.txt
FAILED: out/target/product/ceres-tencent/installed-files-product.txt out/target/product/ceres-tencent/installed-files-product.json
/bin/bash -c "(mkdir -p out/target/product/ceres-tencent/ ) && (rm -f out/target/product/ceres-tencent/installed-files-product.txt ) && (out/soong/host/linux-x86/bin/fileslist out/target/product/ceres-tencent/product > out/target/product/ceres-tencent/installed-files-product.json ) && (build/make/tools/fileslist_util.py -c out/target/product/ceres-tencent/installed-files-product.json > out/target/product/ceres-tencent/installed-files-product.txt )"
Traceback (most recent call last):
File "build/make/tools/fileslist_util.py", line 70, in <module>
main(sys.argv)
File "build/make/tools/fileslist_util.py", line 64, in main
PrintCanonicalList(args[0])
File "build/make/tools/fileslist_util.py", line 32, in PrintCanonicalList
print "{0:12d} {1}".format(line["Size"], line["Name"])
UnicodeEncodeError: 'ascii' codec can't encode characters in position 22-23: ordinal not in range(128)
13:34:35 ninja failed with: exit status 1
编译中文乱码,配置utf8编码格式;
Index: fileslist_util.py
===================================================================
import sys
reload(sys)
sys.setdefaultencoding("utf8")
import getopt, json, sys
def PrintFileNames(path):
pack4dist -v 打包ota的时候时,也会遇到部分中文乱码的问题;
- add_img_to_target_files.py - INFO : done
...
error: lstat 点符号.JXW: file does not exist
...
首先根据日志在build搜索对应的错误,结果发现关键字搜索"lstat"
A133_S12.0_MID/build$ grep '"lstat"' * -Rn
grep: soong/.git/description: No such file or directory
soong/zip/zip.go:580: Op: "lstat",
soong/zip/zip.go:598: Op: "lstat",
soong/zip/zip.go:609: Op: "lstat",
...
func zipTo(args ZipArgs, w io.Writer) error {
...
for _, fa := range args.FileArgs {
var srcs []string
for _, s := range fa.SourceFiles {
s = strings.TrimSpace(s)
if s == "" {
continue
}
}
result, err := z.fs.Glob(s, nil, followSymlinks)
if err != nil {
return err
}
if len(result.Matches) == 0 {
err := &os.PathError{
Op: "lstat",
Path: s,
Err: os.ErrNotExist,
}
if args.IgnoreMissingFiles {
fmt.Fprintln(z.stderr, "warning:", err)
} else {
return err
}
}
srcs = append(srcs, result.Matches...)
}
...
}
...
看起来和输出日志就很像了,在AOSP中golang环境配置在 prebuilts 目录下,完全对的上
A133_S12.0_MID/prebuilts/go$ grep "file does not exist" * -Rn
...
darwin-x86/src/errors/wrap_test.go:244: fmt.Println("file does not exist")
darwin-x86/src/errors/wrap_test.go:251: // file does not exist
darwin-x86/src/syscall/errors_plan9.go:32: ENOENT = NewError("file does not exist")
darwin-x86/src/cmd/go/internal/work/exec.go:1708: // If the file does not exist, there are no exported
darwin-x86/src/cmd/go/internal/modfetch/codehost/codehost.go:54: // If the requested file does not exist it should return an error for which
darwin-x86/src/cmd/go/internal/modfetch/codehost/codehost.go:106: Err error // error if any; os.IsNotExist(Err)==true if rev exists but file does not exist in that rev
darwin-x86/src/cmd/go/internal/modfetch/fetch.go:215: // The zip file does not exist. Acquire the lock and create it.
darwin-x86/src/internal/oserror/errors.go:16: ErrNotExist = errors.New("file does not exist")
...
linux-x86/src/os/error.go:23: ErrNotExist = errNotExist() // "file does not exist"
linux-x86/src/os/file.go:311:// it is truncated. If the file does not exist, it is created with mode 0666
linux-x86/src/os/file.go:321:// (O_RDONLY etc.). If the file does not exist, and the O_CREATE flag
linux-x86/src/io/ioutil/ioutil.go:77:// If the file does not exist, WriteFile creates it with permissions perm
linux-x86/src/errors/wrap_test.go:244: fmt.Println("file does not exist")
linux-x86/src/errors/wrap_test.go:251: // file does not exist
linux-x86/src/syscall/errors_plan9.go:32: ENOENT = NewError("file does not exist")
linux-x86/src/cmd/go/internal/work/exec.go:1708: // If the file does not exist, there are no exported
linux-x86/src/cmd/go/internal/modfetch/codehost/codehost.go:54: // If the requested file does not exist it should return an error for which
linux-x86/src/cmd/go/internal/modfetch/codehost/codehost.go:106: Err error // error if any; os.IsNotExist(Err)==true if rev exists but file does not exist in that rev
linux-x86/src/cmd/go/internal/modfetch/fetch.go:215: // The zip file does not exist. Acquire the lock and create it.
linux-x86/src/internal/oserror/errors.go:16: ErrNotExist = errors.New("file does not exist")
在根据方法继续推进在
soong/zip/zip.go
------------------------------------------------------------------------
func Zip(args ZipArgs) error {
...
zipErr = zipTo(args, out)
if zipErr != nil {
return zipErr
}
...
return nil
}
...
soong/zip/cmd/main.go:216: err := zip.Zip(zip.ZipArgs{
...
------------------------------------------------------------------------
func main() {
...
out := flags.String("o", "", "file to write zip file to")
manifest := flags.String("m", "", "input jar manifest file name")
directories := flags.Bool("d", false, "include directories in zip")
compLevel := flags.Int("L", 5, "deflate compression level (0-9)")
emulateJar := flags.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'")
writeIfChanged := flags.Bool("write_if_changed", false, "only update resultant .zip if it has changed")
ignoreMissingFiles := flags.Bool("ignore_missing_files", false, "continue if a requested file does not exist")
symlinks := flags.Bool("symlinks", true, "store symbolic links in zip instead of following them")
srcJar := flags.Bool("srcjar", false, "move .java files to locations that match their package statement")
parallelJobs := flags.Int("parallel", runtime.NumCPU(), "number of parallel threads to use")
cpuProfile := flags.String("cpuprofile", "", "write cpu profile to file")
traceFile := flags.String("trace", "", "write trace to file")
flags.Var(&rootPrefix{}, "P", "path prefix within the zip at which to place files")
flags.Var(&listFiles{}, "l", "file containing list of files to zip")
flags.Var(&rspFiles{}, "r", "file containing list of files to zip with Ninja rsp file escaping")
flags.Var(&dir{}, "D", "directory to include in zip")
flags.Var(&file{}, "f", "file to include in zip")
flags.Var(&nonDeflatedFiles, "s", "file path to be stored within the zip without compression")
flags.Var(&relativeRoot{}, "C", "path to use as relative root of files in following -f, -l, or -D arguments")
flags.Var(&junkPaths{}, "j", "junk paths, zip files without directory names")
...
}
通过上述操作,就基本定位到问题所在了,这里因为对go语言不熟,所以没有去细究为什么有部分乱码;
首先更具代码修改下允许文件不存在时继续编译
ignoreMissingFiles := flags.Bool("ignore_missing_files", true, "continue if a requested file does not exist")
就可以得到所有会出现乱码的资源文件如下
...
- add_img_to_target_files.py - INFO : done.
warning: lstat .jpg: file does not exist
warning: lstat .swf: file does not exist
warning: lstat ´浪.jpg: file does not exist
warning: lstat ´浪.swf: file does not exist
warning: lstat 天日.swf: file does not exist
warning: lstat 交.jpg: file does not exist
warning: lstat 交.swf: file does not exist
warning: lstat 丠file does not exist
warning: lstat .jpg: file does not exist
warning: lstat 丠file does not exist
warning: lstat .jpg: file does not exist
warning: lstat .swf: file does not exist
warning: lstat 日.jpg: file does not exist
warning: lstat 日.swf: file does not exist
...
然后在zipTo()对这些文件路径进行重新赋值,可以根据索引或者关键字来替换
func zipTo(args ZipArgs, w io.Writer) error {
...
for _, fa := range args.FileArgs {
var srcs []string
numIndex := 0
num := 0
numDIndex := 0
for _, s := range fa.SourceFiles {
if(strings.Contains(s,"ansystem") && strings.Contains(s,"target_files_intermediates")){
}else{
s = strings.TrimSpace(s)
}
num = num +1;
if s == "" {
continue
}
//
if(strings.Contains(s,"ansystem") && strings.Contains(s,"target_files_intermediates")){
if(strings.Contains(s,"一马当") && numIndex %2 ==0){
numIndex = numIndex +1;
s = s[:(strings.LastIndex(s,"/")+1)]+"一马当先.jpg"
}else if(strings.Contains(s,"一马当") && numIndex %2 !=0){
numIndex = numIndex +1;
s = s[:(strings.LastIndex(s,"/")+1)]+"一马当先.swf"
}
....
//fmt.Fprintln(z.stderr, "Path:", s)
}
result, err := z.fs.Glob(s, nil, followSymlinks)
if err != nil {
return err
}
/*if(strings.Contains(s,"ansystem") && strings.Contains(s,"target_files_intermediates")){
fmt.Fprintln(z.stderr, "result len= %d numIndex=%d num=%d Path2:%s ", len(result.Matches),numIndex,num,s)
}*/
if len(result.Matches) == 0 {
// fmt.Fprintln(z.stderr, "result Matches :", result.Matches)
//fmt.Fprintln(z.stderr, "Path2:", s)
err := &os.PathError{
Op: "lstat",
Path: s,
Err: os.ErrNotExist,
}
if args.IgnoreMissingFiles {
fmt.Fprintln(z.stderr, "warning:", err)
} else {
return err
}
}
srcs = append(srcs, result.Matches...)
}
...
for _, src := range srcs {
/*if(strings.Contains(src,"ansystem") && strings.Contains(src,"target_files_intermediates")){
fmt.Fprintf(z.stderr, "-----src: %s \n",src)
}*/
err := fillPathPairs(fa, src, &pathMappings, args.NonDeflatedFiles, noCompression)
if err != nil {
return err
}
}
}
return z.write(w, pathMappings, args.ManifestSourcePath, args.EmulateJar, args.SrcJar, args.NumParallelJobs)
}
当然如果资源太多可能会有如下错误
zipfile.LargeZipFile: Central directory offset would require ZIP64 extensions
2023-08-28 12:14:35 - common.py - WARNING : Failed to read ODM_DLKM/etc/build.prop
2023-08-28 12:14:35 - common.py - WARNING : Failed to read ODM_DLKM/build.prop
Traceback (most recent call last):
File "/disk31/jczeng/A100/A133_S12.0_MID/out/soong/host/linux-x86/bin/img_from_target_files/internal/stdlib/runpy.py", line 174, in _run_module_as_main
File "/disk31/jczeng/A100/A133_S12.0_MID/out/soong/host/linux-x86/bin/img_from_target_files/internal/stdlib/runpy.py", line 72, in _run_code
File "/disk31/jczeng/A100/A133_S12.0_MID/out/soong/host/linux-x86/bin/img_from_target_files/__main__.py", line 12, in <module>
File "/disk31/jczeng/A100/A133_S12.0_MID/out/soong/host/linux-x86/bin/img_from_target_files/internal/stdlib/runpy.py", line 174, in _run_module_as_main
File "/disk31/jczeng/A100/A133_S12.0_MID/out/soong/host/linux-x86/bin/img_from_target_files/internal/stdlib/runpy.py", line 72, in _run_code
File "/disk31/jczeng/A100/A133_S12.0_MID/out/soong/host/linux-x86/bin/img_from_target_files/img_from_target_files.py", line 253, in <module>
File "/disk31/jczeng/A100/A133_S12.0_MID/out/soong/host/linux-x86/bin/img_from_target_files/img_from_target_files.py", line 245, in main
File "/disk31/jczeng/A100/A133_S12.0_MID/out/soong/host/linux-x86/bin/img_from_target_files/img_from_target_files.py", line 218, in ImgFromTargetFiles
File "/disk31/jczeng/A100/A133_S12.0_MID/out/soong/host/linux-x86/bin/img_from_target_files/img_from_target_files.py", line 177, in RebuildAndWriteSuperImages
File "/disk31/jczeng/A100/A133_S12.0_MID/out/soong/host/linux-x86/bin/img_from_target_files/common.py", line 2716, in ZipWrite
File "/disk31/jczeng/A100/A133_S12.0_MID/out/soong/host/linux-x86/bin/img_from_target_files/internal/stdlib/zipfile.py", line 1148, in write
File "/disk31/jczeng/A100/A133_S12.0_MID/out/soong/host/linux-x86/bin/img_from_target_files/internal/stdlib/zipfile.py", line 1114, in _writecheck
zipfile.LargeZipFile: Filesize would require ZIP64 extensions
-k: command not found
这个错误的话把ZIP64配置为true就行
Index: cpython2/Lib/zipfile.py
===================================================================
- def FileHeader(self, zip64=None):
+ def FileHeader(self, zip64=True):
"""Return the per-file header as a string."""
dt = self.date_time
dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
@@ -340,7 +340,6 @@
file_size = self.file_size
extra = self.extra
-
if zip64 is None:
zip64 = file_size > ZIP64_LIMIT or compress_size > ZIP64_LIMIT
if zip64:
@@ -349,7 +348,7 @@
1, struct.calcsize(fmt)-4, file_size, compress_size)
if file_size > ZIP64_LIMIT or compress_size > ZIP64_LIMIT:
if not zip64:
raise LargeZipFile("Filesize would require ZIP64 extensions")
# File is larger than what fits into a 4 byte integer,
# fall back to the ZIP64 extension
file_size = 0xffffffff
@@ -723,7 +722,7 @@
fp = None # Set here since __del__ checks it
- def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=False):
+ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True):
"""Open the ZIP file with mode read "r", write "w" or append "a"."""
if mode not in ("r", "w", "a"):
raise RuntimeError('ZipFile() requires mode "r", "w", or "a"')
@@ -737,7 +736,7 @@
else:
raise RuntimeError, "That compression method is not supported"
- self._allowZip64 = allowZip64
+ self._allowZip64 = True
self._didModify = False
self.debug = 0 # Level of printing: 0 through 3
self.NameToInfo = {} # Find file info given name
@@ -1155,7 +1154,7 @@
zinfo.external_attr |= 0x10 # MS-DOS directory flag
self.filelist.append(zinfo)
self.NameToInfo[zinfo.filename] = zinfo
- self.fp.write(zinfo.FileHeader(False))
+ self.fp.write(zinfo.FileHeader(True))
return
with open(filename, "rb") as fp:
网友评论