我正在尝试按照此处使用自定义证书的Android文档。所需的网络配置文件是:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config>
<trust-anchors>
<certificates src="@raw/extracas"/>
<certificates src="system"/>
</trust-anchors>
</base-config>
</network-security-config>
我已经创建了network_security_config.xml,并将参考文献a ndroid:networkSecurityConfig="@xml/network_security_config"
添加到我的清单中。我有.crt文件我需要包括两个问题:
我无法在原始文件夹中创建一个目录,当我这样做时,它会在我的文件系统中创建目录,而在项目中的原始资源目录中也不能创建目录。
代替目录,我只是直接在原始文件夹中引用.crt文件,但是当我尝试引用证书时,这是我的network_security_config.xml
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config> <trust-anchors> <certificates src="@raw/cert_cubic_trusted_ca-sha256.crt"/> <certificates src="system"/> </trust-anchors> </base-config> </network-security-config>
我得到了一个带有错误的红色s尖"丢失src资源",当我尝试构建时,构建日志输出错误:
AGPBI: {"kind":"error","text":"error: resource raw/certname.crt (aka com.comname.appname:raw/certname.crt) not found.","sources":[{"file":"/Users/205314/project/appname/app/src/main/res/xml/network_security_config.xml","position":{"startLine":5}}],"original":"","tool":"AAPT"}
:app:processDebugResources
:app:processDebugResources FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:processDebugResources'.
> Failed to process resources, see aapt output above for details.
我不知道为什么我无法从XML中引用原始资源文件夹中的资产或在其中创建一个文件夹,这似乎是我最大的问题。我可以在代码中使用R.RAW引用原始资源,但我永远不需要使用@Raw引用,我不确定为什么它不如所述。
根据访问资源文档,资源名称为
文件名,不包括扩展名
因此,您需要从src
删除.crt
:
<certificates src="@raw/cert_cubic_trusted_ca-sha256"/>