I'm building an Android library (aar) in Java.
I want to add a Kotlin module to it. It works great when developing but the sources of the module are missing from the .aar file so it crushes at runtime in the application with FileNotFoundException.
The main module build.gradle looks like this:
apply plugin: "com.android.library"
apply plugin: "com.jfrog.artifactory"
apply plugin: "maven-publish"
...
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
implementation project(':kotlinModule')
....
}
Thre build.gradle of the module is:
apply plugin: 'java-library'
apply plugin: 'kotlin'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
sourceCompatibility = "7"
targetCompatibility = "7"
I've tried to add a source set but apparently you can't add source sets from outside your module folder.
I've tried to copy the kotlinModule.jar file into libs folder but then I need to rebuild every time I make a change to the module.
How can I make the sources of the module to be bundled into the .aar file?
If you want to add kotlinModule.aar file then you need to add some configuration in your Gradle config inside your android tag
android {
repositories {
flatDir {
dirs 'libs'
}
jcenter()
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
Also, add your .aar file name in your Gradle dependencies
dependencies {
implementation name: 'kotlinModule', ext: 'aar'
}
Related
In my java project I have the following folders:
app that contains the android specific application
settings_fetcher that is a java module
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.
The app folder contains the following 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'
}
Whilst settings_fetcher contains the following 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 build
gradlew build^flavor^
Where ^flavor^ is my application's flavor and can be either Debug or Release.
To be able to build the .jar file first and then my app's apk.
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:
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")
}
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.
Library Dependencies
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.
For example if your library needs the okHttp client then use:
dependencies {
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
}
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()
}
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.
}
Trying out to use ObjectBox as java desktop database. However after following the documentation on the web site https://docs.objectbox.io/java-desktop-apps its not working. No MyObjectBox found error.
I am using eclipse ide Version: 2020-09 (4.17.0), Gradle: gradle-6.7.1
ObjectBox seems not creating the model automatically after build (no model folder generated). I have created the class using the Entity annotation, build the project eclipse, nothing happens. Anyone any ideas?
Works in android but not desktop. As i am not familiar with gradle project in eclipse. the following is the build file
buildscript {
ext.objectboxVersion = '2.8.1'
repositories {
jcenter()
}
dependencies {
classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
}
}
apply plugin: 'java-library'
apply plugin: 'io.objectbox'
targetCompatibility = '1.8'
sourceCompatibility = '1.8'
repositories {
jcenter()
}
dependencies {
implementation "io.objectbox:objectbox-linux:$objectboxVersion"
implementation "io.objectbox:objectbox-macos:$objectboxVersion"
implementation "io.objectbox:objectbox-windows:$objectboxVersion"
}
apply plugin: 'io.objectbox'
dependencies {
implementation "io.objectbox:objectbox-java:$objectboxVersion"
annotationProcessor "io.objectbox:objectbox-processor:$objectboxVersion"
}
apply plugin: 'io.objectbox'
Doing apply plugin: 'io.objectbox' three times does not look good. Once is enough. Please check the ObjectBox Java examples for a working setup. In your case have a closer look at the java-main example for standalone Java applications.
This is the basic structure with ... where I left out the details (check the full build.gradle file from the example):
buildscript {
...
}
apply plugin: 'java'
apply plugin: 'application'
targetCompatibility = '1.8'
sourceCompatibility = '1.8'
mainClassName = "io.objectbox.example.Main"
dependencies {
...
}
// Apply plugin after dependencies block so they are not overwritten.
apply plugin: 'io.objectbox'
Maybe checkout the example and start from there?
After a lot of searching, it was the last option to raise it here! In eclipse, I am designing such project structure using Gradle, as shown below...
Algorithms //Parent Project
-SubModuleCore //Child Project for common utilities & dependencies
-build.gradle
-SubModuleOne //Child project for any operation
-build.gradle //Added 'SubModuleCore' as a dependency like compile project(':SubModuleCore')
-SubModuleTwo //Child project for another operation
-build.gradle //Added 'SubModuleCore' as a dependency like compile project(':SubModuleCore')
-build.gradle
-settings.gradle
Services //Stand-Alone project
-build.gradle //Here I want to add 'Algorithms' as a single dependency
-settings.gradle
Project structures are same in eclipse work-space as shown above. I am able to generate individual .jar of Algorithms project. So the problem is I want to add this Algorithms project as a single dependency in project Services like compile project(':Algorithms'). But eclipse just saying 'shut-up!'. I don't want to publish it somewhere like maven central / jitpack etc. instead I want to do it locally. I'm trying this way...
Services/build.gradle
apply plugin: 'war'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'
project.webAppDirName = 'WebContent'
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile fileTree(dir: 'lib', include: ['*.jar'])
compile project(':Algorithms')
}
Services/settings.gradle
rootProject.name = 'Services'
include 'Algorithms'
project(':Algorithms').projectDir = new File(settingsDir, '../Algorithms')
Algorithms/build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = "1.8";
targetCompatibility = "1.8";
subprojects {
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = "1.8";
targetCompatibility = "1.8";
buildscript {
dependencies {
repositories {
jcenter()
mavenCentral()
}
}
}
repositories {
jcenter()
mavenCentral()
}
}
subprojects.each { subproject ->
evaluationDependsOn(subproject.path)
}
jar.dependsOn subprojects.tasks['classes']
jar {
baseName = 'algorithms'
subprojects.each { subproject ->
from subproject.sourceSets.main.output.classesDir
}
from files('resources/log4j2.xml')
from files('resources/application.properties')
}
Algorithms/settings.gradle
rootProject.name = 'Algorithms'
include ':SubModuleCore', ':SubModuleOne', ':SubModuleTwo'
I tried several solutions from SO, still not succeeded. Somebody please help me, I got stuck badly here. It seems I am very close to this, but don't know what is missing!
Thanks
You can use the includeBuild feature.
Declare the included build in Services/settings.gradle
rootProject.name='Services'
includeBuild '../Algorithms'
and then express the dependency using
compile "${project.group}:${project.name}"
where project group and name the one from the Algorithms project.
I am trying to use DexGuard plugin in Java project whith Gradle. It is library project for android.
But i want to link DexGuard library whitout:
apply plugin: 'com.android.application'
Because I need to use:
apply plugin: 'java'
Is it possible to use DexGuard plugin such a way?
I need this way to use because i need to use an additional plugin:
apply plugin: 'com.github.johnrengelman.shadow'
And I have a problem to use this plugin in conjunction whith android plugin...
My gradle:
buildscript {
repositories {
jcenter()
flatDir dirs: 'DexGuard/lib'
}
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2'
classpath ':dexguard'
}
}
apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: '**/*.jar')
}
sourceSets {
main {
java.srcDirs = ['src']
}
}
shadowJar {
...
}
task sdkDexguard(type: com.saikoa.dexguard.gradle.DexGuardTask) {
configuration 'dexguard.txt'
injars 'build/classes'
injars 'libs'
outjars 'build/application.apk'
}
I can not to build tasklist. Error in line task sdkDexguard:
Could not find property 'com' on root project
UPD
Problem in the library DexGuard 6.1.11 for standalone usage. GuardSquare team will solve that soon.
Problem solved in version 7.0.31. Now possible to use this method for standalone usage.
apply plugin: 'java'
archivesBaseName = 'foo-client'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
//version = '1.0'
dependencies{
compile 'foo:bar:1.0-SNAPSHOT',
'foo:bar-common:1.0-SNAPSHOT',
'foo:bar-communication-api:1.0-SNAPSHOT'
}
repositories{
flatDir{
dirs '/lib'
}
}
When executing the inherited jar task, this will create foo-client.jar in the build/lib directory of the project. What I need to do is include several jars to be put in that same directory, otherwise the project will not run correctly.
How can I do this?
You could define the external jars as runtime dependencies and then use a copy task to copy them to build/lib, e.g.:
task copyLibs(type: Copy) {
from configurations.runtime
into "$buildDir/libs"
}
Using Gradle 1.1 the following did the trick,
apply plugin: 'java'
archivesBaseName = 'foo-client'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
task copyToLib(type: Copy){
into 'bar\\lib'
from configurations.runtime
from configurations.default.allArtifacts.getFiles()
}
dependencies{
compile 'foo:bar-agent-dist:2.0.0-SNAPSHOT',
'foo:bar-common:2.0.0-SNAPSHOT',
'foo:bar-communication-api:2.0.0-SNAPSHOT'
}
repositories{
flatDir{
dirs '/lib'
}
}