我如何才能先构建Java模块,然后再构建其余的android应用程序? [英] How I can build java module first then the rest of my android application?
问题描述
在我的Java项目中,我有以下文件夹:
In my java project I have the following folders:
- 包含Android特定应用程序的
-
app
-
settings_fetcher
是一个Java模块
app
that contains the android specific applicationsettings_fetcher
that is a java module
我要做的是首先构建Java模块,创建一个 .jar
文件,然后将 .jar
文件移动到 ./app/libs
,然后构建其余的应用程序.
What I want to do is to build first the java modult create a .jar
file then movwe the .jar
file into ./app/libs
and then build the rest of the application.
app
文件夹包含以下 build.gradle
:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.3"
}
}
plugins {
id 'com.android.application'
}
def versionCodeDate() {
return new Date().format("yyyyMMdd").toInteger()
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "pcmagas.vodafone_fu_h300s"
minSdkVersion 28
targetSdkVersion 30
versionCode versionCodeDate()
versionName "v"+versionCodeDate()
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
println 'In release configs'
def keystoreFile = rootProject.file(".keys/h300s.keystore")
def env = System.getenv();
if(env.containsKey('KEYSTORE_FILE')){
keystoreFile = rootProject.file(env.get('KEYSTORE_FILE'))
}
if (keystoreFile.exists() ) {
println("Configuring Signing options for release")
android.signingConfigs["release"].storeFile = keystoreFile
android.signingConfigs["release"].storePassword = System.getenv('MYAPP_RELEASE_STORE_PASSWORD')
android.signingConfigs["release"].keyAlias = System.getenv('KEYALIAS')
android.signingConfigs["release"].keyPassword = System.getenv('MYAPP_RELEASE_KEY_PASSWORD')
}
}
}
buildTypes {
release {
minifyEnabled true
signingConfig = signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
testOptions {
unitTests.returnDefaultValues = true
}
}
repositories {
google()
jcenter()
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
implementation 'androidx.activity:activity-ktx:1.2.0'
implementation 'androidx.fragment:fragment:1.3.0'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'org.json:json:20210307'
testImplementation 'com.github.gmazzo:okhttp-mock:1.4.1'
testImplementation 'junit:junit:4.+'
testImplementation 'org.mockito:mockito-core:3.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
settings_fetcher
包含以下 build_gradle
:
plugins {
id 'java-library'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
task makeJar(type: Copy) {
from('build/libs/')
into('../app/libs/')
include('settings_fetcher.jar')
}
关于如何解决这个问题的任何想法?
Any ideas on how I can approach this?
换句话说,我想做的是当我运行以下命令之一时:
In other words, what I want to do is when I run one of these commands:
-
gradlew assemble ^ flavor ^
-
gradlew构建
-
gradlew build ^ flavor ^
其中 ^ flavor ^
是我的应用程序的风格,可以是 Debug
或 Release
.
Where ^flavor^
is my application's flavor and can be either Debug
or Release
.
要先构建 .jar
文件,然后再构建我的应用的apk.
To be able to build the .jar
file first and then my app's apk.
推荐答案
在您的情况下,假设 settings_fetcher
创建为 java-library
,然后您可以执行以下操作:
In your case the settings_fetcher
is a dependency to your app
assuming that the settings_fetcher
is created as java-library
then you can do the following:
在 app
文件夹中的 build.gradle
中,将 settings_fetcher
项目放置为依赖项:
In the build.gradle
located in app
folder place the settings_fetcher
project as dependency:
// rest of build.gradle goes here
dependencies{
// Your app's dependencies goes here
implementation project(":settings_fetcher")
}
因此,一旦运行 ./gradlew build
,库的jar文件将位于 ./settings_fetcher/build/libs
文件夹中.因此,也无需执行 makeJar
任务.
So once you run ./gradlew build
the jar file for your library will be located upon ./settings_fetcher/build/libs
folder. Therefore, there's no need for makeJar
task as well.
此外,任何库依赖项都应放置在其各自的 build.gradle
中,在您的情况下,该库依赖项应位于 dependencies
settings_fetcher 文件夹中.>部分.
Also, any library dependencies should be placed in its own respective build.gradle
in your case the one located in settings_fetcher
folder in a dependencies
section.
例如,如果您的图书馆需要 okHttp
客户端,则使用:
For example if your library needs the okHttp
client then use:
dependencies {
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
}
另外,为了使用第三方库,请确保您也具有适当的存储库,例如,一个通用存储库是 jcenter
一个:
Also in order to use 3rd party libraries ensure that you also have the appropriate repositories as well for example a common repository is the jcenter
one:
repositories {
jcenter()
}
位于 settings_fetcher
文件夹中的最终build.gradle
因此,如果不需要额外的库,最终的 build.gradle
将是这个版本:
Final build.gradle located in settings_fetcher
folder
As a result, the final build.gradle
will be this one if no extra libraries needed:
plugins {
id 'java-library'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
或者,如果您需要一些额外的依赖关系,库:
Or in case you will need some extra dependencies & libraries:
plugins {
id 'java-library'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
dependencies {
// App dependencies go here.
}
这篇关于我如何才能先构建Java模块,然后再构建其余的android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!