Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Monday, April 8, 2024

cvc-complex-type.2.4.a: Invalid content was found starting with element 'base-extension'. One of '{layoutlib}' is expected Error in Android


We were facing this issue many times when we download project from github. We are using new Android studio. 

Error was: cvc-complex-type.2.4.a: Invalid content was found starting with element 'base-extension'. One of '{layoutlib}' is expected. 

This is not the gradle issue. When we search this over internet, we get lot of answers as it's a gradle issue. But following all those solutions are in vain as this is not actually a gradle issue.

We solved this issue with some experiments. 

Android SDK configuration file ~/Library/Android/sdk/platforms/android-32/package.xml has an extra tag named base-extension compared to the same file of other API levels. Removing this tag solves the problem.
Delete downloaded Android API 32 SDK Platform from SDK Manager (Tools → SDK Manager) also solves the problem (you can redownload after deletion). Maybe the Android studio team noticed the problem, redownloading API 32 platform files, the base-extension tag disappeared from the API 32 package.xml








Wednesday, January 3, 2024

How to set default value to spinner dropdown in Android?

 In Android, if you want to set a default value for a `Spinner` dropdown, you can achieve this by adding the default value to the adapter and selecting it programmatically. Here's an example:


import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Spinner spinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = findViewById(R.id.spinner);
        // Create a list of items for the spinner
        List<String> items = new ArrayList<>();
        items.add("Default Value"); // Add your default value here
        items.add("Item 1");
        items.add("Item 2");
        items.add("Item 3");
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, items);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);
        // Set the default value as the selected item
        spinner.setSelection(0); // 0 is the index of the default value
        // Set a listener to handle spinner item selection events
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // Handle the selected item
                String selectedValue = (String) parentView.getItemAtPosition(position);
                // Do something with the selected value
            }
            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // Do nothing here
            }
        });
    }
}


In this example, "Default Value" is added to the list of items, and `spinner.setSelection(0);` is used to set it as the default selected item. Adjust the code based on your specific use case and UI elements.

Thursday, July 13, 2023

How to add drawable resources to an existing Android Studio project

To add a drawable resource to an existing Android Studio project, you can follow these steps: 

  • Open your Android Studio project.
  • In the Project Explorer pane on the left side, navigate to the res folder.
  • Right-click on the res folder and select "New" -> "Android Resource Directory".
  • In the "Resource type" dropdown, select "drawable".
  • Click "OK" to create the new drawable resource directory.
  • In the newly created drawable directory, right-click and select "New" -> "Image Asset" (if you want to add an image file) or "Vector Asset" (if you want to add a vector file).     

       Follow the prompts in the Asset Studio wizard to import or create the desired image or vector resource.
  • If you choose "Image Asset", you can select an existing image file or create a new one. You can customize various options such as image type, shape, and padding.
  • If you choose "Vector Asset", you can import an SVG file or create a new vector using the built-in vector editor.
  • After configuring the asset, click "Next" and "Finish" to complete the process.
  • The new drawable resource will be added to the drawable directory, and it will be automatically generated in various densities (e.g., mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi) if it's an image asset.
  • can now reference the newly added drawable resource in your layout XML files or Java code by using its resource identifier (e.g., @drawable/your_drawable_resource_name).

Note: If you want to add an image file directly to the drawable directory without using the Asset Studio, you can simply copy the image file and paste it into the drawable directory. Make sure the image file has a proper filename, adhering to Android resource naming conventions (e.g., lowercase letters, underscores, no spaces or special characters).

 By following these steps, you can add a drawable resource to your existing Android Studio project.

 

 


 

 

 

  

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.