shantanu
shantanu

Reputation: 2418

Cannot find symbol class in Android Studio

I have two project A and B where B is added as a module of project A. I have added dependencies in A's Gradle build file. Now i can import B's class in A without any error (in editor) but can't build. Preferences is a class of project B.

Error

Error:(22, 23) error: cannot find symbol class Preferences 

A's build file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.0.0"

    defaultConfig {
        applicationId "com.example.A"
        minSdkVersion 9
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile project(':B')
}

B's build file

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: "android-library"

android {
    compileSdkVersion 18
    buildToolsVersion "21.0.0"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 11
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles 'proguard.cfg'
        }
    }
    sourceSets.main {
        jniLibs.srcDir 'src/main/jniLibs'
        jni.srcDirs = [] //disable automatic ndk-build call
    }

    task ndkBuild(type: Exec) {
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            commandLine 'ndk-build.cmd', '-C', file('src/main/jni').absolutePath
        } else {
            commandLine '/opt/adt-bundle-linux/android-ndk-r8e/ndk-build', '-C', file('src/main/jni').absolutePath
        }
        tasks.withType(JavaCompile) {
            compileTask -> compileTask.dependsOn ndkBuild
        }
    }

}

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
}

I can successfully build the project(A) if remove the import.

Upvotes: 21

Views: 53694

Answers (6)

S5tarX
S5tarX

Reputation: 1

Maybe the version of gradle is incorrect?

Upvotes: 0

Yannick
Yannick

Reputation: 5942

I had a similar problem. I have implemented a library B via a local maven repository in my app project A. But some classes from B could not be resolved when building A, wheareas others were working fine. In my case i had to invalidate caches/restart B, in addition to that also delete the module build folder in B, recompile B, and sync A. This was again some wierd bug in AndroidStudio which took me hours solving.

Upvotes: 1

WindRider
WindRider

Reputation: 12318

It can happen if the library (be it a local module or external dependency) has a minifyEnabled true, but library's ProGuard configuration is missing or not correct (the class is eligible for removing by ProGuard). This leads to class not being compiled.

Upvotes: 44

BhavikUp
BhavikUp

Reputation: 105

I had this error come up when I added a new module to my project.

To fix it, I also had to change minSdkVersion, targetSdkVersion, buildToolsVersion, and compileSdkVersion to match the build.gradle in my original module.

After I did these things the error was still coming up so I set minifyEnabled to false and then it compiled and ran!

Upvotes: 2

PiR
PiR

Reputation: 175

For me it was a similar problem but on proguard conf. proguard was active on a first library and inactive on a second.

Copie same proguard conf on all build.gradle has solved the "cannot find symbol class" error.

Upvotes: 10

shantanu
shantanu

Reputation: 2418

I have pointed out the problem. TargetSdk version and support package version of two project are not same. After changing these with latest version, my problem is solved.

Upvotes: 6

Related Questions