【问题标题】:Self signed certificate in Android application of RADRAD的Android应用程序中的自签名证书
【发布时间】:2019-10-11 14:16:49
【问题描述】:

我通过 SSL 使用自签名证书提供 REST 服务。我可以通过 TRESTClient 从常规的 delphi 应用程序中调用它。但是当我在 Android 应用程序中使用它时,它会在调用过程中引发以下异常。

带有消息的异常类 EJNIException 'java.security.cert.CertificateException: java.security.cert.CertPathValidatorException:信任锚 找不到证书路径。

我发现,必须通过将android:networkSecurityConfig="@xml/network_security_config" 添加到AndroidManifest.template.xml 文件的application 部分来配置网络安全。

<application android:networkSecurityConfig="@xml/network_security_config"
    android:persistent="%persistent%" 
    android:restoreAnyVersion="%restoreAnyVersion%" 
    android:label="%label%" 
    android:debuggable="%debuggable%" 
    android:largeHeap="%largeHeap%"
    android:icon="%icon%"
    android:theme="%theme%"
    android:hardwareAccelerated="%hardwareAccelerated%"
    android:resizeableActivity="false">

然后我必须创建具有类似内容的 res\xml\network_security_config.xml 文件:

<?xml version="1.0" encoding="utf-8"?> <network-security-config>
    <domain-config>
        <domain includeSubdomains="true">example.com</domain>
        <trust-anchors>
            <certificates src="@raw/my_ca"/>
        </trust-anchors>
    </domain-config> </network-security-config>

但是在哪里放置 network_security_config.xml 文件。当我将它放在 Debug 内的 res 文件夹中时,它在构建过程中被删除了。因此在打包阶段找不到,应用程序会引发异常。

错误:E2312 C:\Users\User\Documents\Embarcadero\Studio\Projects\Andr\Android\Debug\TabbedApplication\AndroidManifest.xml:19: 错误:错误:找不到与给定名称匹配的资源(在 'networkSecurityConfig' 值为 '@xml/network_security_config')。

我不知道将其放置在哪里并让 RAD Studio 知道它必须在编译时获取此文件。

【问题讨论】:

  • '当我把它放在 Debug 内的 res 文件夹中' - 但你把它放在 res/xml 中,是吗?
  • 是的。我在 res 文件夹中创建了 xml 文件夹。就我而言,它是 'C:\Users\User\Documents\Embarcadero\Studio\Projects\Andr\Android\Debug\TabbedApplication\res'
  • 当我通过 'Project>AddTo Project...' 将 network_security_config.xml 文件添加到项目中时,Rad Studio 会在 'assets\internal' 文件夹中创建它。但它必须在“res\xml”内才能在打包阶段找到。我还尝试将此文件添加为资源“项目>资源和图像...”。结果是一样的。
  • 尝试项目 -> 部署。在那里,您可以指定需要部署的本地文件(构建文件夹之外)和位置。您不能将文件直接放入 Debug/Release 构建文件夹,因为它们在每次构建时都会被设计删除(正如您已经注意到的那样)
  • 是的。有用。我将此本地文件添加到调试和发布部分。它有效。谢谢达莉娅。你是我的幸存者

标签: android rest delphi ssl android-manifest


【解决方案1】:

您不能直接使用 Debug 和 Release 文件夹来指定您需要与应用程序一起部署的文件,因为它们是每次重建时清除的临时文件夹。

对于部署,您应该使用Project -&gt; Deployment 对话框。

您可以在此处指定要部署的本地文件(放置在构建文件夹之外)、配置(调试、发布或两者)及其目标路径。

【讨论】:

  • 是的。我添加到项目->部署的调试和发布部分。不进入文件夹。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-19
  • 1970-01-01
  • 2015-11-16
  • 2012-09-23
  • 1970-01-01
  • 2016-07-24
  • 2021-11-20
相关资源
最近更新 更多