Importing Libraries to an Android project - android

I have imported a project from a book on Android development and I am currently getting warnings icons appearing next to a number of imports at the start of my project. When I hover over the warning icon the message that appears is as follows...
"The import java.io.IOException is never used"
I received these warning before on another project and when I imported a the necessary library the warning icons disappeared. My problem is that I do not know which library to import to resolve the warning symbol next to the following statements in my .java file...
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.util.GregorianCalendar;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

I assume you're using Eclipse. Go to Source -> Organize Imports. The unnecessary imports should automatically be removed and only the ones really needed should remain.

Remove
import java.io.IOException;

this is not a problem and you don''t need to import any library to fix this.
All you need to do is "organize imports"
Option 1: right click in the editor ->Source->Organize Imports
Option 2: press [Ctrl]+[Shift]+o

"The import java.io.IOException is never used"
This warning means that you have imported a class java.io.IOException that you are not using in your code. This is not a compiling error so you don't have to fix it in order to run correctly your project.
Anyway if you wanna make the warning vanish there are many ways.The fastest one is to just eliminate this line. If you are using Eclipse and you have many warnings like this then the fastest fix is to put the mouse cursor over the yellow underlined text and wait the open of the quick-fix panel of Eclipse. In this panel you can chose both "remove unused import" or "Organize import". Both this system will remove all your unused import. There is also a third option: "#Suppress warnings". In this case you shouldn't use this option because it will only hide the warning not solving the problem.
Eclipse quick-fix is a powerful feature, using it will fast your code writing a lot so you should start practice it ^^
This kind of error are quiet often in project because many programmers doesn't care a lot for the "unused import warnings" and through the code change they forget to remove useless import

Related

android : navigation drawer and upgrading libraries methodology

Im following headfirst methodology to learn to code in androidstudio.
they provide some code for building a simple navigation drawer app.
in order to be compatible with previous android version, they advise to import :
import android.content.Intent;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
but in my last version of android studio this doesnt work : some of them are greyed and alt+enter does not import the correct library when i put the java code provided by my methodology.
so i replaced a few imports with the newer versions. for example :
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.ActionBarDrawerToggle ;
import android.widget.Toolbar;
however, i did not find anything for to import "NavigationView" and com.google.android.material.navigation.NavigationV iew is grayed when i try to import it.
So my question is basic: how do i know which lib to import when android studio does not suggest anything with alt+enter and when the libraries have evolved? the question could be asked for many programs i guess ..is there a standard methodology to upgrade the code?
You first need to include your dependencies in your build.gradle (app) file.
For example, your NavigationView is a part of Google's Material library. This library does not come included in Android SDK when you create the project and therefore you need to add the library under 'dependencies' in your build.gradle (app) file in order to use it:
dependencies {
implementation 'com.google.android.material:material:1.3.0-alpha01'
}
This way, upon the build of the project, Gradle will download the library and include it in your project so you can use the Material components. You can usually find the gradle dependency you need to include with a quick Google search.
Another thing to note is that the legacy support libraries have been replaced with AndroidX as you can read here:
https://developer.android.com/topic/libraries/support-library
https://developer.android.com/jetpack/androidx
And you can find the full list of AndroidX support libraries in here:
https://developer.android.com/jetpack/androidx/explorer

How to customize Android Studio's import ordering in Kotlin to ignore whether they are "static" imports?

Using the "Optimize Imports" in Android Studio 3.4.1, the imports are ordered similar to this:
import com.walla.walla
import com.willy.willy
import org.koin.android.ext.android.get
import org.koin.androidx.viewmodel.ext.android.viewModel
import kotlin.concurrent.thread // <-- note this line
import kotlin.random.Random
As you can see above, function (a.k.a. "static" import in Java) imports like kotlin.concurrent.thread and kotlin.random.Random are put under other imports.
It is not consistent with the Android Kotlin style guide:
Import statements for classes, functions, and properties are grouped together in a single list and ASCII sorted.
I couldn't find a way to make it such that Android Studio order imports irrespective of whether the import is a class or a function. Is there an option to make it so?
This seems to be a misunderstanding. In fact, kotlin.concurrent.thread is a function. Therefore, it should be grouped together with the other classes.
UPDATE: I do see that the latest version of IntelliJ 2019.1 (and Android Studio) might not be able to conform to the Android Kotlin style guide. If you have these import statements, then IntelliJ does not sort strictly by ASCII:
import org.apache.commons.lang3.StringUtils
import java.util.Base64
import kotlin.concurrent.thread
Instead, IntelliJ orders them as:
Third-party
Java
Kotlin
I do not see a way to configure IntelliJ or Android Studio to sort them this way:
import java.util.Base64
import kotlin.concurrent.thread
import org.apache.commons.lang3.StringUtils
Perhaps you should submit some feedback to IntelliJ or the authors of the Android Kotlin style guide.

Why used import statement is specified as "unused import statement" in Android Studio?

I'm using Android Studio and build as "Generate Signed APK...". And I faced the error, "Unused import statement" like following.
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity; // <- specified as unused ERROR
import android.support.v4.app.FragmentManager; // <- specified as unused ERROR
import android.support.v4.view.ViewPager; // <- specified as unused ERROR
import android.util.Log;
public class Main extends FragmentActivity {
...
}
But I can easily see FragmentActivity is used in code. How can I fix it? This error happens all over my codes...
This is because you have "Optimize imports on the fly" enabled, which automatically removes unused imports. You likely also have "Add unambiguous imports on the fly" selected, which automatically adds imports you need. The solution is to write the code first, and watch your imports be added automatically, and manually add any ambiguous ones, as they become needed.
https://www.jetbrains.com/idea/help/creating-and-optimizing-imports.html?search=optim
In the event this does not work for you, or if you simply prefer to enter your own import statements, then simply disable the features in settings > Editor > Auto Import.
When I updated the android studio to 3.3.1 I go this error.
Solved it by updating the libraries and compileSdkVersion to the latest version that is 27. Hope it will help someone else.
I meet the problem in the Android studio 3.2.1. Invalidate and delete ./idea does not work. It disappear when I upgrade 3.4.1.

android BaseGamesUtils library error

I'm trying to implement the google play services in my app. I have successfully imported BaseGameUtils library from GitHub, referenced the GooglePlayServicesLib to the library, but it has a really annoying error...
This is part of the GameHelper.java file :
import com.google.android.gms.appstate.AppStateManager;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.games.Games;
import com.google.android.gms.games.GamesActivityResultCodes;
import com.google.android.gms.games.multiplayer.Invitation;
import com.google.android.gms.games.multiplayer.Multiplayer;
import com.google.android.gms.games.multiplayer.turnbased.TurnBasedMatch;
import com.google.android.gms.games.request.GameRequest;
import com.google.android.gms.plus.Plus;
Everything is OK except the import com.google.android.gms.games.request.GameRequest; line which is underlined with the error message : com.google.android.gms.games.request cannot be resolved
I have no idea about how to solve this problem, and I see that the help community around google play games services is not as large as I would have bet...
any help would be welcome ! :)
I ran into the same problem. What fixed it for me was updating the google play-services library to a newer version.
Update the google play services library to the latest version. Thanks

The import com.company.projectname.R cannot be resolved

I am developing an application for the company I work for and I am running into an issue with Eclipse. I've been looking online all over and I cannot seem to find a fix for this specific issue. I have changed import android.R; to import com.company.projectname.R; however I get the error "The import com.company.projectname.R cannot be resolved" obviously I replaced company and projectname with the appropriate values.
Here is an example of what the imports should look like:
import android.os.Bundle;
import android.util.Log;
import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ActionBar;
Notice I don't import com.company.projectname.R; Make sure your sdk is updated... After you have done that clean the project.. And rebuild it. You should be fine :D
-Monkey
Try to clean the project, sometimes clean my projects have solved my problems on Eclipse/Android.
If it doesn't work, you can see there if it corresponds with what you did:
Question like yours

Categories

Resources