‘D8: Cannot fit requested classes in a single dex file’ error in Flutter/Android Studio


Sometimes you may have end up with this error while starting with a new Flutter project and should tried to google it.

The gradle error caused by the wrong configuration as a result app may not run and end up with the following message.

BUILD FAILED in 1m 0s [!] The shrinker may have failed to optimize the Java bytecode. Exception: Gradle task assembleDebug failed with exit code 1 To disable the shrinker, pass the --no-shrink flag to this command.

Solution

To solve the problem you need to fix the Min SDK version which is wrongly configured. Go to your app level gradle Project ->Android ->app > build.gradle and find the following

defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.developerm.kkart"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

and replace the it with the newer  version

defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.developerm.kkart"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

and the error will go away, try to rebuild the project.

How to use Photoshop color code with Color class in Flutter


I love color pallet of photoshop and do miss them in Android studio. Google should think about the pallet plugin.

I search the market place and not find anything can help with color pallet. I figure out how I can use those photoshop color code with Flutter, the Color class

The class that make colors

The Color class can generate color I want, the problem is that photoshop bring me color code as string of alphabets and Color class only accepting the integer value.

To use it with the class I have to prefix 0xFF which defines the full opacity and it make up the Integer value.

const color = const Color(0xFFB74093);

and it just do work.

Insert if not exist sqllite [android]


We are femiliar with if not exits keyword when working sqllite database in Android or in web applications as well.

If not exist work with create statements, so how do we implement the same tactics in insert statement ?

Here what I found ,

Take a look at what we have

Here I got a table Entries which keep track of different entries with auto grnerated eid column. I wanna make happends only insert entries that is not exist in the table.entries.pngTodo

  1. Create a unique index on ename column
  2. usere insert or ignore into statement instead of insert into
Index the colum
create unique index Entry_name_start on Entries ( ename ) ;

This will generate a intex on column ename, which means it can’t be duplicated

The new insert insert.png
insert or ignore into "Entries" (
    id,
    ename)
values (
    :id,
    :ename)
;

It will work….

Note: I used Android Studio and plugin called DB Browser in this tutorial

Ways you can handle button click in Android Studio Project


In a Android project you need to handle button click for completion of data processing, a double click maiy not enough create automatic Event handler as in Microsoft visual Studio projects. You have to do it in Java style.

You have two options, one is to handle click in Oncreate method or simply add new Method and attach it to the Onclick property [Layout]

 The first Method

Most of the coders love this method and it can save time as well. The code reside in OnCreate method

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);    
    textView = (TextView) findViewById(R.id.text1);
    Button button2=findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textView.setText("I clciked button2 - Oncreate");
        }
    });
     
}
The Second Method

This will require Create a Public Method with View as argument. Keep in mind that all controls should be declared just below the beggining of the class and assing controls at OnCreate method

public class MainActivity extends AppCompatActivity {
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);         
        textView = (TextView) findViewById(R.id.text1);
   
    }
The hadler method

Place the code after the OnCreate

public void ButtonClick1(View view) {
    textView.setText("I Clicked Button1");
}

Go to the layout and  find the onclick property of the button ther you will see the ButtonClick1 choose the right one

or you can configure it in the XML file

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="ButtonClick1"
            android:text="OnClick 1"
            tools:layout_editor_absoluteX="30dp"
            tools:layout_editor_absoluteY="58dp" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Onclick 2"
            tools:layout_editor_absoluteX="128dp"
            tools:layout_editor_absoluteY="58dp" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

Watch the tutorial video on Youtube

I think this was enough for begiiners, leave comments and questions below

 

 

 

 

Create and read simple JSON_STRING in Android Studio


JSON_STRING strings are similar to Dictionary and List in Python, they can be searched quickly. JSON_STRING can be used to store data for your application.

This post will teach you how to create and read JSON_STRING objects in Android Studio/Eclipse Android apps.

  1. Go ahead and create a simple activity app
  2. Switch to Android View
  3. Create simple JSON_STRING as follows in the MainActivity.Java class
    String JSON_STRING = "{\"DSLR\":{\"Model\":\"Sony Alpha 58\",\"Sensor\":APC}}";

    [Drop the above code at the beginning of the OnCreate method]

  4. Open main_activity.xml [can find under res/layout]
  5. Drag and Drop two TextView control to the Form.
  6. Change the name of the controls in the content_main.xml file as ‘model’ and ‘sensor’ respectively
  7. Now create variable to read a string from JSON and assign the values to controls
String model, sensor;
TextView Model, Sensor;
Model = (TextView) findViewById(R.id.model);
Sensor = (TextView) findViewById(R.id.sensor);
try {
 // get JSONObject from JSON file
 JSONObject obj = new JSONObject(JSON_STRING);
 // fetch JSONObject named employee
 JSONObject dslr = obj.getJSONObject("DSLR");
 // get employee name and salary
 model = dslr.getString("Model");
 sensor = dslr.getString("Sensor");
 // set employee name and salary in TextView's
 Model.setText("Model: "+model);
 Sensor.setText("Sensor: "+sensor);

} catch (JSONException e) {
 e.printStackTrace();
}

Alt+Enter to auto import libraries. Here the JSONObject will help you to fetch ‘DSLR’ String. In our example, we have one only one JSON string so that used two Text views only.

Remember JSON strings are case sensitive.

You can download the source code from GitHub