Marcus
Marcus

Reputation: 6717

Android Studio application module to a library module

I'm using Android Studio and I have an application module that I want to convert to a library module.

In my application module build.gradle file, I've tried changing

apply plugin: 'com.android.application'

to

apply plugin: 'com.android.library'

also removing my applicationId tag.

This is giving me gradle build errors, and I do not know how to proceed.

Gradle Build Message:

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

And 18 of these:

error: constant expression required

Where I'm referencing to my R.java files in MainActivity.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);
}

I've been searching on how to convert an application module to a library module for a while, and above should have solved my problem so I don't know why I am getting these errors. What am I missing?

Upvotes: 2

Views: 798

Answers (1)

Ziem
Ziem

Reputation: 6697

Since ADT 14 resource constants in library projects are no longer final. You can find more information here: https://stackoverflow.com/a/7840985/759007 .

You should replace switch with if-else statements. If you are using IntelliJ/Android Studio you can press Alt + Enter on switch and choose Replace 'switch' with 'if'.

Upvotes: 4

Related Questions