2016-12-14 34 views
0

我正在努力去做不可能的事情,而且它似乎沒有工作。總體目標是這樣的:我有一個組件接口,我想向用戶顯示實現該接口的類路徑上的類的列表。訣竅是,它必須在Android中運行。如何構建一個存儲接口所有實現名稱的sbt插件?

附近,我可以告訴這是不可能在運行時做。 Java機制(ServiceLoader)已被Android工具鏈故意癱瘓,所以它不起作用。番石榴不適用於Android,ClassUtils也不適用Reflections

在這一點上,我已經犛牛颳了8個小時的海峽,並沒有盡頭,所以我正在尋找替代方法。我目前的想法是構建一個插件(非常類似於sbt-spi,但不是,因爲Android討厭SPI),它可以在編譯時生成一個文本文件,列出實現該接口的每個類,以便在運行時我可以打開該文件爲一個資源,然後使用反射來開始構建它們。這是一個合理的想法?我應該怎麼做呢? (我目前的做法是「閱讀sbt-spi插件源並嘗試複製它」,但這似乎是一個「請求智慧」是更好的方法)

回答

0

知道了!最後我用SBT-SPI畢竟(好哇,不要重複任何車輪!),只是移動輸出到中間的資產目錄作爲resourceGenerators任務的一部分:

lazy val androidEntryPoint = (project in file("android-entry-point")) 
    .dependsOn(core, components, androidComponents) 
    .enablePlugins(SpiPlugin) 
    .settings(commonSettings: _*) 
    .settings(resourceGenerators in Compile += Def.task{ 
    // This task copies the list of Components to the appropriate place to ensure 
    // it gets included in an accessible place in the APK 
    val res = collectResources.value._1 // item _1 here is for assets, item _2 is for resources. See the output of sbt "show androidEntryPoint/android:collectResources" 
    mapExport.value.toSeq.map { name => 
     IO.move(target.value/name, res/name) 
     res/name 
    } 
    }.taskValue 
) 

這麼說,我很樂意聽到如果你能想到一個更好的方法。如果在下個星期內沒有出現,我會標記這個答案。