问题描述
在我的研究未发现适用于我的情况的任何有用解决方案(我认为应该很受欢迎)之后,我决定问这个问题.
I have decided to ask the question after my research did not reveal any useful solutions for my scenario which I believe should be quite popular.
我的问题与此,但该解决方案对我的情况没有帮助.
My question is slightly related to this SO question, but the solution did not help in my scenario.
我想创建一个仅包含我的应用程序逻辑的Java模块.就是说,我不希望它成为Android Library模块.对于这样的库,我不需要清单文件和其他文件.我的应用程序逻辑Java模块中没有任何Android活动.所有特定于Android的代码都将位于Android模块(称为 app )中.我的Java模块称为 applogic .
I want to create a Java module that will contain only my application logic. That said, I do not want it to be an Android Library module. I do not need to have manifest files and others for such a library. There will not be any Android activities in my application logic Java module. All Android-specific code will be in the Android module (called app). My Java module is called applogic.
从技术上讲,我希望我的项目看起来像
So technically, I want my project to look like
AppProjectName
|-->app //Only Android-specific code here
|
-->src
|-->applogic //General, reusable Java-code here
|
-->src
在 app 模块设置中,我添加 applogic 作为模块依赖项.在我的Android app 中,我创建了在 applogic 中的类中定义的对象.我能够轻松做到这一点.
In the app module settings, I add applogic as a module dependency.From my Android app, I create objects defined in classes in applogic. I am able to do that easily.
这是东西.我想使用Android的日志记录功能(在android.util.Log
中).如何在我的Java库中使用它?
And here is the thing. I want to use Android's logging capabilities (in android.util.Log
). How can I use that for my Java library?
在上面提到的SO问题中提出的一种可能的解决方案是将android.jar
作为库添加到我的applogic
模块中.我无法在Android Studio中做到这一点.任何指导都会有很大帮助.
A possible solution that was stated in the SO question mentioned above was to add android.jar
as a library to my applogic
module. I was not able to do that in Android Studio. Any guidance will be greatly helpful.
谢谢!
推荐答案
如果您是在Android Studio创建的项目的上下文中运行,则将有一个local.properties
文件,其中提供了SDK位置.即使您没有这样的自动生成的文件,也没有什么可以阻止您创建它的.看起来像这样:
If you're running within the context of a project created by Android Studio, you'll have a local.properties
file giving the SDK location. Even if you're haven't got such an auto-generated file, there's nothing stopping you from creating it. It looks something like this:
## 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插件相同的方式使用它:
Either way, you can then use it in what I assume is the same manner as the Android Gradle plugin does:
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 Manager下载了适当版本的平台,但是如果您是Android开发人员,则可能已经做到了.
Of course, you'll have to make sure you've downloaded the appropriate version of the platform using the Android SDK Manager, but if you're an Android developer you've probably already done that.
这篇关于将android.jar添加为Android Studio中模块的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!