在Android Studio中找不到符号类

时间:2021-06-17 15:18:15

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.

我有两个项目A和B,其中B被添加为项目A的模块。我在A的Gradle构建文件中添加了依赖项。现在我可以在A中导入B的类而没有任何错误(在编辑器中)但无法构建。偏好是一类项目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.

如果删除导入,我可以成功构建项目(A)。

4 个解决方案

#1


42  

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.

如果库(无论是本地模块还是外部依赖项)的minifyEnabled为true,但库的ProGuard配置缺失或不正确(该类有资格通过ProGuard删除),则会发生这种情况。这导致课程没有被编译。

#2


10  

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

对我来说这是一个类似的问题,但在proguard conf。 proguard在第一个库上处于活动状态,在第二个库中处于非活动状态。

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

所有build.gradle上的Copie相同的proguard conf已经解决了“找不到符号类”的错误。

#3


4  

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.

我已经指出了这个问题。 TargetSdk版本和两个项目的支持包版本不一样。用最新版本更改这些后,我的问题就解决了。

#4


1  

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.

要修复它,我还必须更改minSdkVersion,targetSdkVersion,buildToolsVersion和compileSdkVersion以匹配原始模块中的build.gradle。

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

在我做了这些事情后,错误仍然存​​在,所以我将minifyEnabled设置为false然后编译并运行!

#1


42  

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.

如果库(无论是本地模块还是外部依赖项)的minifyEnabled为true,但库的ProGuard配置缺失或不正确(该类有资格通过ProGuard删除),则会发生这种情况。这导致课程没有被编译。

#2


10  

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

对我来说这是一个类似的问题,但在proguard conf。 proguard在第一个库上处于活动状态,在第二个库中处于非活动状态。

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

所有build.gradle上的Copie相同的proguard conf已经解决了“找不到符号类”的错误。

#3


4  

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.

我已经指出了这个问题。 TargetSdk版本和两个项目的支持包版本不一样。用最新版本更改这些后,我的问题就解决了。

#4


1  

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.

要修复它,我还必须更改minSdkVersion,targetSdkVersion,buildToolsVersion和compileSdkVersion以匹配原始模块中的build.gradle。

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

在我做了这些事情后,错误仍然存​​在,所以我将minifyEnabled设置为false然后编译并运行!