Android Studio 应用程序模块到库模块

Android Studio application module to a library module

我正在使用 Android Studio,我有一个应用程序模块,我想将其转换为库模块。

在我的应用程序模块 build.gradle 文件中,我尝试更改

apply plugin: 'com.android.application'

apply plugin: 'com.android.library'

同时删除了我的 applicationId 标签。

这给我 gradle 构建错误,我不知道如何继续。

Gradle 构建消息:

Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.

其中 18 个:

error: constant expression required

我在 MainActivity.java 中引用我的 R.java 文件的地方。

MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.action_settings: //gives error

            break;

        case R.id.action_share: //gives error

            break;

        case R.id.action_about: //gives error

            break;
    }

    return super.onOptionsItemSelected(item);
}

我一直在寻找如何将应用程序模块转换为库模块,上面应该已经解决了我的问题,所以我不知道为什么会出现这些错误。我错过了什么?

由于库项目中的 ADT 14 资源常量不再是最终的。 您可以在此处找到更多信息: .

您应该将 switch 替换为 if-else 语句。如果您使用的是 IntelliJ/Android Studio,您可以在开关上按 Alt + Enter 并选择将 'switch' 替换为 'if'.