Can I use an Android AAR's resources in an APK that depends on it?

Question!

Can I use resources (strings, assets, drawables) from an Android AAR (lib1) in an APK that depends on it? What about resources in a transitive AAR (lib2) that lib1 depends on?



Answers

Yes.

Using Android Archetypes I created an android-with-test project, and then two android-library-quickstarts underneath it. I had to change the android-library-quickstart projects' packaging from apklib to aar. Then, I make lib1 depend upon lib2, and made my apk depend on lib1.

I constructed an Activity that directly consumed resources (images from res/drawable, fonts from assets/fonts, and strings from values/strings.xml) in its layout from both libraries. Even though lib2 was a transitive dependency, I could use its resources.

activity_main.xml

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/library_1_string" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/library_2_string"/>
    <TextView
        android:id="@+id/library2_via_library1_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/library1" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/library2" />

    <TextView
        android:id="@+id/library1_font_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="library1.ttf"/>

    <TextView
        android:id="@+id/library2_font_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="library2.ttf"/>
</LinearLayout>

HelloAndroidActivity.java public class HelloAndroidActivity extends Activity {

    /**
     * Called when the activity is first created.
     * @param savedInstanceState If the activity is being re-initialized after 
     * previously being shut down then this Bundle contains the data it most 
     * recently supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it is null.</b>
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView library2ViaLibrary1TextView = (TextView) findViewById(R.id.library2_via_library1_text_view);
        library2ViaLibrary1TextView.setText(Library1.getLibrary2String(this));

        TextView library1FontTextView = (TextView) findViewById(R.id.library1_font_text_view);
        library1FontTextView.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/library1.ttf"));

        TextView library2FontTextView = (TextView) findViewById(R.id.library2_font_text_view);
        library2FontTextView.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/library2.ttf"));
    }
}


This video can help you solving your question :)
By: admin