R.java没有为Android库应用程序正确生成

mvr*_*rck 7 android

我创建了一个简单的Android库应用程序,只有一个包含TextView的活动.它工作正常,但当我在另一个应用程序中将其标记为库和引用时,当我尝试使用findViewById(R.id.welcome_textview)获取文本视图时,它会出错.

它生成了R.java,但是在我引用库的第二个应用程序中,它不包含id字段.以下是生成的两个R.java文件: -

图书馆应用

    /* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.example.mylibrary;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class id {
        public static final int welcome_textview=0x7f050000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}
Run Code Online (Sandbox Code Playgroud)

其他应用程序中的文件相同

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.example.mylibrary;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

mvr*_*rck 10

经过几个小时的尝试很多东西,终于找到了问题所在.在引用的项目中,有一个自动创建的Layout文件夹,该文件夹为空.删除该文件夹修复了该问题.希望这有助于某人.

  • 我的情况有点不同,但这使我指出了正确的方向.对我来说,引用项目在其布局目录中有一个文件,其名称与引用库中的文件名相同.奇怪的是,这会导致引用库中的某些ID(但不是全部)不可用.删除文件和重建修复了问题. (2认同)

The*_*heo 7

还有一个可能的原因是基于我自己的痛苦经历:在库res/layout和app res/layout文件夹中使用相同名称的XML布局文件.