Friday, June 30, 2023

Android Studio says: duplicate class found and not able to build

While I was trying to build an android project on latest Android studio that is: 

Android Studio Flamingo | 2022.2.1 Patch 2
Build #AI-222.4459.24.2221.10121639, built on May 12, 2023
Runtime version: 17.0.6+0-b2043.56-9586694 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.

I got the below error:
"Duplicate class found"

Duplicate class kotlin.collections.jdk8.CollectionsJDK8Kt found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk8-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.21)
Duplicate class kotlin.internal.jdk7.JDK7PlatformImplementations found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.internal.jdk7.JDK7PlatformImplementations$ReflectSdkVersion found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.internal.jdk8.JDK8PlatformImplementations found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk8-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.21)
Duplicate class kotlin.internal.jdk8.JDK8PlatformImplementations$ReflectSdkVersion found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk8-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.21)
Duplicate class kotlin.io.path.DirectoryEntriesReader found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.io.path.ExperimentalPathApi found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.io.path.FileVisitorBuilder found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.io.path.FileVisitorBuilderImpl found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.io.path.FileVisitorImpl found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.io.path.LinkFollowing found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.io.path.PathNode found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.io.path.PathRelativizer found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.io.path.PathTreeWalk found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.io.path.PathTreeWalk$bfsIterator$1 found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.io.path.PathTreeWalk$dfsIterator$1 found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.io.path.PathTreeWalkKt found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.io.path.PathWalkOption found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.io.path.PathsKt found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.io.path.PathsKt__PathReadWriteKt found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.io.path.PathsKt__PathUtilsKt found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.jdk7.AutoCloseableKt found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk7-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.21)
Duplicate class kotlin.jvm.jdk8.JvmRepeatableKt found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk8-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.21)
Duplicate class kotlin.jvm.optionals.OptionalsKt found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk8-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.21)
Duplicate class kotlin.random.jdk8.PlatformThreadLocalRandom found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk8-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.21)
Duplicate class kotlin.streams.jdk8.StreamsKt found in modules kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0) and kotlin-stdlib-jdk8-1.7.21 (org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.21)
I solved this issue by downgrading the dependencies.
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'

Just downgrading from 2.6.1 to 2.5.1
Like below

implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'


If the above solution does not affect, you can try to add the below dependency:

implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.0"))
If the above solution does not affect anything, try downgrading some plugins you just upgraded. It'll definitely resolve.

I hope it is helpful.

Saturday, June 24, 2023

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Error in android

I got the below error while running my android project :

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script7.groovy: 1: unable to resolve class

To solve this error I have to upgrade my jdk. To upgrade the jdk, you can follow the below steps

From File menu -> Project Structure -> SDK Location -> JDK location Drop down menu choose:
Embeded JDK c:....\jre


or you can upgrade the jdk to latest jdk by clickiing on upgrade to latest jdk. You can upgrade jdk from Gradle settings also.

This solves the above error. 

Wednesday, June 7, 2023

Understanding Android Security: Best Practices for Secure App Development

 Developing secure Android apps requires following best practices and implementing various security measures. Here are some tips to enhance the security of your Android app:

  1. Secure Data Storage:
    • Use secure storage mechanisms, such as the Android Keystore system, to store sensitive data like passwords, encryption keys, and API keys.
    • Avoid storing sensitive information in plain text or easily accessible locations like shared preferences.
    • Employ data encryption techniques like AES or RSA when storing data on disk or transmitting it over the network.
  2. Input Validation and Sanitization:
    • Validate and sanitize all user inputs to prevent common security vulnerabilities like SQL injection, cross-site scripting (XSS), and remote code execution.
    • Utilize input validation libraries and frameworks to simplify the process and reduce the risk of errors.
  3. Secure Network Communication:
    • Use secure communication protocols such as HTTPS/TLS for transmitting sensitive data over the network.
    • Implement certificate pinning to ensure the authenticity of the server’s SSL certificate.
    • Validate server certificates and hostname to prevent man-in-the-middle attacks.
    • Avoid transmitting sensitive information in URL parameters or query strings.
  4. User Authentication and Authorization:
    • Implement strong and secure authentication mechanisms, such as password hashing, biometric authentication, or OAuth-based authentication.
    • Enforce secure password policies (e.g., complexity, length, expiration) to enhance user account security.
    • Implement session management techniques to handle user sessions securely.
    • Employ appropriate authorization mechanisms to control access to sensitive app features and data.
  5. Secure Code Practices:
    • Regularly update and patch your app’s dependencies, libraries, and frameworks to address known security vulnerabilities.
    • Implement secure coding practices, such as input validation, output encoding, and secure error handling.
    • Avoid hardcoding sensitive information like API keys, passwords, or cryptographic keys directly in the code.
    • Utilize appropriate security frameworks and libraries to handle security-related tasks effectively.
  6. User Privacy:
    • Request and handle user permissions appropriately and transparently, following the principle of least privilege.
    • Implement privacy controls and settings to allow users to control their data and preferences.
    • Be cautious with collecting and storing personally identifiable information (PII), adhering to privacy regulations like GDPR or CCPA.
  7. Code Obfuscation and Reverse Engineering:
    • Employ code obfuscation techniques (e.g., ProGuard, R8) to make it harder for attackers to understand and reverse engineer your app’s code.
    • Use techniques like code tampering detection and runtime integrity checks to detect and respond to tampering attempts.
  8. Regular Security Testing:
    • Conduct regular security assessments and penetration testing to identify and address vulnerabilities.
    • Use static code analysis tools and security testing frameworks to identify common security issues during the development process.
    • Perform dynamic security testing to simulate real-world attacks and assess the resilience of your app.
  9. Stay Updated with Security Best Practices:
    • Keep up-to-date with the latest security guidelines and best practices from Android and security communities.
    • Follow Android’s security-related recommendations, including the Android Security Guidelines and Security Best Practices documentation.

Remember, security is an ongoing process, and it’s essential to monitor security news and updates regularly to address emerging threats and vulnerabilities.

Tuesday, June 6, 2023

Building Android Apps with Material Design Guidelines

Building Android apps with Material Design guidelines can help you create visually appealing and intuitive user interfaces. Here are some key considerations and best practices to follow when incorporating Material Design into your Android app:

  1. Understand Material Design Principles:
    • Familiarize yourself with the principles of Material Design, such as material metaphor, bold and intentional design, meaningful motion, and responsive interactions. This will provide a foundation for implementing Material Design effectively.
  2. Use Material Components:
    • Utilize the Material Components for Android library, which provides pre-built UI components following Material Design guidelines. This library offers a wide range of customizable components like buttons, cards, dialogs, text fields, and more, ensuring visual consistency and smooth integration.
  3. Consistent Color and Typography:
    • Follow the color and typography guidelines provided by Material Design. Use the color palette and typography scale to ensure consistency throughout your app.
    • Apply color and typography appropriately to indicate hierarchy, emphasize important elements, and enhance readability.
  4. Responsive Layouts:
    • Design responsive layouts that adapt to different screen sizes and orientations. Utilize ConstraintLayout and other responsive layout techniques to create flexible and scalable UIs.
    • Ensure that your app’s UI elements are appropriately sized and positioned to provide a consistent user experience across different devices.
  5. Meaningful Motion and Transitions:
    • Leverage motion to enhance user experience and provide visual feedback. Use meaningful and purposeful animations to guide users, convey changes, and highlight important actions.
    • Implement smooth transitions between screens and interactions to create a seamless and engaging user flow.
  6. Iconography and Imagery:
    • Utilize appropriate icons from the Material Design icon library or create custom icons that align with the Material Design style.
    • Use high-quality imagery that aligns with Material Design aesthetics. Optimize image sizes and resolutions to ensure smooth performance.
  7. Adaptive Design:
    • Take advantage of adaptive design techniques to optimize your app’s appearance on different devices, including smartphones, tablets, and foldable devices.
    • Adapt layouts, fonts, and spacing dynamically to provide an optimal user experience on various screen sizes and form factors.
  8. Gestures and Touch Feedback:
    • Implement touch gestures and touch feedback to make your app interactive and responsive. Use touch ripples and animations to provide visual feedback when elements are tapped or swiped.
    • Ensure that touch targets are appropriately sized and spaced to accommodate different screen sizes and user interactions.
  9. Accessibility:
    • Consider accessibility guidelines when designing your app. Use appropriate contrast ratios, provide alternative text for images, ensure proper focus order, and support assistive technologies.
    • Test your app’s accessibility features and make necessary adjustments to ensure inclusivity for all users.
  10. Test and Iterate:
    • Test your app thoroughly on various devices and screen sizes to ensure that the UI elements and interactions align with Material Design principles.
    • Collect user feedback and iterate on your design to improve usability and address any usability issues or inconsistencies.
Remember that Material Design is a set of guidelines, and while it provides a solid foundation for building visually appealing and intuitive apps, it’s essential to strike a balance between adhering to the guidelines and incorporating your app’s unique branding and identity.
Happy coding!!!