Android Studio Find Unused Classes



In Java, if you receive ”cannot resolve symbol” error, it simply mean that you have used a name that the compiler was not able to recognize.

1. Class name

You can easily search for unused resources from Android Studio. Just press CtrlAltShiftiand type 'unused resources' (without quotes). That will execute lint. Super easy way to run lint commands (and other stuff from IDE). In Android Studio we need to install Gherkin plugin. This plugin provides Gherkin language support. Gherkin is the language that Cucumber uses to define test cases (test scenarios). The unused declarations IDEA (and AFAIK, NetBeans) looks for are for private members and local variables: things that are not accessible, even dynamically, from outside that class or scope. (Well, at least without doing things with Reflection or JVM hacking that you're not supposed to.).

If you do misspell in the class name, the class won’t get imported properly, and the compiler won’t be able to find the class. These names are case sensitive and you must be specifically careful with them, or the class simply won’t get recognized by the compiler.

Another probability is that the class is not available in the current working directory. This happens after a renaming of package, so be sure that all the required imports got renamed, and check for if the CLASSPATH has ‘.’ ‘the current directory’ in it.

2. Variable name

Implementation Details LintClient. Lint is highly embeddable; it’s been integrated natively in Android Studio/IntelliJ, Eclipse, the Android Gradle plugin, Jenkins, as a command line tool (“lint”) distributed with the SDK - and this tool is itself used to integrate lint in tools like Tricorder and Maven. LintClient is the abstraction which makes this possible. Android tools project information site. Again, you have to enable minifyEnabled in order to turn on code shrinking, and then shrinkResources to turn on resource shrinking. If you have not already been using minifyEnabled, make sure you get that working before also adding shrinkResources, since you may have to edit your proguard-rules.pro file to make sure any methods you access with reflection.

Android Studio Find Unused Classes Online

If the issue is in a variable name, then most probably you did not declare your variable before you try to use it. This is also case sensitive, and so check for if you did misspell it.

Another chance is that the variable is written in the wrong scope. Like if you declare the variable in the method A and try to access it from method B. If you need to access the variable in both the methods, you should make it common to both by declaring it in the main method.

class A {
int a=5;
}
class B {
System.out.println(a); //a cannot be resolved
}
}

3. Methods

You might have given the method the wrong parameter datatypes, For example, calling an integer where you need a String value.

Another problem might be when you try to invoke a constructor that is not within the scope.

class A
{
private A(int a)
{
}
}

A a=new A(5); //this won’t work, unless you change the scope ‘private’ to ‘public’

A a=new A(“Hello world!”); //this won’t work, unless you change the Parameter type to String

This should be written like

class A
{
A(String a)
{
}
}

4. Missing resources R

All the files in the resources directory are assigned an integer value in a common class file called R.java, which is auto generated at build time. This acts as a pointer to all the files in the resources folder. The compiler during compilation simply checks for the R.java file and picks the correct resource from the resources directory using this R.java class file.

Once the compiler picks up the wrong value, it eventually misses the resource file and that is when missing R problem is notified. This happens if you accidentally delete the R.java file, or after a package rename if the CLASSPATH miss the ‘.current directory’.

When it comes to Android Studio or IntelliJ, a simple Invalidate and refresh of the IDE’s caches might address the problem.

  • You can safely delete the ‘build’ folder that is in the app directory.
  • Then File -> Invalidate Caches / Restart
  • After the project is reloaded and indexed, do a clean and rebuild from the build menu, so that build folder will be regenerated and the missed Resources error is gone!

It will still show “cannot resolve R” error, only if you have performed refactoring the package name before. In such case, try importing the R by pressing ALT + ENTER simultaneously.

This happens when you’re using the old version of basegameutils files in your project.

AppStateManager is deprecated, and Basegameutils is updated now.

Simply replace the class files GameHelper.java and BaseGameActivity.java with the latest ones found here

In this document

  1. Adding Support Libraries
  2. Using Support Library APIs

See also

How you setup the Android Support Libraries in your development project depends on what features you want to use and what range of Android platform versions you want to support with your application.

This document guides you through downloading the Support Library package and adding libraries to your development environment.

Downloading the Support Libraries

The Android Support Library package is provided as a supplemental download to the Android SDK and is available through the Android SDK Manager. Follow the instructions below to obtain the Support Library files.

To download the Support Library through the SDK Manager:

  1. Start the Android SDK Manager.
  2. In the SDK Manager window, scroll to the end of the Packages list, find the Extras folder and, if necessary, expand to show its contents.
  3. Select the Android Support Library item.

    Note: If you're developing with Android Studio, select and install the Android Support Repository item instead.

  4. Click the Install packages... button.

Figure 1. The Android SDK Manager with theAndroid Support Library selected.

After downloading, the tool installs the Support Library files to your existing Android SDK directory. The library files are located in the following subdirectory of your SDK: <sdk>/extras/android/support/ directory.

Choosing Support Libraries

Before adding a Support Library to your application, decide what features you want to include and the lowest Android versions you want to support. For more information on the features provided by the different libraries, see Support Library Features.

Adding Support Libraries

In order to use a Support Library, you must modify your application's project's classpath dependencies within your development environment. You must perform this procedure for each Support Library you want to use.

Some Support Libraries contain resources beyond compiled code classes, such as images or XML files. For example, the v7 appcompat and v7 gridlayout libraries include resources.

If you are not sure if a library contains resources, check the Support Library Features page. The following sections describe how to add a Support Library with or without resources to your application project.

Adding libraries without resources

To add a Support Library without resources to your application project:

  1. Make sure you have downloaded the Android Support Library using the SDK Manager.
  2. Create a libs/ directory in the root of your application project.
  3. Copy the JAR file from your Android SDK installation directory (e.g., <sdk>/extras/android/support/v4/android-support-v4.jar) into your application's project libs/ directory.
  4. Right click the JAR file and select Build Path > Add to Build Path.
  1. Make sure you have downloaded the Android Support Repository using the SDK Manager.
  2. Open the build.gradle file for your application.
  3. Add the support library to the dependencies section. For example, to add the v4 support library, add the following lines:

Adding libraries with resources

To add a Support Library with resources (such as v7 appcompat for action bar) to your application project:

Create a libraryproject based on the support library code:

  1. Make sure you have downloaded the Android Support Library using the SDK Manager.
  2. Create a library project and ensure the required JAR files are included in the project's build path:
    1. Select File > Import.
    2. Select Existing Android Code Into Workspace and click Next.
    3. Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding the appcompat project, browse to <sdk>/extras/android/support/v7/appcompat/.
    4. Click Finish to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat.
    5. In the new library project, expand the libs/ folder, right-click each .jar file and select Build Path > Add to Build Path. For example, when creating the the v7 appcompat project, add both the android-support-v4.jar and android-support-v7-appcompat.jar files to the build path.
    6. Right-click the library project folder and select Build Path > Configure Build Path.
    7. In the Order and Export tab, check the .jar files you just added to the build path, so they are available to projects that depend on this library project. For example, the appcompat project requires you to export both the android-support-v4.jar and android-support-v7-appcompat.jar files.
    8. Uncheck Android Dependencies.
    9. Click OK to complete the changes.

You now have a library project for your selected Support Library that you can use with one or more application projects.

Add the library to your application project:

  1. In the Project Explorer, right-click your project and select Properties.
  2. In the category panel on the left side of the dialog, select Android.
  3. In the Library pane, click the Add button.
  4. Select the library project and click OK. For example, the appcompat project should be listed as android-support-v7-appcompat.
  5. In the properties window, click OK.

Note: If you are using the android-support-v7-mediarouter support library, you should note that it depends on the android-support-v7-appcompat library. In order for the v7 mediarouter library to compile, you must import both library projects into your development workspace. Then follow the procedure above to add the v7 appcompat project as a library to the v7 mediarouter library project.

  1. Make sure you have downloaded the Android Support Repository using the SDK Manager.
  2. Open the build.gradle file for your application.
  3. Add the support library feature project identifier to the dependencies section. For example, to include the appcompat project add compile 'com.android.support:appcompat-v7:18.0.+' to the dependencies section, as shown in the following example:

Using Support Library APIs

Support Library classes that provide support for existing framework APIs typically have the same name as framework class but are located in the android.support class packages, or have a *Compat suffix.

Caution: When using classes from the Support Library, be certain you import the class from the appropriate package. For example, when applying the ActionBar class:

  • android.support.v7.app.ActionBar when using the Support Library.
  • android.app.ActionBar when developing only for API level 11 or higher.

Android Studio Find Unused Classes For Beginners

Android Studio Find Unused Classes

Note: After including the Support Library in your application project, we strongly recommend using the ProGuard tool to prepare your application APK for release. In addition to protecting your source code, the ProGuard tool also removes unused classes from any libraries you include in your application, which keeps the download size of your application as small as possible. For more information, see ProGuard.

Further guidance for using some Support Library features is provided in the Android developer training classes, guides and samples. For more information about the individual Support Library classes and methods, see the android.support packages in the API reference.

Manifest Declaration Changes

Android Studio Find Unused Classes Near Me

Find

If you are increasing the backward compatibility of your existing application to an earlier version of the Android API with the Support Library, make sure to update your application's manifest. Specifically, you should update the android:minSdkVersion element of the <uses-sdk> tag in the manifest to the new, lower version number, as shown below:

This change tells Google Play that your application can be installed on devices with Android 2.1 (API level 7) and higher.

Android Studio Find Unused Classes Free

Note: If you are including the v4 support and v7 appcompat libraries in your application, you should specify a minimum SDK version of '7' (and not '4'). The highest support library level you include in your application determines the lowest API version in which it can operate.

Code Samples

Each Support Library includes code samples to help you get started using the supportAPIs. The code is included in the download from the SDK Manager and is placed inside the AndroidSDK installation directory, as listed below:

  • 4v Samples: <sdk>/extras/android/support/samples/Support4Demos/
  • 7v Samples: <sdk>/extras/android/support/samples/Support7Demos/
  • 13v Samples: <sdk>/extras/android/support/samples/Support13Demos/
  • App Navigation: <sdk>/extras/android/support/samples/SupportAppNavigation/