Convert from Kodular to Android Studio - TinyDB Implementation?

Hi team,

I’m (slowly) working on moving an app over from Kodular to Android Studio.
(I am still also using Kodular for many apps, but I want to learn Kotlin/Android Studio)

Can anyone suggest what the best way to import TinyDB items would be?

If I add TinyDB to Android Studio via:

https://showmeyourcode.org/create-super-easy-databases-android-apps-tinydb/

will users that update the app still have the TinyDB from Kodular, and can I access these as normal?

I don’t want to lose user data when updating, so I am trying to preserve it by implementing TinyDB into the Android Studio version. I realised it will be hard to test this - as I would have to install the Kodular app, save to TinyDB, update the app to Android Studio version and check the data, so I thought I’d ask here first.

If anyone has tips, please let me know.

Thanks!

Follow up question:

Assuming it is possible, what is the TinyDB variable called in Kodular?

I’m guessing it is called whatever you call the TinyDB component in Kodular.

If I get things working, I’ll update this thread so others trying to do the same conversion can learn.

Before you launch your Android Studio version of your app, make an update on the Kodular version with an EXPORT procedure, which at exiting the app, will create a file that you can read and re create the data base once the new version is running.

Yeah, I thought about this too and this will probably be my process if I can’t access the database from Android Studio. I want to still use TinyDB in Android Studio though as it seems like an easy way to store data.

This approach would only be an issue if people updated the app before the app makes the file (i.e. if it’s automatically updated before they open the app again)

I might be wrong but I think TinyDB is just an xml file. Is that so? or there’s an actual tinydb in Android like sqlite?

The one I linked to is a Java file that you add to your project.

Not too sure how to implement it yet - I’m working on moving over other areas of my app in Android Studio first and then I’ll try to come up with a solution.

I did find a similar thread where someone was trying to convert from App Inventor to Android Studio.
https://groups.google.com/forum/#!topic/app-inventor-open-source-dev/O1gR1MVOGNY

1 Like

Still struggling to find a solution for this that doesn’t require the user to open the app before updating :thinking:

Anyone have an idea how to access TinyDB from Android Studio?

I just hate the idea of users having automatic updates enabled and losing their saved data from the app

TinyDB stores the values to this directory:

/data/user/0/<package_name>/shared_prefs

After entering the /shared_prefs directory, you will see a .XML file, so you can read that file in Android Studio.

But;

  • This is a system directory. So you won’t able to see this directory yourself from file explorer without a root access.

  • However, apps can access own directories without any permission in the app itself. So if your package name is com.example then the app can write to /data/user/0/com.example/shared_prefs

  • So your app’s package name needs to be the same both in Android Studio and Kodular, so you will get access to the TinyDB and its contents.

I think this might be just what I need, thank you! The app will have the same package name, as it’s an update.

Do you know if the shares prefs file that Kodular uses has a name?
(So in Android Studio I would use getSharedPreferences() )

Or does it use the default, i.e only one shared prefs file?
(So in Android Studio I would use getPreferences() )

If it uses the default, then I should just be able to update my app with the Android Studio version and access the tinyDB values with getPreferences() and the tinyDB keys I used before.

That would be ideal! I’ll test this out when I get some time and if I get it working I’ll update here, in case someone later has the same issue.

1 Like

I just checked and the file name is TinyDB1.xml

And its content looks like this:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
     <string name="tag">&quot;value&quot;</string>
</map>

I imagine the file name of the XML file/shared preferences would correspond to the name of your TinyDB component. Mine was TinyDB1.

I’ll add some code here (Kotlin for Android Studio) in case anyone is struggling with the same issue in the future.

In MainActivity:

public fun LoadData(Key: String): String {
    val tinyDBComponentName = "TinyDB1" 
    val sharedPreferences = getSharedPreferences(tinyDBComponentName, Context.MODE_PRIVATE)
    var Data = sharedPreferences.getString(Key, null)
    return Data.toString()}

Then you can call the above function from any fragment to return the value like this:

var tinyDBKey = "YourTinyDBKeyHere"
var value = (activity as MainActivity).LoadData(tinyDBKey)
Toast.makeText(requireContext(), value, Toast.LENGTH_LONG).show()

One issue I faced with updating from my Kodular version to Android Studio verison is getting “App not installed” when trying to update it and I had to do the following:

  • Make sure version number in manifest is higher
  • Make sure package name and app name is the same
  • Make sure it is signed with the same keystore (Build - Generate signed bundle/APK)
  • Clean project and rebuild.

This will let you check the TinyDB data saved via a Kodular verison of your app from an Android Studio version and ensure no data is lost in the update. You can then integrate this data into the new version however you want to.