Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find symbol class in Android Studio

Tags:

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.

like image 457
shantanu Avatar asked Jan 26 '15 08:01

shantanu


People also ask

How do I get rid of Cannot find symbol error?

Output. In the above program, "Cannot find symbol" error will occur because “sum” is not declared. In order to solve the error, we need to define “int sum = n1+n2” before using the variable sum.

What is Cannot find symbol?

The “cannot find symbol” error comes up mainly when we try to use a variable that's not defined or declared in our program. When our code compiles, the compiler needs to verify all the identifiers we have. The error “cannot find symbol” means we're referring to something that the compiler doesn't know about.


2 Answers

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.

like image 53
WindRider Avatar answered Sep 18 '22 18:09

WindRider


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.

like image 22
PiR Avatar answered Sep 19 '22 18:09

PiR