java - 从 Android Studio 中的模块访问主项目?

标签 java android kotlin

我正在使用 this library我已将其作为模块安装在本地。我可以通过我的主项目访问它,但我不能做相反的事情。例如,从这个库访问我的主项目中的一个变量......
我尝试在库的 build.gradle 中添加这一行:

    implementation project(':app')
但我得到了这个奇怪的错误:
Circular dependency between the following tasks:
:placepicker:generateDebugRFile
\--- :placepicker:generateDebugRFile (*)
    
(*) - details omitted (listed previously)
我怎样才能解决这个问题?我的项目在 Java 中,我的库在 Kotlin 中

最佳答案

“循环依赖”只能通过删除在两侧之一上导致此问题的依赖性来修复。
如果您需要从库代码中访问一些数据,您可以在库中实现一个接口(interface),该接口(interface)将由项目中的某个类扩展。然后你就可以在你的库中使用扩展类和接口(interface)中定义的访问方法.
例子
假设您需要在库中获取对应用程序上下文的引用。您应该创建一个接口(interface):

interface ContextAccessor {
    // Marking it as optional just in case
    // you will not be able to get a context
    // from an object that implemented ContextAccessor
    fun getApplicationContext(): Application?
}
因为您在项目中添加了库作为依赖项,所以您可以访问 ContextAccessor .用这个接口(interface)扩展一些类并实现getApplicationContext方法。假设您想扩展一些 Activity .
class MyActivity: Activity, ContextAccessor {
    ... other code here

    override fun getApplicationContext(): Application? = application
}
现在从您的MyActivity类,可以设置ContextAccessor进入你的图书馆,就像它是 dependency injection .
class MyActivity: Activity, ContextAccessor {
    ... other code here 
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val someLibraryClassInstance = SomeLibraryClass()
        someLibraryClassInstance.setContextAccessor(this)
        // OR -> `someLibraryClassInstance.contextAccessor = this`
    }
}
警告 :当您保存对任何 Android 组件的引用时,尤其是 Activity、Fragment、Dialog 等,请确保稍后在对象将要被销毁时删除此引用以避免内存泄漏。
如何从前面的代码 fragment 中删除对一些修改过的代码的引用的示例:
class MyActivity: Activity, ContextAccessor {
    ... other code here 

    private val someLibraryClassInstance = SomeLibraryClass()   
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
         
        // ContextAccessor reference is set to some library class
        someLibraryClassInstance.setContextAccessor(this)
    }

    override fun onDestroy() {
        super.onDestroy()

        // Super important!
        someLibraryClassInstance.setContextAccessor(null)
        // OR create some method like `someLibraryClassInstance.removeContextAccessor(this)`
    }
}
Java中的相同类
interface ContextAccessor {
    // Marking it as optional just in case
    // you will not be able to get a context
    // from an object that implemented ContextAccessor
    Application getApplicationContext();
}
public class MyActivity extends Activity implements  MyActivity.ContextAccessor {
    
    private SomeLibraryClass someLibraryClassInstance = SomeLibraryClass();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ContextAccessor reference is set to some library class
        someLibraryClassInstance.setContextAccessor(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Super important!
        someLibraryClassInstance.setContextAccessor(null);
        // OR create some method like `someLibraryClassInstance.removeContextAccessor(this)`
    }

    @Override
    public Application getApplicationContext() {
        return super.getApplication();
    }
}
更新(2020 年 8 月 10 日):如何使用 ContextAccessor?
以下是如何使用 ContextAccessor在你的图书馆:
class SomeLibraryClass {
    private var mContextAccessor: ContextAccessor?

    fun setContextAccessor(contextAccessor: ContextAccessor?) {
        mContextAccessor = contextAccessor
    }
    
    fun someOtherMethod() {
        mContextAccessor?.getAppContext()?.let { nonNullContext ->
            // use nonNullContext here
        }
    }
}

关于java - 从 Android Studio 中的模块访问主项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63204657/

相关文章:

Android:检测何时安装了应用程序

java - Kotlin kotlinClass.class.getName() 不能返回包名,只能返回简单的类名

Java:.contains 和 .equals

java - 用于将字符序列转换为字符串的流的替代方案

android - 如何让 ACRA 在 Android 上与 Phonegap 一起工作?

android - 在 Delphi 10.3.3 上以 Android API 29 为目标

Java - 使用循环依赖关系映射 DTO

java - 返回减去特定字符之间的特定字符的字符串

scala - Kotlin 序列会缓存中间结果吗?

java - Swing - 等待渲染完成