Search code examples
androidandroid-activityandroid-library

Using activities within libraries in main application project


I am working on an Android project where I have a library project which has an activity.

I have a main app project which references the library project and tries to start the activity within the library project. In the library project I have the following activity:

public class DirectoryPicker extends Activity
{

    GridView gridView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.directory_picker);

        gridView = (GridView)findViewById(R.id.directory_picker_gridview);

        DirectoryAdapter.directories.add("String1");
        DirectoryAdapter.directories.add("String2");
        DirectoryAdapter.directories.add("String3");
        DirectoryAdapter.directories.add("String4");

        gridView.setAdapter(new DirectoryAdapter());
    }
}

I am trying to start the activity using the following in my main application project.

protected OnPreferenceClickListener mPrefDefaultBackupClickListener = new OnPreferenceClickListener()
        {
            @Override
            public boolean onPreferenceClick(Preference preference)
            {
                Intent intent = new Intent(getActivity(), DirectoryPicker.class);
                startActivity(intent);
                return false;
            }
        };

In my main application project AndroidManifest file I've added the following:

<activity android:name="com.MyCompany.Library.DirectoryPicker">

        </activity>

When I try and launch the activity I get the following exception:

java.lang.NoClassDefFoundError: com.MyCompany.Library.DirectoryPicker

I can't see any reason why it doesn't work.


Solution

  • I've managed to find out the problem finally. Didn't manage to find any documentation but just trial and error and found if I go to File > Project Structure > Depdencies and then select my library and change the scope from compile to Provided and clean and rebuild everything the activity is then launched correctly.