Android install-time asset delivery can't access the asset folder - android

I followed this guide Integrate asset delivery (Kotlin & Java) from step 1-8, to integrate install-time asset delivery. I added folders with drawables to my \asset_pack1\src\main\assets folder, but i can't access them with code or open the folder in android studio. When i build my bundle with gradle, the manifest appers in asset_pack1\build\intermediates\asset_pack_manifest, but the drawable folders don't.
it doesn't show the content of asset_pack1
I've create the build.gradle in my asset_pack1 folder:
plugins {
id 'com.android.asset-pack'
}
assetPack {
packName = "asset_pack1"
dynamicDelivery {
deliveryType = "install-time"
}
}
Setuo the asset pack in my app build.gradle:
android {
..
assetPacks = [":asset_pack1"]
..
}
Included the pack in my settings.gradle:
include ':app'
include ':asset_pack1'
Added my asset directories:
Content of \asset_pack1\src\main\assets
And built the bundle with gradle:
over the menu
Also tried it over the terminal with: ./gradlew bundle
Does anyone know what i did wrong?
I followed the guide multiple times but always with the same result. Could't find any thread describing the same problem.

Related

Use Sample data directory from a library

I created Sample data directory for my Android app using the process described in this article. I would like to share this set of sample data between my projects, so I created a library that only has sample data inside. But as far as I can see sampledata folder is not being compiled into the library. Is there a way to share sample data between multiple Android projects?
As already said, you can't do that with a library because sampledata simply can't be part of an Android library.
One thing you could though, host your names file somewhere and then fetch it with a gradle task, you could just add to an app's build.gradle
clean.doFirst {
println "cleanSamples"
def samplesDir = new File(projectDir.absolutePath, "sampledata")
if (samplesDir.exists()) {
samplesDir.deleteDir()
}
}
task fetchSamples {
println "fetchSamples"
def samplesDir = new File(projectDir.absolutePath, "sampledata")
if (samplesDir.exists()) {
println "samples dir already exists"
return
}
samplesDir.mkdir()
def names = new File(samplesDir, "names")
new URL('http://path/to/names').withInputStream { i ->
names.withOutputStream {
it << i
}
}
}
You can see 2 functions there, the first one is run before a clean task and it will just delete your sampledata folder. The second one is a task run on every build, it won't download the file every time but only if the directory is not there.
I understand you might as well copy paste names file, but, with this method you need to copy paste the tasks only once and you would be able to change names in any project just by uploading a new file and doing a clean build.
The short answer is no, you can't do that with sampledata folder. Basically, the format of the Android Libraries is AAR. If you reference the official documentation, it says that:
The file itself is a zip file containing the following mandatory entries:
/AndroidManifest.xml
/classes.jar
/res/
/R.txt
/public.txt
Additionally, an AAR file may include one or more of the following optional entries:
/assets/
/libs/name.jar
/jni/abi_name/name.so (where abi_name is one of the Android supported ABIs)
/proguard.txt
/lint.jar
So, sampledata can't be a part of AAR library.
UPDATE
Instead of your own data samples, you can use predefined sample resources. For example #tools:sample/first_names will randomly select from some common first names, eg., Sophia, Jacob, Ivan.
Example of usage:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="#tools:sample/first_names" />

Tensorflow android demo . How to retrain?

I am trying to use the android TensorFlow demo TF classify app code to change training data. I want to use two flowers instead just like in this demo: https://medium.com/#daj/creating-an-image-classifier-on-android-using-tensorflow-part-3-215d61cb5fcd
the problem is according to the tutorial im following it says that there should be a assets folder generated and i can put my training data in there and then re-build.
But when i built the tensorflow android demo there was no assets folder created in bazel-bin or in android src folder. i also did a search for assets folder and nothing.
I am using the docker container outlined in the article.
You have to create assets folder by yourself.
If you are using Android Studio, then select menu from
File -> New... -> Folder -> Assets Folder and then paste the files to that folder.
FYI, you also have to create libs folder (in project level) and jniLibs folder.
UPDATE:
After putting those graph file(.pb) and label file (.txt) in that Assets folder, you have to specify and load them from your code.
For example, assuming that your graph file name is "my_graph.pb" and label file is
"my_labels.txt", then specify them as :
private static final String MODEL_FILE = "file:///android_asset/my_graph.pb";
private static final String LABEL_FILE ="file:///android_asset/my_labels.txt";
and you can load them when initializing:
classifier = TensorFlowImageClassifier.create(
getAssets(),
MODEL_FILE,
LABEL_FILE,
IMAGE_SIZE,
IMAGE_MEAN,
IMAGE_STD,
INPUT_NAME,
OUTPUT_NAME);
} catch (final Exception e) {
throw new RuntimeException("Error initializing TensorFlow!", e);
}
Of course don't forget to define other constants (IMAGE_SIZE, IMAGE_MEAN, etc..) with appropriate values before initializing.
UPDATE 2
FYI, here's sample project's app structure in android studio:

openalpr on android - path to config and runtime_data

I want to use open alpr (automatic licences plate recognition) library in my android project. I compiled everything successfully and now it is time to use open alpr in app but...
to create Alpr class object properly I have to provide path to config file and path to runtime_data folder which contains some mandatory files needed by open alpr (ocr and trained data).
I tried something like:
Alpr alpr = new Alpr("eu", "android_assets/alpr.conf", "android_assets/runtime_data");
but Alpr.isLoaded() returns false which means that config or runtime_data have not been found.
Path to assets folder in project is: src/main/assets.
Can someone explain to me how path to "runtime_data" directory and "alpr.conf"
should looks to be visible by open alpr?
Thanks in advance.
I am not familiar with the specific library, but on newer Android devices (Android 6 and up), you can not rely on your application files residing under /data/data/your.package.name
The actual library name still includes the package name of your app, but also has some identifier appended to it in base64 format.
This identifier is unique per installation, and it will change if you uninstall and reinstall the app on the same device.
So, if your library needs to use a configuration file with a path to some other files, there are 2 options:
The right way:
Get the real address of your application files folder using Context.getFilesDir().
Unpack you files from the assets folder of the APK on the device using AssetManager.
Programmatically rewrite your configuration file with the path returned by getFilesDir().
The "hacky" but simpler way:
Use public storage to unpack your files.
You will need to add WRITE_EXTERNAL_STORAGE permission to your app, and unpack the assets files to the external storage.
For backwards compatibility this will be available under /sdcard folder on most Android devices, even with the latest Android version.
The second method is not recommended since using /sdcard directly is deprecated and strongly discouraged by Google.
Also, not all Android devices have /sdcard link to their public storage, but this is the only way to avoid dynamically editing the configuration file after installation.
Important note before you start implementing those steps. This library supports only arm CPU architecture. Good news is, most probably, your physical device is using arm architecture but to make sure just double-check it before implemting those steps.
I've recompiled this library to a new wrapper library. In original library, you need to manually configure openalpr.conf file and edit its content with correct path to your data directory. Manual configuration is cumbersome because since Android 5 multiple user accounts is supported and we can't simply hardcode data directory as /data/data/com.your.packagename/..... Because every user gets their symlink to data directory as /data/user/0/com.your.packagename/..... All those manual steps are gone in recompiled wrapper library.
Implementation
Add this in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency into app module:
dependencies {
...
implementation 'com.github.mecoFarid:openalpr:1.0.0'
}
And you're done. Please check this sample app to get started with UI.
Troubleshooting:
If your target sdk is targetSdkVersion >= 24 and you're running your app on a device with Android API 24+ you'll get following error:
android.os.FileUriExposedException: file:///storage/emulated/0/OpenALPR/2019-09-21-01-32-13.jpg exposed beyond app through ClipData.Item.getUri()
To solve this error: you can add following lines into onCreate() of your Activity as a workaround or you may use this thread for offical solution:
if(Build.VERSION.SDK_INT>=24){
try{
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
}catch(Exception e){
e.printStackTrace();
}
}
TEST:
You can use this image to test your app.
"/data/data/yourpackagename" + File.separatorChar + "runtime_data"
+ File.separatorChar + "openalpr.conf";

android gradle mergeDebugAssets

android project with files in assets, these file need encrypt before generator apk
every times i changed some file in assets,
i need copy out these file , encrypt ,then copy into assets
what i want is:
keep file in assets not encypt (can edit it conveniently) ,
but file in .apk encrypted
encrypt work do automatically by gradle.build
my basic idea is thad add some task before mergeDebugAssets (or mergeReleaseAssets)
before mergeDebugAssets, i replace all file in file://$MODULE_DIR$/build/assets
code like below
task processAssetFile {
// code : replace file in build/assets
}
mergeDebugAssets.dependsOn processAssetFile
the problem is
mergeDebugAssets is not available in gradle.build
error log below:
Could not find property 'mergeDebugAssets' on project ':gradle'.
so is there some idea can achieve my goals ?
android studio ver :0.52
You can always find the task by its name.
I would also suggest that you want to perform encryption for other build variants (makes sense for release even more than for debug).
This requires a little bit of coding since you need to create a task for each variant, too.
It is best to create those tasks dynamically so you don't forget a variant.
Say you're encrypting with some command-line tool, the code you need to add could look like this:
android.applicationVariants.all { variant ->
String suffix = variant.variantData.name.capitalize()
Task mergeAssetsTask = tasks.findByName("merge${suffix}Assets")
Task processAssetFileTask = tasks.create(name: "process${suffix}AssetFile", type: Exec)
processAssetFileTask.commandLine "path/to/your/encryption/tool",
"--input-file=${inputFilePath}",
"--output-file=${outputFilePath}"
mergeAssetsTask.dependsOn processAssetFileTask
}

Unable to create Android Resource Directory for flavor in Android Studio

I have a project with two flavors and decided to add a third. However with the third flavor I am experiencing some problems.
My res file is not defined as an Android resource file, and consequently my layout xml files are not rendered.
You can see the file res file for flavor len does not have the yellow mark on the bottom right like the one for en.
When I right click on the en-res folder I see the create resource file, however I do not see this for my len-res folder.
Is there a way I can tell studio my folder is a resource folder ?
I am running 0.3.7 on Windows
productFlavors {
de {
packageName 'org.rh.ellierides.de'
}
en {
packageName 'org.rh.ellierides'
}
len {
packageName 'org.rh.ellierides.en.lite'
}
I resolved this by changing the build variant to the specific one which was causing me problems in my case len
Once I built this flavor the project self corrected.

Categories

Resources