如何在Android Studio中添加jbcrypt库?求助:无法引入mindrot.org的jbcrypt
Got it, let's walk through how to add jBCrypt to your Android Studio project—there are two straightforward methods to get this set up, depending on your workflow:
Method 1: Use Gradle Dependency (Recommended)
This is the easiest way since Gradle handles updates and dependencies automatically:
- Open your project's settings.gradle file (for newer Android Studio versions). Make sure
mavenCentral()is listed in the repositories block:dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() // Ensure this line exists } } - Next, open your app module's build.gradle (located at
app/build.gradle). Add the jBCrypt dependency to thedependenciesblock:dependencies { // Your other dependencies here... implementation 'org.mindrot:jbcrypt:0.11' // Use the latest version available } - Click the Sync Now button in the top-right corner of Android Studio. Once sync finishes, you can start using jBCrypt in your code:
// Example: Hash a password String hashedPassword = BCrypt.hashpw("yourPlainTextPassword", BCrypt.gensalt()); // Example: Verify a password boolean passwordMatches = BCrypt.checkpw("inputPassword", hashedPassword);
Method 2: Manually Import the JAR File
If you prefer to handle the JAR file directly (e.g., if you need a specific older version):
- Download the jBCrypt JAR file from the official project page.
- In your Android Studio project, right-click the app module -> New -> Directory, and name it
libs. - Copy the downloaded JAR file into the
libsfolder. - Right-click the JAR file in Android Studio and select Add as Library. Choose your app module in the popup and click OK.
- Gradle will automatically add this line to your app's
build.gradledependencies block:implementation files('libs/jbcrypt-0.11.jar') - Run a Gradle sync, and you're ready to use the library.
Notes to Keep in Mind
- Min SDK Version: jBCrypt works with most Android versions (API 16 and above should be fine, but double-check if you're targeting very old devices).
- ProGuard/R8 Obfuscation: If you're using code obfuscation, add this line to your
proguard-rules.profile to prevent jBCrypt classes from being stripped:-keep class org.mindrot.jbcrypt.** { *; }
内容的提问来源于stack exchange,提问作者Nic Parmee
相关产品推荐
相关产品推荐

