3

我可以在依赖于它的 Android AAR(lib1) 中使用资源(字符串、资产、drawables)吗?lib1 所依赖APK的传递(lib2)中的资源呢?AAR

4

1 回答 1

0

是的。

我使用Android Archetypes创建了一个android-with-test项目,然后android-library-quickstarts在它下面创建了两个。我不得不将android-library-quickstart项目的包装从更改apklibaar. 然后,我让 lib1 依赖于 lib2,并使我的 apk 依赖于 lib1。

我从两个库的布局中构建了一个Activity直接消耗资源(来自 的图像、来自res/drawable的字体和来自的assets/fonts字符串)的布局。values/strings.xml即使 lib2 是传递依赖项,我也可以使用它的资源。

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 公共类 HelloAndroidActivity 扩展 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"));
    }
}
于 2014-08-06T19:19:52.363 回答