I created an Android library (AAR) with some text files inside the src / main / assets folder. Text files are used only in the library, and not in the application that uses the library, which is in the same Android Studio project. This should be easy access to these text files, but I cannot do this.
Each text file contains many lines that need to be parsed into a list that will only be used by the library. Using an object Context, this should be easy; however, I am trying to upload files in a static context, and I do not have access to any object Context. Here is the code I tried:
static {
InputStream is = null;
try {
is = Resources.getSystem().getAssets().open("assets/stopwords.txt");
Request.stopwords = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String stopword;
while ((stopword = reader.readLine()) != null) {
Request.stopwords.add(stopword);
}
}
catch (IOException e) {
logger.error("Could not open input stream");
logger.error("Exception: {}", e.getMessage());
}
catch (Exception e) {
logger.error("Input stream opened but something else happened");
logger.error("Exception: {}", e.getMessage());
}
finally {
try {
is.close();
} catch (Exception e) {
logger.error("Could not close input stream");
logger.error("Exception: {}", e.getMessage());
}
}
}
All my assets are in src / main / assets. Here is the build.gradle library:
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
minSdkVersion 24
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
assets.srcDirs = ['src/main/assets']
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'sk.baka.slf4j:slf4j-handroid:1.7.25-3'
compile 'org.ejml:ejml-all:0.31'
}
, : AAR, ? , , , , . !