What assets are used in Library project?
-
29-06-2021 - |
Question
There's something magic going on in one of my library projects. While layouts (res/layout) are fetched from the library project itself, files within the assets folder are fetched from the calling project (not the library project).
I'm currently building a help activity in a library project. The calling project gives a filename to the activity in the library project. The files are stored in the calling project - not in the library project.
When using the AssetsManager in the library project it uses the files from the calling project - not from the assets folder of the library project.
Is this correct behaviour?
Here's the stripped down calling Activity in the root project:
// Calling Project
package aa.bb.aa;
public class MyListActivity extends ListActivity {
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.mylistactivity); // <--- Stored in resources of the calling project
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.men_help) {
Intent intent = new Intent(this, aa.bb.bb.MyFileBrowser.class);
intent.putExtra(MyConstants.FILE, "mylistactivity.html"); // <-- Stored in assets of the calling project
startActivityForResult(intent, MyConstants.DIALOG_HELP);
return true;
}
return super.onOptionsItemSelected(menuItem);
}
}
And here's the Activity in the Library project. I thought that assets are fetched from here and not from the calling Activity/Project:
// Library project
package aa.bb.bb;
public class MyFileBrowser extends Activity {
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.myfilebrowser); // <-- Stored in resources of the library project
webView = (WebView) findViewById(R.id.browser);
Locale locale = Locale.getDefault();
String localeLanguage = (locale.getLanguage() != null) ? locale.getLanguage() : "en";
Bundle bundleExtras = getIntent().getExtras();
if (bundleExtras != null) {
String file = bundleExtras.getString(MyConstants.FILE);
if (!StringUtils.isEmpty(file)) {
String filename = "help-" + localeLanguage + File.separator + file;
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open(filename);
} catch (IOException ioException) {
filename = "help-en" + File.separator + file;
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException ioException) {
}
}
webView.loadUrl("file:///android_asset" + File.separator + filename); // <-- Uses file in assets of calling project - not library project
}
}
}
}
Solution
Found the answer. assets are always used from the referencing project. A library project can't hold assets. I found it on this page:
Here's the part of the doc that does describe this:
The tools do not support the use of raw asset files (saved in the assets/ directory) in a library project. Any asset resources used by an application must be stored in the assets/ directory of the application project itself. However, resource files saved in the res/ directory are supported.