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

添加android.jar作为android Studio中模块的依赖项,android,android-studio,module,Android,Android Studio,Module,我决定问这个问题,因为我的研究没有为我的场景提供任何有用的解决方案,我相信这些方案应该很受欢迎 我的问题与此略有关联,但解决方案在我的场景中没有帮助 我想创建一个只包含我的应用程序逻辑的Java模块。也就是说,我不希望它成为Android库模块。对于这样的库,我不需要清单文件和其他文件。我的应用程序逻辑Java模块中不会有任何Android活动。所有特定于Android的代码都将位于Android模块(称为应用程序)中。我的Java模块称为applogic 所以从技术上讲,我希望我的项目看起来像

我决定问这个问题,因为我的研究没有为我的场景提供任何有用的解决方案,我相信这些方案应该很受欢迎

我的问题与此略有关联,但解决方案在我的场景中没有帮助


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

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

AppProjectName
|-->app //Only Android-specific code here
   |
   -->src
|-->applogic //General, reusable Java-code here
   |
   -->src
AppProjectName
|-->app
   |
   -->src
|-->applogic
   |
   -->src
   -->libs
     |
     -->android.jar
在应用程序模块设置中,我将applogic添加为模块依赖项。 从我的Android应用程序中,我创建在applogic类中定义的对象。我很容易做到

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

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


谢谢

我的一个项目有相同的设置。我正在Java库模块中使用
android.util.Log
android.os.Build

在您的应用程序模块中,您应该将applogic作为模块依赖项添加到
build.gradle
,您已经这样做了

然后,在applogic模块的
build.gradle
中,您应该添加
android.jar
依赖项,如下所示:

apply plugin: 'java'

dependencies {
    compile files('libs/android.jar')
}
## 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 由于您使用的是API 1中提供的
android.util.Log
,因此您可以从任何SDK平台使用任何“android.jar”。只需将其复制到您的applogic module
libs
文件夹。Android Studio为Java库模块创建
libs
文件夹

所以,你的最终项目应该是

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

我的一个项目有相同的设置。我正在Java库模块中使用
android.util.Log
android.os.Build

在您的应用程序模块中,您应该将applogic作为模块依赖项添加到
build.gradle
,您已经这样做了

然后,在applogic模块的
build.gradle
中,您应该添加
android.jar
依赖项,如下所示:

apply plugin: 'java'

dependencies {
    compile files('libs/android.jar')
}
## 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 由于您使用的是API 1中提供的
android.util.Log
,因此您可以从任何SDK平台使用任何“android.jar”。只需将其复制到您的applogic module
libs
文件夹。Android Studio为Java库模块创建
libs
文件夹

所以,你的最终项目应该是

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

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

apply plugin: 'java'

dependencies {
    compile files('libs/android.jar')
}
## 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 Studio自动生成。 #不要修改此文件--您所做的更改将被删除! # #此文件必须*不*签入版本控制系统, #因为它包含特定于本地配置的信息。 # #SDK的位置。这只供Gradle使用。 #有关使用版本控制系统时的自定义,请阅读 #标题注释。 #2017年4月4日星期二12:34:41英国夏令时 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") } 依赖关系{ Properties localProps=新属性() localProps.load(project.rootProject.file('local.properties').newDataInputStream()) 实现文件(${localProps.getProperty('sdk.dir')}/platforms/android-15/android.jar) }
当然,您必须确保使用Android SDK管理器下载了相应版本的平台,但如果您是Android开发人员,您可能已经下载了相应版本的平台。

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

apply plugin: 'java'

dependencies {
    compile files('libs/android.jar')
}
## 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 Studio自动生成。 #不要修改此文件--您所做的更改将被删除! # #此文件必须*不*签入版本控制系统, #因为它包含特定于本地配置的信息。 # #SDK的位置。这只供Gradle使用。 #有关使用版本控制系统时的自定义,请阅读 #标题注释。 #2017年4月4日星期二12:34:41英国夏令时 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") } 依赖关系{ Properties localProps=新属性() localProps.load(project.rootProject.file('local.properties').newDataInputStream()) 实现文件(${localProps.getProperty('sdk.dir')}/platforms/android-15/android.jar) }
当然,您必须确保使用Android SDK管理器下载了相应版本的平台,但如果您是Android开发人员,您可能已经下载了相应版本的平台。

“我不希望它成为Android库模块”——为什么?“对于这样一个库,我不需要清单文件和其他文件”——那么?“在我的应用程序逻辑Java模块中将不会有任何Android活动”——那么?如果您刚刚使用了一个Android库模块,并支付了一个几乎为空的清单文件的价格,那么您现在应该已经完成了。@commonware Good points,bu