Androidx RenderScript not run android api<19 - renderscript

I have use androidx with blur image, but when run android api< 19 crash app.
When i run with android>19, i run normal,not crash app and if i use android normal with "android.support.v8.renderscript" no crash app.
At build.gradle. I have add :
renderscriptTargetApi 18
renderscriptSupportModeEnabled true
Code app:
public static Bitmap blurBitmap(Bitmap bitmap,
float radius) { //Create renderscript
RenderScript
rs = RenderScript.create(MyApplication.getInstance());
//Create allocation from Bitmap
Allocation allocation = Allocation.createFromBitmap(rs,
bitmap);
Type t = allocation.getType();
//Create allocation with the same type
Allocation blurredAllocation = Allocation.createTyped(rs,
t);
//Create script
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8(rs));
//Set blur radius (maximum 25.0)
blurScript.setRadius(radius);
//Set input for script
blurScript.setInput(allocation);
//Call script for output allocation
blurScript.forEach(blurredAllocation);
//Copy script result into bitmap
blurredAllocation.copyTo(bitmap);
//Destroy everything to free memory
allocation.destroy();
blurredAllocation.destroy();
blurScript.destroy();
t.destroy();
rs.destroy();
return bitmap;
}

Edit:
This issue is now fixed in the latest build tools. I've verified this by upgrading compile sdk to API 29 and build tools to 29.0.2.
This has nothing to do with your implementation. It's a bug in the androidx library and in fact, it happens for me even on API 21 so it might have a bigger impact than you've experienced.
Someone has already filed issues here and here. I've been following on the issue for quite sometime unfortunately there isn't much progress going on. This is currently a showstopper for me to migrate to AndroidX for many of my projects.

The Element.U8 argument for ScriptIntrinsicBlur.create() is not correct.
The ScriptIntrinsicBlur is expecting an Allocation with elements of type Element.U8, but a Bitmap-backed Allocation has elements of type Element.RGBA_8888 (a.k.a Element.U8_4).
try:
ScriptIntrinsicBlur.create(rs, Element.RGBA_8888(rs))
or in general:
ScriptIntrinsicBlur.create(rs, allocation.getElement())

Related

Converting Bitmap to Texture2D crashes unity

I tried many ways to do this, Want to show a bitmap in a Unity image(UI) tried all the ways I can find. I can do it by saving the file to disk and then reading it but that's too costly as it needs to be done in every frame.
I checked image is loaded correctly but the problem is in converting to Texture2D and also saving it to memory stream and reading the file also crashes unity, I tried in Unity 2017 2018 2019 all crashes
tried all this ,this
*Only need to make this work in desktop Build if it helps
Bitmap bitmap = new Bitmap(#"C: \image1.jpg");
final = UnmanagedImage.FromManagedImage(bitmap);
Texture2D convertedTx;
convertedTx = Texture2D.CreateExternalTexture(final.Width, final.Height, TextureFormat.ARGB32, false, false, final.ImageData);
//Convert UnmanagedImage to Texture
convertedTx.UpdateExternalTexture(final.ImageData);
display.texture = convertedTx;
I think you didn't put this code in an enumeration, so its run on the main thread and cause unity crash

64 bit show stopper error for appbundle flutter application

I am trying to publish our flutter application to the google play store and Im getting what appears to be a show stopper error...
Error
This release is not compliant with the Google Play 64-bit requirement
The following APKs or App Bundles are available to 64-bit devices, but they only have 32-bit native code: 3.
Include 64-bit and 32-bit native code in your app. Use the Android App Bundle publishing format to automatically ensure that each device architecture receives only the native code it needs. This avoids increasing the overall size of your app. Learn More
Does anyone know how to fix this? Im not seeing anything when I google this error. We are just trying to get a Closed Alpha test out there.
I was able to finally fix this by adding this to build.gradle
ndk {
abiFilters "armeabi-v7a", "arm64-v8a"
}
}
splits {
abi {
include "armeabi-v7a", "arm64-v8a"
}
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
// For each separate APK per architecture, set a unique version code as described here:
// http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
def versionCodes = ["armeabi-v7a":1, "arm64-v8a":2]
def abi = output.getFilter(OutputFile.ABI)
if (abi != null) { // null for the universal-debug, universal-release variants
output.versionCodeOverride =
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
}
}
}

UnloadUnusedAssets doesn't appear to be freeing memory

I'm working on a game in Unity where a sprite is imported using Resources.Load(), and as expected, the memory increases.
But the problem is, I can't seem to figure out how to free the memory.
I know I need to use Resources.UnloadUnusedAssets(), but even when I remove every reference to the sprite that I can think of, the memory doesn't go back down.
Here's my code right now:
Loading the sprite (works fine):
map = Resources.Load <Sprite> ("Map7");
spRend = this.GetComponent<SpriteRenderer>();
spRend.sprite = map;
Unloading the sprite (memory isn't freed):
spRend.sprite = null;
map = null;
Resources.UnloadUnusedAssets();
UPDATE:
After further research, I found this on the Unity wiki:
When not used anymore, you can free the memory it took up by calling Object.Destroy on the object, followed by Resources.UnloadUnusedAssets
But the problem is when I try to destroy 'map', I get the following error:
Destroying assets is not permitted to avoid data loss.
If you really want to remove an asset use DestroyImmediate (theObject, true);

AssetBundles on iOS: Memory always increases, causing crash

We have our asset bundles stored on an Amazon S3 bucket. When the game starts, it determines which bundles it needs to download new versions of, using WWW.LoadFromCacheOrDownload.
The problem we're running into is the memory iOS reports it has allocated for our app keeps increasing, however the memory Unity reports it's using (through the profiler) always stays the same. We have enough bundles that by the time it has finished downloading everything we need, it has invariably received a memory warning from iOS, and we are shutdown due to memory pressure shortly after.
Common solutions we have in place already: Unloading the assetbundle after the WWW is finished, using assetBundle.unload(), calling Resources.UnloadUnusedAssets(), and calling Dispose() on the WWW. None of it is solving the problem.
Code follows:
private IEnumerator DownloadBundle(DownloadQueueEntry entry, DownloadFinishedCallback callback)
{
while (!entry.finished)
{
// grab bundle off S3
string url = string.Format(BUNDLE_URL_FORMAT, entry.directory, entry.assetName);
WWW www = WWW.LoadFromCacheOrDownload(url, entry.version);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
Debug.Log("[BundleDownloader] Download Completed " + entry.assetName);
entry.finished = true;
entry.downloading = false;
www.assetBundle.Unload (true);
Resources.UnloadUnusedAssets ();
}
else
{
// usually timed out resolving host, just try again for now
Debug.LogError("[BundleDownloader] Download failed: " + url + " Error: " + www.error);
}
www.Dispose();
www = null;
}
if(callback != null)
{
callback ();
}
}
--edit--
A screenshot showing the increasing memory usage is at the link below. Memory usage proceeds like that until it has chewed up around 150MB. This is all in an scene that only has a GameObject for init scripts in it (no art or anything).
https://www.dropbox.com/s/3b6skexz6xhug5g/Screenshot%202014-03-28%2014.54.26.png
As the Unity docs suggest, you should really be encapsulating your usage of the WWW object/caching routines in a "using" statement block.
using System;
using UnityEngine;
using System.Collections;
public class CachingLoadExample : MonoBehaviour {
public string BundleURL;
public string AssetName;
public int version;
void Start() {
StartCoroutine (DownloadAndCache());
}
IEnumerator DownloadAndCache (){
// Wait for the Caching system to be ready
while (!Caching.ready)
yield return null;
// Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
using(WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)){
yield return www;
if (www.error != null)
throw new Exception("WWW download had an error:" + www.error);
AssetBundle bundle = www.assetBundle;
if (AssetName == "")
Instantiate(bundle.mainAsset);
else
Instantiate(bundle.Load(AssetName));
// Unload the AssetBundles compressed contents to conserve memory
bundle.Unload(false);
} // memory is freed from the web stream (www.Dispose() gets called implicitly)
}
}
"using" statements are a C# feature to ensure that "Dispose()" methods work correctly (and in many cases, are called automagically for you).
As stated in MS's docs: http://msdn.microsoft.com/en-us/library/yh598w02.aspx
"[using] Provides a convenient syntax that ensures the correct use of IDisposable objects."
I'm guess your memory leak is due to these functions not performing as intended (possibly due to improper configuration, not sure).
I had similar issues on AssetBundle downloading on iPad. However, in my case;
I was downloading assetbundle from server (without using cache) and loading it later. In one asset bundle, it boosts the memory if the asset bundle has images more than 100 or lets say 200.
This was not UnityDestroyWWWConnection or "using" issue. I was downloading 60MB asset bundle (with using best-compression on asset bundle generation) and it uses about 450MB while downloading.
I checked the result from instrument and saw tons of small malloc while downloading asset bundle. You can check the instrument screenshot from here.
My guess - not sure: Unity extract the information from asset bundle while downloading continues, which gives error on memory. It gets the header info first, and understands that it is unity3d object and because it downloads with www class it prepare for WWW asset bundle.
My solution was:
Compressing assetbundle and putting server as zip.
Downloading asset bundle zip and extracting on iPad (using SharpZipLib)
We ran into a similar issue in our app. We were loading lots of textures from WWW and noticing iOS being the only platform to have a memory leak from it. We eventually found our solution here http://forum.unity3d.com/threads/www-memory-leak-ios.227753/. Basically, there is a known issue in unity 4.3 that leaks the data from www calls. This SHOULD be fixed in unity 4.5. In the meantime, you can follow Alexey's suggestion to modify the code in the generate xcode project or update to 4.5:
4.5 will have the fix.
Essentially you need:
search for
extern "C" void UnityDestroyWWWConnection(void* connection)
in WWWConnection.mm
[delegate.connection cancel];
delegate.connection = nil;
[delegate.data release]; // <-- ADD THIS
[delegate release];

Camera::connect() brings AppOps Bad Call: on Android 4.4?

I wrote an apk to test camera on Android 4.2.2 before. This apk works fine.
However, when I moved this apk to Android 4.4.
I got a problem with Camera::connect().
Fail to call Camera::connect() and it prints message:
W/AppOps ( 1546): Bad call: specified package TestCamera under uid 1000 but it is really -1
I think the reason may be USE_CALLING_UID, security or something that I can't figure out.
Please give me some suggestions, thanks!
My apk is very simple, only one activity. In onCreate(), I called a jni function.
The jni function just do the code belowed:
int cameraId = 0;
String16 clientPackageName("TestToGoService");
sp<Camera> camera = Camera::connect(cameraId, clientPackageName, Camera::USE_CALLING_UID);
if (camera == NULL) {
ALOGE("camera==NULL.");
return -1;
}
ALOGV("camera=%p.",camera.get());
Try:
If I put the code above to a executable (main()), then Camera::connect() works OK.
I have already add permissons on AndroidManifest.xml
Thanks again!
I'm not sure if it's still of any help. I had the same error in the past. The problem is clientPackageName, that has to be set to the exact package name of your application (which must have the proper camera permissions set on the manifest).