Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve symbol `FileUtils` in Codepath `Todo App` tutorial (Android)

I'm trying to follow a Todo app tutorial from Codepath (https://guides.codepath.com).

Source: https://guides.codepath.com/android/Basic-Todo-App-Tutorial

As far as I can tell I've followed the tutorial, but am getting the below issue.

Cannot resolve symbol `FileUtils`

MainActivity.Java

package com.codepath.simpletodo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView; 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ArrayList<String> items;
    private ArrayAdapter<String> itemsAdapter;
    private ListView lvItems;

    private void readItems() {
        File filesDir = getFilesDir();
        File todoFile = new File(filesDir, "todo.txt");
        try {
            items = new ArrayList<String>(FileUtils.readLines(todoFile));
        } catch (IOException e) {
            items = new ArrayList<String>();
        }
    }

    private void writeItems() {
        File filesDir = getFilesDir();
        File todoFile = new File(filesDir, "todo.txt");
        try {
            FileUtils.writeLines(todoFile, items);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lvItems = (ListView) findViewById(R.id.lvItems);
        items = new ArrayList<String>();
        itemsAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, items);
        lvItems.setAdapter(itemsAdapter);
        items.add("Default item");
        items.add("Another default item");
        items.add("The third default item");
        // setup remove listener method call
        setupListViewListener();
    }

    // Attaches a long click listener to the listview
    private void setupListViewListener() {
        lvItems.setOnItemLongClickListener(
                new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> adapter,
                                                   View item, int pos, long id) {
                        // Remove item within array at position
                        items.remove(pos);
                        // Refresh the adapter
                        itemsAdapter.notifyDataSetChanged();
                        // Return true consumes the long click event (marks it handled)
                        return true;
                    }
                });
    }

    public void onAddItem(View v) {
        EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
        String itemText = etNewItem.getText().toString();
        itemsAdapter.add(itemText);
        etNewItem.setText("");
    }
}

activity_mail.xml

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/lvItems"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_above="@+id/btnAddItem" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/etNewItem"
    android:layout_alignTop="@+id/btnAddItem"
    android:hint="Enter a new item"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/btnAddItem"
    android:layout_toStartOf="@+id/btnAddItem"
    android:layout_alignParentBottom="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add Item"
    android:id="@+id/btnAddItem"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="onAddItem"
    />

I'm extremely new to Android and would be so happy just to get things to work, as I don't have enough background yet to have any hope at debugging.

Any guidance would be very appreciated.

UPDATE 1

app/build.gradle

Original Format

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "com.codepath.simpletodo"
        minSdkVersion 14
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
}

I've tried various combinations of including 'org.apache.commons:commons-io:1.3.2'.

  1. Replacing 'com.android.support:appcompat-v7:24.0.0' with 'org.apache.commons:commons-io:1.3.2'
  2. Putting both on same line: compile 'com.android.support:appcompat-v7:24.0.0', 'org.apache.commons:commons-io:1.3.2'.
  3. Putting both on separate lines: compile 'com.android.support:appcompat-v7:24.0.0' compile 'org.apache.commons:commons-io:1.3.2'.

But when attempting the Gradle project sync, there is a failure with below message.

Error:(24, 13) Failed to resolve: compile org.apache.commons:commons-io:1.3.2

UPDATE 2

The final working version needed to look like this.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'
}
like image 284
tim_xyz Avatar asked Jul 17 '16 04:07

tim_xyz


4 Answers

implementation 'org.apache.commons:commons-io:1.3.2' contains FileUtils class. You should add this to your dependencies.

like image 165
Shree Krishna Avatar answered Oct 04 '22 19:10

Shree Krishna


I think, in your build.gradle following lines are missing

dependencies {
    compile 'org.apache.commons:commons-io:1.3.2'
}

Add above line to your build.gradle

like image 43
USKMobility Avatar answered Oct 04 '22 19:10

USKMobility


This worked much better for me for newer grade (3+) and dex files conflicts

dependecies { implementation 'commons-io:commons-io:2.4' }

like image 41
user2009449 Avatar answered Oct 04 '22 19:10

user2009449


What worked for me in 2022 is in the build.gradle file in dependencies add:

dependencies {
    (all the other dependencies that you have)  
    implementation 'commons-io:commons-io:2.4'
}

Then, in the Activity.java import:

import org.apache.commons.io.FileUtils;

If I didn´t do that, my program imported another library that doesn´t have FileUtils.copyFile() but instead have FileUtils.copy()

like image 30
Magali Sganga Avatar answered Oct 04 '22 21:10

Magali Sganga