Skip to content Skip to sidebar Skip to footer

Where To Put An Asset File To Be Accessible For An Application?

Got some updates recently, i.e. moved from Eclipse to Android Studio and started using Gradle. Besides, my target API got updated from 16 to 20 and things are not working as they u

Solution 1:

In a classic Android Studio project, assets go in an assets/ directory in your main sourceset, or in another sourceset if appropriate. So, if your Android Studio project has your app in an app/ module of the project, the assets go in app/src/main/assets/. They will then be blended in with the assets from other sourcesets in your project, plus those contributed from Android library projects, and will be bundled into your APK.

Here is a sample app that uses SQLiteAssetHelper for packaging a database with an app. It has the database in the databases/ directory within app/src/main/assets/. The resulting APK has the database right where you would expect:

Android APK, Built By Android Studio, Showing Assets

SQLiteAssetHelper is able to pick up the asset as you would expect.

Note that this project uses appcompat-v7 (and therefore support-v4 through the transitive dependency).

Now, you can have your assets located elsewhere if you prefer, but then you will need to teach Gradle where to find them. For example, my CWAC-RichTextUtils project shares assets between the richtextutils library's tests (in androidTest) and a separate demo module. Rather than have two copies of those assets, I have one at the project level, and I point each module's configuration to those assets in their build.gradle files:

sourceSets {
    main {
        assets.srcDirs = ['../testAssets']
    }
}

Solution 2:

Okay, Ladies and Gentlemen. I got the answer.

If you make your application backwards compatible and import libraries from com.android.support, check subdirectories in that folder: you'll probably have appcompat-v7 and/or support-v4. And in those directories you'll find also assets folders: assetManager.list() from my example refers to them.

Long story short: if you plan to use predefined assets in API 4 and/or API 7, make sure your assets are copied to respective subdirectories beneath com.android.support.

Post a Comment for "Where To Put An Asset File To Be Accessible For An Application?"