Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access assets-pack data in Android (kotlin)

I packed some assets in my Android kotlin app using the asset-pack library: https://developer.android.com/guide/app-bundle/asset-delivery/build-native-java

Not I should be able to access the assets using the AssetManager via context.assets. However, I have no clue what the correct path is.

My assets pack is called models and correctly created in the root folder of the .aab file, so I guess it is also installed correctly. enter image description here

Now according to the documentation (https://developer.android.com/guide/playcore/asset-delivery/integrate-java) I can access the assets like this:

import android.content.res.AssetManager;
...
Context context = createPackageContext("com.example.app", 0);
AssetManager assetManager = context.getAssets();
InputStream is = assetManager.open("asset-name");

where I chane the package name to my package name. But what is the asset name?!? It's not the name of the files in my models asset folder. I tried assetManager.list(p) with p as anything I could think of, but I could not find where my assets are stored.

For p='' the result of assetManager.list(p) is

 hw_pc_white_apps.xml
    hw_pc_white_apps_pad.xml
    images
    permission_grant_policy.xml
    permission_grant_policy_oversea.xml
    pfw
    sfpconfig.json
    ukeyapp.xml
    water.frag
    water.vert
    webkit
    wifi_policy.xml
    wifipro_regexlist.xml

which also makes no sense to me. How can I access assets from asset packs?

like image 590
GenError Avatar asked Jan 24 '23 22:01

GenError


1 Answers

Ok, I found the solution: the Android Studio does not deliver the asset packs by default, the configuration needs to be adjusted under Run -> Edit configuration -> Deliver APK from app bundle (https://developer.android.com/guide/app-bundle/test) to deliver assets and the app.

Now the assets are accessible as if they were assets of the main app.

like image 108
GenError Avatar answered Feb 08 '23 01:02

GenError