android - 在 Android Studio 中添加 android.jar 作为模块的依赖项

标签 android android-studio module

在我的研究没有为我的场景揭示任何有用的解决方案之后,我决定提出这个问题,我认为应该非常受欢迎。

我的问题与此 SO question 略有相关,但该解决方案对我的情况没有帮助。

我想创建一个只包含我的应用程序逻辑的 Java 模块。也就是说,我不希望它成为一个 Android 库模块。对于这样的库,我不需要 list 文件和其他文件。我的应用程序逻辑 Java 模块中不会有任何 Android Activity 。所有特定于 Android 的代码都将位于 Android 模块(称为 app)中。我的 Java 模块称为 applogic

从技术上讲,我希望我的项目看起来像

AppProjectName
|-->app //Only Android-specific code here
   |
   -->src
|-->applogic //General, reusable Java-code here
   |
   -->src

app 模块设置中,我将 applogic 添加为模块依赖项。 在我的 Android app 中,我创建了在 applogic 类中定义的对象。我能够轻松做到这一点。

事情是这样的。我想使用 Android 的日志记录功能(在 android.util.Log 中)。如何将它用于我的 Java 库?

上述 SO 问题中提到的一个可能的解决方案是将 android.jar 作为库添加到我的 applogic 模块中。我无法在 Android Studio 中做到这一点。任何指导都会非常有帮助。

谢谢!

最佳答案

如果您在 Android Studio 创建的项目上下文中运行,您将拥有一个提供 SDK 位置的 local.properties 文件。即使您没有这样一个自动生成的文件,也没有什么可以阻止您创建它。它看起来像这样:

## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Tue Apr 04 12:34:41 BST 2017
ndk.dir=C\:\\Users\\smith\\Programs\\android-sdk\\ndk-bundle
sdk.dir=C\:\\Users\\smith\\Programs\\android-sdk

无论哪种方式,您都可以按照我认为与 Android Gradle 插件相同的方式使用它:

dependencies {
    Properties localProps = new Properties()
    localProps.load(project.rootProject.file('local.properties').newDataInputStream())
    implementation files("${localProps.getProperty('sdk.dir')}/platforms/android-15/android.jar")
}

当然,您必须确保已使用 Android SDK 管理器下载了适当版本的平台,但如果您是 Android 开发人员,您可能已经这样做了。

关于android - 在 Android Studio 中添加 android.jar 作为模块的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32168220/

相关文章:

android - 是否有可能从 fragment 中获取 `RecyclerView`?

java - 如何解决 android studio 中的错误 'Package R doesn' t exit' 和 'Execution failed for task":app.compileDebugJavaWithJavac"' 错误?

android - 我的 Android Studio 上缺少调试窗口

module - 如何跨模块文件使用宏?

generics - 无法定义 'a->' b 函数列表

android - 从 Android 读取计算机的 RSA key 指纹

android - € 登录方法名称使用 Gradle 测试用例失败

android - onPause 停止 LocationManager

java - 错误 :Error converting bytecode to dex Android Studio 3. 0

c - 简单中断处理程序 : request_irq returns error code -22