Android Studio能否自动导入缺失的sun.security.pkcs.PKCS10包?
sun.security.pkcs.PKCS10 in Android & How to Fix It Hey there, let's break down exactly why you're hitting that "Cannot resolve symbol PKCS10" error and how to fix it properly:
The Root Cause
You can't use sun.security.pkcs.PKCS10 in Android for a simple, non-negotiable reason: Android's runtime environment doesn't include any sun.* package classes.
These sun.* classes are proprietary, non-standard APIs from Oracle's JDK—they're not part of the official Java SE specification. Android's class library is built on OpenJDK, which intentionally excludes these private, vendor-specific classes to ensure consistency and security across all Android devices. There's no "automatic fix" to make this class appear; you can't enable a setting or rely on the system to provide it.
The Recommended Solution: Use BouncyCastle
BouncyCastle is a widely trusted, open-source cryptography library that fully supports PKCS#10 certificate signing request (CSR) functionality and works seamlessly with Android. Here's how to set it up:
Add the BouncyCastle dependency
Open your app-levelbuild.gradle(Module: app) file and add this line to thedependenciesblock:implementation 'org.bouncycastle:bcpkix-jdk15on:1.70'(You can check for the latest stable version if needed, but 1.70 is compatible with most Android versions.)
Import the correct PKCS10 class
Replace your broken import with BouncyCastle's official implementation:import org.bouncycastle.pkcs.PKCS10CertificationRequest;Quick Example: Generate a PKCS#10 CSR
Here's a snippet to get you started with creating a CSR using BouncyCastle (assuming you already have an RSA KeyPair generated):KeyPair keyPair = ...; // Your pre-generated RSA key pair X500Name subject = new X500Name("CN=YourName, OU=YourTeam, O=YourCompany, L=YourCity, ST=YourState, C=US"); try { PKCS10CertificationRequest csr = new JcaPKCS10CertificationRequest( "SHA256withRSA", subject, keyPair.getPublic(), null, keyPair.getPrivate() ); // Encode the CSR to PEM format or use it as needed } catch (Exception e) { e.printStackTrace(); }
Important Note
Avoid relying on sun.* classes in any Java/Android project—they're not portable, can break across different JDK/Android versions, and are not supported long-term. BouncyCastle is the industry-standard alternative for this kind of cryptography work.
内容的提问来源于stack exchange,提问作者Manishoaham

