1

Im building an android application(MyApplication) in AOSP enviroment and it uses a library project which has a custom android view that inflates a layout. I have included the library project in the android.mk file of MyApplication. Then i have added the custom android view in MyApplication's layout.

Android.mk of MyApplication has the following entry

LOCAL_STATIC_JAVA_LIBRARIES += mylibrary  

Android.mk of mylibrary has the following entry

include $(CLEAR_VARS)
LOCAL_MODULE := mylibrary
LOCAL_MODULE_TAGS := optional
LOCAL_SDK_VERSION := 21
LOCAL_STATIC_JAVA_LIBRARIES :=android-support-annotations
LOCAL_SRC_FILES := $(call all-java-files-under, library/ui/src/main/java)
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/library/ui/res
LOCAL_AAPT_FLAGS := --auto-add-overlay
LOCAL_MANIFEST_FILE := library/ui/src/main/AndroidManifest.xml
LOCAL_AAPT_FLAGS += --generate-dependencies --extra-packages com.mylibrary.player.ui --auto-add-overlay
include $(BUILD_STATIC_JAVA_LIBRARY)

Now i have included the custom view from the library in to the layout of MyApplicaiton

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">

    <com.mylibrary.player.ui.CustomView
        android:id="@+id/idCustomView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

 

public class CustomView extends LinearLayout {

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.customview, this, true);

        String title;
        String subtitle;
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView, 0, 0);

        try {
            title = a.getString(R.styleable.CustomView_customViewTitle);
            subtitle = a.getString(R.styleable.CustomView_customViewSubtitle);
        } finally {
            a.recycle();
        }

        // Throw an exception if required attributes are not set
        if (title == null) {
            throw new RuntimeException("No title provided");
        }
        if (subtitle == null) {
            throw new RuntimeException("No subtitle provided");
        }

        init(title, subtitle);
    }

    // Setup views
    private void init(String title, String subtitle) {
        TextView titleView = (TextView) findViewById(R.id.customview_textview_title);
        TextView subtitleView = (TextView) findViewById(R.id.customview_textview_subtitle);

        titleView.setText(title);
        subtitleView.setText(subtitle);
    }
}

Now on building the AOSP there is no build error. But there are some runtime exception occured

   Caused by: java.lang.ClassNotFoundException: Didn't find class "com.mylibrary.player.ui.R$layout" 

How can i access resources in the library in the main applicaiton?

Can some one guide me to make it work. Thanks in advance

Bruno Parmentier
  • 1,219
  • 2
  • 14
  • 34
playmaker420
  • 1,527
  • 4
  • 27
  • 52
  • 1
    Have a look at https://stackoverflow.com/questions/17454162/android-mk-include-other-projects - seems you need to put all source files in single Android.mk – Mixaz Dec 01 '17 at 18:45
  • Thank you so much. The link you shared helped me to achieve the result – playmaker420 Dec 04 '17 at 15:57

0 Answers0