简体   繁体   English

Android风味-无法解析符号R

[英]Android Flavors - Cannot resolve symbol R

I have two apps in the same codebase. 我在同一代码库中有两个应用程序。 They use the same java files and save res files, except for the values and drawable folders, which have the same files & filenames but different content. 它们使用相同的Java文件并保存res文件,但值和可绘制文件夹除外,它们具有相同的文件和文件名,但内容不同。

The problem I'm having is that when my Gradle builds, I am having a problem because everywhere that I use RI have an error 我遇到的问题是,在构建Gradle时,我遇到了问题,因为我使用RI的每个地方都有一个错误

Cannot resolve symbol R

My build.gradle file looks like this (with a few settings left out for brevity): 我的build.gradle文件如下所示(为简洁起见,省略了一些设置):

android {
    signingConfigs {
        // left out
    }
    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    useLibrary 'org.apache.http.legacy'
    lintOptions {
        disable "ResourceType"
    }
    defaultConfig {
        applicationId = "com.au.testapp"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode = 1
        buildConfigField 'String', 'app_ver_numb', '"1.00"'
        multiDexEnabled = true
    }
    sourceSets {
        main {
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            assets.srcDirs = ['assets']
            res.srcDirs = ['res/main']
            manifest.srcFile 'AndroidManifest.xml'
        }

        flavorone{
            res.srcDirs = ['res/flavorone']
        }

        flavortwo{
            res.srcDirs = ['res/flavortwo']
        }

        instrumentTest.setRoot('tests')
    }

    productFlavors {
        flavorone {
            applicationId "com.au.testapp.flavorone"
            versionName = "''"
            buildConfigField 'String', 'app_id', '"79"'
        }
        flavortwo {
            applicationId "com.au.testapp.flavortwo"
            versionName = "''"
            buildConfigField 'String', 'app_id', '"79"'
        } 
    }

    buildTypes {
        debug {
            productFlavors.flavorone.signingConfig signingConfigs.flavorone
            productFlavors.flavortwosigningConfig signingConfigs.flavortwo 
        }
        release {
            productFlavors.flavorone.signingConfig signingConfigs.flavorone
            productFlavors.flavortwo.signingConfig signingConfigs.flavortwo 

            minifyEnabled true
            shrinkResources true
            proguardFile getDefaultProguardFile('proguard-android.txt')
        }
    }
}

My folder structure looks like this: 我的文件夹结构如下所示:

src
    com
        au
            testapp
                adapter
                utils
                db
                common
                ...
res
    main
        anim
        animator
        ...
        drawable
        layout
        values
        ...
    flavorone
        drawable
        values
    flavortwo
        drawable
        values

When I build this in Gradle I don't get any errors, but when I build the project my java files have errors all through them because they can't find R.. I'm guessing it's to do with the package name not being correct due to my build flavor setup. 当我在Gradle中构建它时,我没有收到任何错误,但是当我构建项目时,我的java文件在整个过程中都出现了错误,因为它们找不到R。我猜想这与软件包名称不相关由于我的构建风格设置而正确。

Any ideas? 有任何想法吗?

Most of the time it is due to a bad XML file. 大多数情况下,这是由于XML文件损坏所致。 XML files can be layout files, value files, or the Manifest file. XML文件可以是布局文件,值文件或清单文件。

1) Check your xml files. 1)检查您的xml文件。

2) import R like this import android.R.*; 2)像这样import android.R.*; or like this import com.au.testapp.R; 或像这样import com.au.testapp.R;

3) Clean the project and rebuild 3)清理项目并重建

Okay, now that my nitpicking is done, here is my actual attempt at an answer: 好的,既然我的挑剔工作已经完成,这是我实际尝试的答案:

I think the issue lies in the fact that you have your resource directories for each flavor inside of res/. 我认为问题在于,您在res /中拥有每种口味的资源目录。 if you look at this post you will see that flavor specific resources should go in the src/flavorName/res directory not in res/flavorname as you have it. 如果您看这篇文章,您会发现风味特定的资源应该放在src / flavorName / res目录中,而不是在res / flavorname中。

While you are at it, go ahead and remove the flavor closures from your source sets closure. 在处理过程中,请继续从源集封包中删除风味封包。 As long as the directory names match the flavor names and you follow the canonical organization of folders you won't need to define the source sets for flavors. 只要目录名称与风味名称匹配,并且您遵循规范的文件夹组织,就无需定义风味的源集。

I don't have enough reputation to comment so I have to answer. 我没有足够的声誉来发表评论,所以我必须回答。

Is your directory in res actually called "flavor1" while the directory name you are using in the build.gradle is called "flavorone"? 您在res中的目录实际上称为“ flavor1”,而在build.gradle中使用的目录名称称为“ flavorone”吗? I noticed that in your post but Im not sure if that is actually the case in your project. 我在您的帖子中注意到了这一点,但是我不确定在您的项目中是否确实如此。 It could be causing the issue if so. 如果是这样,可能是导致问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM