Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
ringu0
Feb 24, 2013


I'm using retrofit2 with Jackson converter to communicate with a web service. One of the methods of this service accepts a POST request with a JSON object in its body. That JSON object must contain a JSON array of strings. On the client side this array is represented by an ArrayList<String>.

Everything works fine when there are two or more strings in the list. However, if there is only one string in that list, it's serialized as a JSON object instead of a JSON array, which cannot be accepted by the web method. How to fix it?

I am aware of SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, but, according to Jackson documentation, it's disabled by default. Providing a custom ObjectMapper with that feature explicitly disabled doesn't have any effect.

Web method declaration:
pre:
@POST("api/FindByFilter")
@FormUrlEncoded
Call<BaseApiResponse<String>> findByFilter(@Field("Filter") List<String> filter);
How it's called:
pre:
Call<BaseApiResponse<String>> call = api.findByFilter(getFilter());
What web method expects:
pre:
{ "Filter" : [ "test" ] }
What it actually receives:
pre:
{ "Filter" : "test" }
e: missed field annotation

ringu0 fucked around with this message at 23:41 on Mar 11, 2019

Adbot
ADBOT LOVES YOU

PokeJoe
Aug 24, 2004

hail cgatan


Not too familiar with jackson's specifics but it sounds like you might be able to write a type converter that will place the object in a list of 1 or something like that. That's what I would do with logansquare or moshi

ringu0
Feb 24, 2013


Yeah, that's a possibility, I just think I'm missing something incredibly obvious in the way retrofit is set up or used.

e: Ended up ditching individual parameters and their annotations and building the request body manually:

Web method declaration:
pre:
@POST("api/FindByFilter")
@FormUrlEncoded
Call<BaseApiResponse<String>> findByFilter(@Field("Filter") List<String> filter @Body HashMap<String, Object> requestBody);

ringu0 fucked around with this message at 20:07 on Mar 12, 2019

Froist
Jun 6, 2004

I'm an iOS developer just dipping my toes into Android development for the first time, but have hit some memory leaks on rotation. I've stripped my app back to the bare basics but I'm still seeing the leak (albeit smaller) in the profiler, and I'm not really sure which lifecycle events I'm missing where I presumably need some explicit cleanup.

MainActivity:
code:
class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    supportFragmentManager
      .beginTransaction()
      .replace(R.id.fragment_container, MapFragment())
      .commit()
  }
}
MapFragment:
code:
class MapFragment : Fragment(), OnMapReadyCallback
{

  private lateinit var mMap: GoogleMap

  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_map, container, false)
  }

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val mapView = view.findViewById<MapView>(R.id.map)
    mapView.onCreate(savedInstanceState)
    mapView.onResume()
    mapView.getMapAsync(this)
  }

  override fun onMapReady(googleMap: GoogleMap) {
    mMap = googleMap
  }
}
Layout-wise the main activity is literally just a FrameLayout, and fragment_map is just another FrameLayout with a MapView as its only child.

I don't have any pause/stop/destroyView methods implemented - is there anything here I need to explicitly destroy or disconnect to avoid leaks?

Froist fucked around with this message at 22:55 on May 3, 2019

Lutha Mahtin
Oct 10, 2010

Your brokebrain sin is absolved...go and shitpost no more!

code:
private val vm: QuestsViewModel by lazy {
    ViewModelProviders.of(this).get(QuestsViewModel::class.java)
  }
I don't know Kotlin syntax so this is probably a really babby-level question, but what's going on with this bit that you have in both classes? Based on two minutes of googlin' it looks like a variable assignment that is (effectively) deferred until the first time it's used in the code but then... the variable "vm" isn't actually used anywhere in the code?

Froist
Jun 6, 2004

Lutha Mahtin posted:

code:
private val vm: QuestsViewModel by lazy {
    ViewModelProviders.of(this).get(QuestsViewModel::class.java)
  }
I don't know Kotlin syntax so this is probably a really babby-level question, but what's going on with this bit that you have in both classes? Based on two minutes of googlin' it looks like a variable assignment that is (effectively) deferred until the first time it's used in the code but then... the variable "vm" isn't actually used anywhere in the code?

Sorry, I went back and heavily edited my post as I stripped all the ViewModel handling out and still saw the leak in a really minimal app.

But to answer your question: I store my app's data objects (parsed JSON data from a server) within the ViewModel, and need to access this in a few different fragments - I omitted the others from my first post because I could repro the leak without ever loading those fragments. "vm" actually is used in my code later, as it takes a load of coordinates from those model objects and plots them on the map. It was my understanding (I may have this wrong!) that the ViewModel should be owned by the Activity (my app only has one) and looked up in each Fragment, as it's intended to have a lifecycle beyond that of any single fragment.

Again I stripped all this ViewModel code out completely and the leak's still there, but if I'm doing something drastically wrong in the VM handling I'm happy to accept tips!

Lutha Mahtin
Oct 10, 2010

Your brokebrain sin is absolved...go and shitpost no more!

i was hoping i'd be able to pick it apart just by skimming, but i haven't been doing any android programming for months so i don't remember any tips and tricks when it comes to the process lifecycle. you can tell i'm really good at android programming because i didn't notice a pretty obvious "oh this must be related to stuff they stripped out of the post" bit of code :v:

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

I haven't used it, but the docs for MapView say this:

quote:

Users of this class must forward all the life cycle methods from the Activity or Fragment containing this view to the corresponding ones in this class. In particular, you must forward on the following methods:

onCreate(Bundle)
onStart()
onResume()
onPause()
onStop()
onDestroy()
onSaveInstanceState()
onLowMemory()

so you might need to call through on all those events to let the MapView know when to tear itself down. If it's not getting destroyed, you'll have them hanging around - and you're passing them a reference to your fragment too, so it'll be keeping that in memory as well. Every time your activity is recreated (like by rotating it) you're creating a new fragment (calling its constructor in the fragment transaction) so you'll end up with those all piling up and anything those fragments have a reference to as well

PokeJoe
Aug 24, 2004

hail cgatan


Yeah you're passing
code:

this
to the mapview, which may be leaking that context. Try forwarding the life cycle methods to the view so it can clean itself up.

Froist
Jun 6, 2004

baka kaba posted:

I haven't used it, but the docs for MapView say this:

so you might need to call through on all those events to let the MapView know when to tear itself down.

This put me on the right track, thanks! I’d built out this app from the Map example in Android Studio, but along the way swapped out a SupportMapFragment (which handles all this for you) for this MapView without realising the extra obligations that brought. I’ve swapped it back as I realised there was no need to handle it myself, and the leak has gone. Thanks again!

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

No probs!

I know you said you're new, so this might be a bit advanced, but probably the best way to troubleshoot this kind of thing is to do a heap dump (probably best to hit the garbage collection button first)



It'll show you all the objects allocated in memory, you can sort it to see where all your memory is going, and if certain stuff is taking more than you'd expect. Probably the easiest thing to do is do the Sort By Package bit, find your app's package in the list (so you can just look at your own stuff), then sort by Allocation count and basically look at how many of your own objects are hanging around, and check if that looks right.

Like you'd probably have seen several instances of your MapFragment, when you'd only expect there to be one, right? That's a big clue, and you can click on the different instances to see what's holding a reference to them (what's keeping them alive in memory, basically) and you'd have probably seen MapViews, which also shouldn't still be around. And then you go hmm, what is the deal with these MapViews...

it's a good tool to learn at some point anyways

kitten smoothie
Dec 29, 2001

baka kaba posted:

it's a good tool to learn at some point anyways

100%. This tool will save you so much time. And once you get to the bottom of the leak it's usually always something that'll make you feel really stupid, like a listener causing a referential loop, or something like that.

Sereri
Sep 30, 2008

awwwrigami

https://twitter.com/chrisbanes/status/1126899316818186241

"How do you open the navigation drawer then?"
"Very carefully"

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Why do I get the feeling viewpagers are cancelled

Taffer
Oct 15, 2010


Feels like these gestures just get in the way of the functionality of half of all apps. Why does Google have to destroy all of the good parts of Android

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Ian Lake's in the replies on that tweet saying the gestures break part of the I/O showcase app :androidsmileythatsimnotlookingfor:

baka kaba fucked around with this message at 00:16 on May 11, 2019

brand engager
Mar 23, 2011

Been working on a bug at work where a ContentProvider is dying or not being started up or something. The app is split into multiple processes so it's probably some awful concurrency bullshit. I think we also have parts of the code that just get the database itself and then interact with it which might be a nono. Anyone got some dos+don'ts for working with these that arent in the official ContentProvider guide?

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

brand engager posted:

The app is split into multiple processes

Dare I ask why?

brand engager
Mar 23, 2011

I don't know why, it was before I worked there

Lutha Mahtin
Oct 10, 2010

Your brokebrain sin is absolved...go and shitpost no more!

brand engager posted:

Been working on a bug at work where a ContentProvider is dying or not being started up or something. The app is split into multiple processes so it's probably some awful concurrency bullshit. I think we also have parts of the code that just get the database itself and then interact with it which might be a nono. Anyone got some dos+don'ts for working with these that arent in the official ContentProvider guide?

What do you mean by "multiple processes"? A lot of commonly-used stuff in the standard Android API uses multithreading. The UI of your app (if it has one) is on a separate thread, AsyncTask calls use a background thread, etc. Is that what you're talking about?

brand engager
Mar 23, 2011

It runs in three processes. We've got one that has all the activities, one that has network stuff and native c/c++ junk, and a "utility" process that I cant remember what it's responsible for.

Vim Fuego
Jun 1, 2000

I LITERALLY SLEEP IN A RACING CAR. DO YOU?
p.s. ask me about my subscription mattress
Ultra Carp
So I'm making a little android app. It's my first project developing for a phone, and may I say: Good lord, android studio is godawful. It's full of tiny icons with no explanations, labyrinthine submenus, wow. Whatever.

I've made my app with the desired functionality using React-Native. Great. It runs fine in the emulator. So, I want to get it on my phone. That's not playing nice with the debugging over USB, but that may be my phone being a cheap piece of poo poo factory second. So I found a guide to exporting a .apk on stackoverflow: https://stackoverflow.com/questions/16622843/how-do-i-export-a-project-in-the-android-studio/16622975#16622975

That goes great. Everything is like the pictures, until the build fails with an internal error:

Error:Internal error:
code:
(java.lang.ClassNotFoundException) com.google.wireless.android.sdk.stats.IntellijIndexingStats$Index
$Index
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
etc.

This is irritating because this is a fresh install of all the latest stuff recommended by the react-native team at facebook- https://facebook.github.io/react-native/docs/getting-started
Python2, the Android SDK, Android Studio, and the Java development kit.

It would be wonderful if a fresh install of the recommended tools didn't fail by default. This looks like a dependency is missing. It's not one I'm intentionally using... But ok. Googling this gets me jack-poo poo for useful information.
https://github.com/spotbugs/spotbugs/issues/931
https://stackoverflow.com/questions/55844077/findbugs-idea-classnotfoundexception-com-google-wireless-android-sdk-stats-int

From what I understand, there was an old utility build into android studio called "findbugs." Findbugs is deprecated, and also causes this error to pop up? The new version is called "spotbugs". I am not aware that I am using either of these, but again, I haven't touched any settings in android studio. People say you can look at your plugins in Android Studio through File -> Settings -> Plugins. I have done that. It doesn't say I'm using either spotbugs or findbugs or even that either is installed.

Even more baffling, this is the checked answer on Stackoverflow:

quote:

I think this may be the answer.

SpotBugs is the spiritual successor of FindBugs, carrying on from the point where it left off with support of its community.

The FindBugs Plugin

Since FindBugs is unmaintained and does not support bytecode compiled for Java 9 and above, the FindBugs plugin has been deprecated and is scheduled to be removed in Gradle 6.0. Please consider using the SpotBugs plugin instead.

You should have at least 512 MB of memory to use SpotBugs. To analyze very large projects, more memory may be needed.

Very important issue Unable to use Spotbugs in Android application.

I find that I cannot divine any useful information from that one.

I gather that I may need to edit a gradle build file somewhere, but although I have spotted one or two candidates in my react-native project I am still in the dark as to
A: which gradle file is the one to edit
B: Really, what gradle is- I get that it's google's build tool, but not much more than that.

If anyone has any insight into how I can get my app build/turn off/ignore this error, that would be wonderful.

Taffer
Oct 15, 2010


I can't directly help you with your problem, but I can tell you that you're inviting pain by using React Native. It's a poorly made multiplatorm solution that doesn't integrate well at all with the tools. Pretty much universally it turns out that people who use React Native end up taking more time to develop their app than if they had just made separate native apps. I realize if you're doing this for work then it's probably out of your hands, but I recommend doing whatever you can to change that decision.

That is probably also your gripe with Android Studio. Things like RN aren't made to work with it and just add a big layer of pain. It's based on Intellij which is the best full IDE out there (distinct from heavy featured editors like VSCode), bar none. Compared to Xcode, which is the closest analog you can get, the comparison is laughable.

Of all the things to complain about with Android development, and there are a lot, the IDE isn't one of them. It's really good.

Vim Fuego
Jun 1, 2000

I LITERALLY SLEEP IN A RACING CAR. DO YOU?
p.s. ask me about my subscription mattress
Ultra Carp
Interesting! Yeah, I was getting the sense from poking around that React Native was kinda dead/undersupported. I just built this one for fun and to learn some mobile work because I'm planning on doing some mobile stuff for work at some point.

I stand by my initial complaints about Android Studio- for a first time user, unlabeled 15x15 pixel icons are pretty hostile. I'm sure it's fine once you're used to it, but it's a kick in the teeth compared to visual studio code.

FAT32 SHAMER
Aug 16, 2012



Vim Fuego posted:

Interesting! Yeah, I was getting the sense from poking around that React Native was kinda dead/undersupported. I just built this one for fun and to learn some mobile work because I'm planning on doing some mobile stuff for work at some point.

I stand by my initial complaints about Android Studio- for a first time user, unlabeled 15x15 pixel icons are pretty hostile. I'm sure it's fine once you're used to it, but it's a kick in the teeth compared to visual studio code.

if you hover over the icons itll tell you what they do, and if that doesnt explain it then you can google the functionality to learn how to use it.


also dont use flutter either.

Taffer
Oct 15, 2010


Vim Fuego posted:

Interesting! Yeah, I was getting the sense from poking around that React Native was kinda dead/undersupported. I just built this one for fun and to learn some mobile work because I'm planning on doing some mobile stuff for work at some point.

I stand by my initial complaints about Android Studio- for a first time user, unlabeled 15x15 pixel icons are pretty hostile. I'm sure it's fine once you're used to it, but it's a kick in the teeth compared to visual studio code.

Definitely understandable, but as a professional tool its primary design motivation is not intuitive adoption by newcomers, but efficient use by experienced users. The same will be true of any professional tool that people are meant to work in for 8+ hours a day. I come from a background of of 3D work, and let me tell you, those tools are an absolute nightmare as a newcomer. But after a few weeks (or months, or years) you come to understand the decisions that were made to make the tools the way they are, and appreciate them. But there will definitely be some pain. Read tutorials, guides, watch videos, learn hotkeys, and you'll start to get comfortable.

Regarding gradle, it's a broad build tool, not exclusively used by Android but that's certainly it's biggest area. I can tell you personally it was a gigantic pain in my rear end when I first started (I was not a very experienced developer when I started doing Android dev), but over time I've not only come to be comfortable with it, but vastly prefer it over other build tools I've had to use. NPM, cocoapods, CMAKE, etc all have their strengths, but they also all have very big downsides. Gradle has downsides too for sure, but IMO, they are far more manageable than the downsides of other build tools. Its integration with Android Studio also makes it much more pleasant to use. With time, of course.

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Yeah Android Studio has a lot of stuff going on in it, it's very dense so you kinda need everything to be compact otherwise it would be unusable without multiple monitors. There's definitely a learning curve, so don't expect to know what everything is (and you won't always need to know either)

biggest tip - Ctrl+Shift+A and start typing (it's like F1 in VS Code only more powerful), or tap Shift twice for navigating to stuff

also if you're having build issues, it's always worth doing Build > Clean and trying again, usually this is an issue if you jump around in source control and there are significant changes to dependencies or generated code. Could always try File > Invalidate Caches and Restart as a hail mary. Probably won't help in this case, there's a ticket about this kind of thing - the people affected seem to be saying it appeared in 3.4, so maybe try an earlier version like 3.3? Honestly though it's probably worth trying to get USB Debugging working on your phone if you want to develop on it

Also I dunno if it matters, but you mentioned installing the JDK, but the AS team recommend using the OpenJDK that comes with Android Studio, so it might be worth a try

Vim Fuego
Jun 1, 2000

I LITERALLY SLEEP IN A RACING CAR. DO YOU?
p.s. ask me about my subscription mattress
Ultra Carp
Thanks! I'll see how it goes. From poking around it seems like Java, Kotlin, or C# are the way to go. Any clear leaders there or is it personal preference/company requirements?

PokeJoe
Aug 24, 2004

hail cgatan


Write kotlin if it's a new app imo

ringu0
Feb 24, 2013


Just fyi, something they posted last week on Android Developers Blog:

quote:

Android development will become increasingly Kotlin-first

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

brand engager posted:

I don't know why, it was before I worked there

The codebase at my previous job had two processes; the ui process and player process. The idea was that if the UI crashed for a dumb reason playback would continue in the background. This wasn't a great reason but was somewhat understandable.

There's almost no reason to have multiple processes. You should ask about this.

The database is going to assume single process, and the SQLite classes may help enforce concurrency issues. If multiple processes are using the DB, you're in for a bad time.

FAT32 SHAMER
Aug 16, 2012



PokeJoe posted:

Write kotlin if it's a new app imo

we're slowly converting everything to Kotlin because we suspect Oracle is going to start charging Google for licensing or w/e

i wonder when Kotlin Native will be a [bigger] thing

PokeJoe
Aug 24, 2004

hail cgatan


same and the kotlin classes are shorter and easier to debug

Taffer
Oct 15, 2010


Kotlin owns, definitely write it. But you'll still need to know Java cause a ton of libraries and AOSP are Java.

Volguus
Mar 3, 2009
On the other hand, let's look at what help can we get when we're stuck:

https://stackoverflow.com/questions/tagged/kotlin - 23,106 questions
https://stackoverflow.com/questions/tagged/java - 1,550,652 questions

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Now you have 1,550,652 problems!!

FAT32 SHAMER
Aug 16, 2012



Volguus posted:

On the other hand, let's look at what help can we get when we're stuck:

https://stackoverflow.com/questions/tagged/kotlin - 23,106 questions
https://stackoverflow.com/questions/tagged/java - 1,550,652 questions

90% of java questions on stack overflow are because some cute :shobon: newbie :shobon: didnt initialise their variable

Volguus
Mar 3, 2009

FAT32 SHAMER posted:

90% of java questions on stack overflow are because some cute :shobon: newbie :shobon: didnt initialise their variable

That could be true (quality/quantity bla bla) . so lets take a look at Kotlin. First question: "Kotlin: How to iterate through a collection from a specific position with indexes (skip N elements with indexes) ". Yeah.
Q.e.d.

On the other hand, if one is learning the language, what are the odds they will have some "cute :shobon: newbie :shobon: didnt initialise their variable" problem too. For which the answer will be at a google search away.

Volguus fucked around with this message at 19:08 on May 21, 2019

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Volguus posted:

That could be true (quality/quantity bla bla) . so lets take a look at Kotlin. First question: "Kotlin: How to iterate through a collection from a specific position with indexes (skip N elements with indexes) ". Yeah.
Q.e.d.

What about it though? Seems like a pretty normal question, especially if you're just getting used to the functional paradigm instead of manually iterating over things

Adbot
ADBOT LOVES YOU

Volguus
Mar 3, 2009

baka kaba posted:

What about it though? Seems like a pretty normal question, especially if you're just getting used to the functional paradigm instead of manually iterating over things

What does a loop have to do with "functional paradigm"? And "what about it?" It just looks a simple enough question, on par with the "initialized variable" questions java gets.
Trying to come and say: but the kotlin questions are so much better, doesn't really fly. Remember, most are the same developers asking both.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply