Saving custom Library globally



I'm strugling with Custom Library - global at least.

I've managed to create and use project level Library by:

val table = LibraryTablesRegistrar.getInstance().getLibraryTable(project)

val newLib = table.createLibrary(name)
val libModel = newLib.modifiableModel
libModel.addRoot(virtualFile!!.url, type)

val module = ModuleManager.getInstance(project).modules.first()
val rootModel = ModuleRootManager.getInstance(module).modifiableModel

This works and creates and index that Library, problem is that I'm trying to improving into something like GoLand offers for it's SDKs:

I wanted to try an approach to creating Global Libraries which will then be visible in that select box for all projects. After selecting any of them I'd then call rootModel.addLibraryEntry(lib) to link it into a project.

Problem is, that I'm not able to save/list those Libraries. I'm trying very similar approach, but instead of .getLibraryTable(project), getting what might be a Global one?

val modifier = LibraryTablesRegistrar.getInstance().libraryTable.modifiableModel
val library = modifier.createLibrary(name, GdLibraryKind.getOrCreate())
val libModel = library.modifiableModel
libModel.addRoot(path, type)

Problem is that even though this runs sucessfully, later on getting Library in any of those ways:


all returns empty array.

Does anyone know what might be wrong? I'm kind of lost in what are those different tables and modifiableModels and where to register/retrieve Libraries so I can access them across all projects, not just the current one.


Oh, I've managed to overlook that I'm commiting Library instead of LibraryTable.

Another case where taking a break from code solves it.
Thank you for pointing me to tests... saw it right away.


One additional question please:
I'm having hard time persisting LibraryState.

Library is now created and saved ok with:

val library = modifier.createLibrary(name, GdLibraryKind)
val libModifier = library.modifiableModel as ModifiableModelEx
val state = as GdLibrary
state.version = "xyz"

libModifier.addRoot(path, type)

Properties class

@State(name = "GdLibraryProperties", storages = [Storage("GdLibraryProperties.xml")])
class GdLibraryProperties : LibraryProperties<GdLibrary>() {
var config: GdLibrary = GdLibrary()

  override fun equals(other: Any?): Boolean { ... }

override fun hashCode(): Int {
      return config.hashCode()

override fun getState(): GdLibrary {
return config

override fun loadState(state: GdLibrary) {
config = state

And the state itself:

class GdLibrary {
var path: String = ""

var version: String = ""

But the state is not saved and after getting Library back it's always empty

.forEach { }

Just a little update, I've been looking into community idea and noticed, that Kotlin's LibraryPropeties used itself in generic type so I've tried to update it like:

@State(name = "GdLibraryProperties", storages = [Storage("GdLibraryProperties.xml")])
class GdLibraryProperties : LibraryProperties<GdLibraryProperties>() {

var path: String = ""

var version: String = ""

override fun equals(other: Any?): Boolean {
if (other !is GdLibraryProperties) return false
return path == other.path
&& version == other.version

override fun hashCode(): Int {
return this.hashCode()

override fun getState(): GdLibraryProperties {
return this

override fun loadState(state: GdLibraryProperties) {
path = state.path
version = state.version

But result is still the same and State is not persisted for some reason.


I've found it, properties are not getted mutable and copied I suppose, so after altering them I have to call

(library.modifiableModel as ModifiableModelEx).properties = props

Please sign in to leave a comment.