Unity 5.6 custom gradle configuration with Fabric - android-gradle-plugin

I would like to configure Fabric dependencies for Unity Android build using gradle. I'm now exporting the project and using Android Studio to get rid of the errors and then prepare a custom working "mainTemplate.gradle" so I can build directly from Unity 5.6.
Here are the configured dependencies as Unity suggested:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// other dependencies
compile project(':answers')
compile project(':beta')
compile project(':crashlytics')
compile project(':crashlytics-wrapper')
compile project(':fabric')
compile project(':fabric-init')
}
Each of the Fabric folders is treated as a library that has its own gradle config.
Here are the errors I'm getting (due to a file used in same namespace of two "libraries"):
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lio/fabric/unity/crashlytics/android/BuildConfig;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lio/fabric/unity/android/BuildConfig;
I tried adding the following but it did not work:
android {
dexOptions {
preDexLibraries = false
}
I also tried without success:
task androidReleaseJar(type: Jar, dependsOn: assembleRelease) {
from "$buildDir/intermediates/classes/release/"
exclude '**/BuildConfig.class'
}

Here is how I solved this issue:
By default each fabric folder is treated as a project however only "fabric" needs to be handled as one since it contains a "res" folder and an "AndroidManifest.xml" file with required meta data values. So I just kept only "fabric" as a project and changed the other dependencies to be handled as simple *.jar files.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(dir: 'fabric-init/libs', include: ['*.jar'])
compile fileTree(dir: 'crashlytics-wrapper/libs', include: ['*.jar'])
compile fileTree(dir: 'crashlytics/libs', include: ['*.jar'])
compile fileTree(dir: 'beta/libs', include: ['*.jar'])
compile fileTree(dir: 'answers/libs', include: ['*.jar'])
compile project(':fabric')
}
and in Settings.gradle I keep only one project reference:
//include 'answers'
//include 'beta'
//include 'crashlytics'
//include 'crashlytics-wrapper'
include 'fabric'
//include 'fabric-init'

You can disable generation of BuildConfig java class by changes in only one file (without Fabric modifications). Place this at end of Plugins/Android/mainTemplate.gradle for all your problem projects:
['crashlytics', 'crashlytics-wrapper', 'fabric', 'fabric-init'].each { name ->
project(":$name").tasks.whenTaskAdded { task ->
if (task.name == 'generateDebugBuildConfig' || task.name == 'generateReleaseBuildConfig' ) {
task.enabled = false
}
}
}

Related

How to repair broken jar built from an IntellIJ gradle Kotlin project?

I try to build a working jar from my project by IntellIJ.
(I add the artifact and build it from the build menu)
I literally can build it as a clean helloworld but as I keep adding the dependencies it breaks.
The built artifact those not find my main class:
Error: Could not find or load main class hu.qlmdc.drp.HelloWorldKt
After a lot of tests I found the exact dependency causing the problem:
"com.microsoft.sqlserver:mssql-jdbc:6.2.2.jre8"
Without it the project works but I need the dependency.
What causes this?
How can I repair the artifact?
My gradle file:
plugins {
id "application"
id "java"
id "war"
id "org.jetbrains.kotlin.jvm" version "1.5.0"
}
group 'hu.qlmdc.drp'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
flatDir {
dirs 'lib'
}
maven { url 'http://repo.jenkins-ci.org/releases/' }
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: '1.5.0'
implementation group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.5.0', ext: 'pom'
implementation "io.ktor:ktor-server-core:1.5.4"
implementation "io.ktor:ktor-server-netty:1.5.4"
implementation "io.ktor:ktor-metrics:1.5.4"
implementation "io.ktor:ktor-locations:1.5.4"
implementation "io.ktor:ktor-gson:1.5.4"
implementation "io.ktor:ktor-client-core:1.5.4"
implementation "io.ktor:ktor-client-apache:1.5.4"
implementation "ch.qos.logback:logback-classic:1.2.3"
//implementation 'mysql:mysql-connector-java:8.0.23'
implementation "com.microsoft.sqlserver:mssql-jdbc:6.2.2.jre8"
implementation "org.hibernate:hibernate-core:5.4.31.Final"
implementation "org.apache.httpcomponents:httpclient:4.5.13"
implementation "org.apache.httpcomponents:fluent-hc:4.5.13"
// Jenkins plugins
implementation group: 'org.jenkins-ci.plugins', name: 'credentials', version: '2.3.15', ext: 'jar'
implementation group: 'org.jenkins-ci.plugins', name: 'matrix-auth', version: '2.6.6', ext: 'jar'
implementation group: 'org.jenkins-ci.plugins.workflow', name: 'workflow-cps', version: '2.90', ext: 'jar'
implementation fileTree(dir: 'lib', include: ['*.jar'])
}

io.micronaut.runtime.Micronaut - No embedded container found

I'm trying to build a Kotlin application but, even with successfully build, I face with the error bellow. What I'm doing wrong?
▶ java -jar build/libs/app-0.1.jar
22:10:02.122 [main] INFO io.micronaut.runtime.Micronaut - No embedded container found. Running as CLI application
Here is my build status:
▶ ./gradlew assemble
BUILD SUCCESSFUL in 3s
14 actionable tasks: 1 executed, 13 up-to-date
That is the part of my gradle.build file:
apply from: "dependencies.gradle"
apply from: "protobuf.gradle"
version "0.1"
group "app"
mainClassName = "app.Application"
dependencies {
compile "ch.qos.logback:logback-classic:1.2.3"
}
jar {
manifest {
attributes "Main-Class": mainClassName
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
I had a similar issue:
I was able to run code from IDE, but failed to run Docker container with the app
And had compile 'io.micronaut:micronaut-http-server-netty:1.1.0' in my build.gradle.
Also I was using shadowJar plugin and there was the issue. Adding:
shadowJar {
mergeServiceFiles()
}
solved the problem. It transforms entries in META-INF/services resources into a single resource. My shadowed jar file contained a lot of entries in this folder.
It is difficult to say for sure without seeing the project but one thing that could cause that issue would be not having a dependency on io.micronaut:micronaut-http-server-netty. A newly created app will have something like this in build.gradle...
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}"
compile "org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}"
compile "io.micronaut:micronaut-runtime"
compile "io.micronaut:micronaut-http-client"
// Make Sure You Have This...
compile "io.micronaut:micronaut-http-server-netty"
kapt "io.micronaut:micronaut-inject-java"
kapt "io.micronaut:micronaut-validation"
kaptTest "io.micronaut:micronaut-inject-java"
runtime "ch.qos.logback:logback-classic:1.2.3"
runtime "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.4.1"
testCompile "org.junit.jupiter:junit-jupiter-api:5.1.0"
testCompile "org.jetbrains.spek:spek-api:1.1.5"
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.1.0"
testRuntime "org.jetbrains.spek:spek-junit-platform-engine:1.1.5"
}

IntelliJ Idea 16 CE gradle issues

In my gradle file I am trying to use the following to solve another issue. This is my gradle file:
group 'com.winapp'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'src/main/java/libs', include: ['*.jar'])
compile 'com.intellij:forms_rt:6.0.5'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
task copyDependenciesToTarget(type: Copy) {
println 'Copying dependencies to target...'
configurations.compile.collect().each { compileDependency ->
copy {
with from (compileDependency.getPath()) {
include '*'
}
into 'target/libs/libs'
}
}
}
build.dependsOn(copyDependenciesToTarget)
jar {
manifest.attributes(
"Main-Class": "Main",
"Class-Path": configurations.compile.collect { 'libs/' + it.getName()}.join(' ')
)
}
The problem are is this:
configurations.compile.collect().each { compileDependency ->
copy {
with from (compileDependency.getPath()) {
include '*'
}
into 'target/libs/libs'
}
}
When I run the application I get this Exception:
org.gradle.api.internal.file.copy.CopySpecWrapper_Decorated cannot be cast to org.gradle.api.internal.file.copy.CopySpecInternal
Main problem is, I have no idea how to fix this error, I simply want my project to create a JAR file that works with JDBC, using this gradle code seems to be a solution for that issue, but now I have run into another problem, yet again.
Please let me know if you require any additional information and thank you in advance. Literally, I cant even. This problem.
EDIT
As stated in the comments. My project runs fine when I run it through the IDE. There is an issue when I create a JAR file using gradle. The bare gradle file that IntelliJ created when I started the gradle project follows (Added the jar config so my Main class would be picked up):
group 'com.winapp'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'src/main/java/libs', include: ['*.jar'])
compile 'com.intellij:forms_rt:6.0.5'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
jar {
manifest.attributes(
"Main-Class": "Main",
"Class-Path": configurations.compile.collect { 'libs/' + it.getName()}.join(' ')
)
}
When I execute my app from terminal after the JAR has been created (keeping in mind there are no issues when hitting run in the IDE) with java -jar myAppName.jar:
java.lang.ClassNotFoundException: org.sqlite.JDBC
NOTE: Usually I get a full stack running a java app, but in this case the above is the only output.
I am using this JAR for my sqlite needs:
sqlite-jdbc-3.15.1
As a test I commented out the Sqlite usage in my application and the JAR file worked fine. My GUI was displayed and everything went as expected (all things considered). The JAR stops working when I add the sqlite JAR file usage (code). Added it the same as my other libs (like retrofit), so this seems to be quite a strange issue.
Please let me know if I have explained the issue correctly?

Android espresso-contrib gradle build failing

I'm trying to learn android espresso.. I followed some basic tutorials and it was working fine. But now I want to do some tests on the android navigation drawer. For that I need to use gradle dependency androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2' but it's causing conflict with other dependencies. My gradle file :
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "my.com.myapp_android"
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
jcenter()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
//material design
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
//zxing
compile 'com.journeyapps:zxing-android-embedded:3.2.0#aar'
compile 'com.google.zxing:core:3.2.1'
//Testing
// Optional -- Mockito framework
testCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.android.support:support-annotations:23.3.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.4.1'
// Optional -- Hamcrest library
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
// Optional -- UI testing with Espresso
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
// Optional -- UI testing with UI Automator
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
//inMarketSDK
//compile group: 'com.inmarket', name: 'm2msdk', version: '2.29', ext: 'aar'
}
Error is something like this:
Error:Conflict with dependency 'com.android.support:support-v4'. Resolved versions for app (23.3.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
Error:Conflict with dependency 'com.android.support:appcompat-v7'. Resolved versions for app (23.3.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
followed this : link for espresso install
I also tried to exclude annotation dependency :
androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.2') {
// Necessary if your app targets Marshmallow (since Espresso
// hasn't moved to Marshmallow yet)
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2')
{
// Necessary if your app targets Marshmallow (since Espresso
// hasn't moved to Marshmallow yet)
exclude group: 'com.android.support', module: 'support-annotations'
}
TL;DR;
New version of espresso-contrib 2.2.2 library has now dependency on com.android.support:appcompat-v7:23.1.1 resulting into conflict when using different version of appcompat-v7 in our compile time dependency like below:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
}
To avoid conflict when we exclude appcompat-v7 dependency from espresso-contrib like below it breaks again due to some value dependencies on design support lib.
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){
exclude module: 'support-annotations'
exclude module: 'support-v4'
exclude module: 'support-v13'
exclude module: 'recyclerview-v7'
exclude module: 'appcompat-v7'
}
Error:
Error:(69) Error retrieving parent for item: No resource found that matches the given name 'TextAppearance.AppCompat.Display1'.
Root cause:
This is because the design support lib has dependency on
appcompat-v7.
So,when we exclude 'appcompat-v7' module from
espresso-contrib dependencies(like above) , the design support lib downloaded as
part of transitive dependency of espresso-contrib lib couldn't find
the compatible version of appcompat-v7 lib(23.1.1) it is using
internally in its resources files and thus gives out the above error.
So, the solution to above problem is to exclude 'design-support' lib dependency from espresso-contrib like below:
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){
exclude module: 'support-annotations'
exclude module: 'support-v4'
exclude module: 'support-v13'
exclude module: 'recyclerview-v7'
exclude module: 'design'
}
That solves the conflict problem!
LONGER VERSION (in case someone is interested):
To found out the reasons of various conflict issues we face when using `espresso-contrib' library i have created sample app to find out the root cause.
Step 1:Using Espresso-Contrib Lib version 2.2.1
Created App to use 'espresso-contrib' lib version 2.2.1 by adding following lines in app/build.gradle file:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
}
Note: In this case,I am not importing any other support library components like
appcompat-v7,recyclerview-v7,etc.
The dependency graph for the above setup looks like below:
As can be seen that espresso-contrib 2.2.1lib has transitive dependencies on version 23.0.1 of
support-v4,recyclerview-v7,support-annotations ,etc.
As i am not defining dependencies for recyclerview-v7,support-annotations in my project the above setup would work just fine.
But when we define those as compile dependencies [like below] in our project we get version conflict issues as stated in your question.
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
To avoid those conflicts we add below line to our espresso-contrib lib:
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1'){
exclude module: 'support-annotations'
exclude module: 'support-v4'
exclude module: 'support-v13'
exclude module: 'recyclerview-v7'
}
This makes sure those dependencies aren't downloaded as part of espresso-contrib transitive dependencies.
Everything runs fine with above setup.No issues!
Step 2: Using Espresso-Contrib lib version 2.2.2
Changed App's build.gradle to use 'espresso-contrib' lib version 2.2.2 by changing the previous build.gradle file:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
testCompile 'junit:junit:4.12'
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){
exclude module: 'support-annotations'
exclude module: 'support-v4'
exclude module: 'support-v13'
exclude module: 'recyclerview-v7'
}
}
But when i build project using above setup..build fails with error posted in question..
Error:
Error:Conflict with dependency 'com.android.support:appcompat-v7'. Resolved versions for app (23.3.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
So, looking at error i added one more line to above build.gradle:
exclude module: 'appcompat-v7' (inside androidTestCompile block of espresso-contrib)
But that doesn't resolves the conflict issue and i get value dependencies error posted in comments.
So i check for dependency graph of my app again:
As can be seen now that espresso-contrib 2.2.2 lib has now transitive dependency on com.android.support:design:23.1.1 causing the above conflict.
So, we need to add below line inside androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2') block:
exclude module: 'design'
This resolves the conflict issue in lib version 2.2.2!
For some reason, it has also started to happen to me out of the blue.
The only thing that solved it was to invalidate caches and restart
do below
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1'){
exclude module: 'support-annotations'
exclude module: 'support-v4'
exclude module: 'recyclerview-v7'
}

Intellij-idea gradle sub-module keeps forgetting root project dependency

I have two gradle projects defined like so
apiclient (root)
| src\
| build.gradle
|--- android (module)
| src\
| build.gradle
These projects are not on maven, yet, and the module apiclient.android depends on the root module (apiclient).
I keep adding apiclient using the project structure... -> dependency dialog and the reference keeps getting removed from the .iml file whenever I click refresh in the gradle tool window.
how can I keep the child module from forgetting the parent module dependency?
edit: as requested the build.gradle files.
apiclient build.gradle
group 'emby.apiclient'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'com.google.guava:guava:18.0'
compile 'org.java-websocket:Java-WebSocket:1.3.0'
}
apiclient.android build.gradle
group 'emby.apiclient'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'org.codehaus.groovy:groovy-all:2.3.11'
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'com.google.code.gson:gson:2.5'
compile 'com.mcxiaoke.volley:library:1.0.16'
compile 'com.squareup.okhttp:okhttp:2.1.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.1.0'
compile 'emby.apiclient:apiclient:1.0-SNAPSHOT'
}
The last line compile 'emby.apiclient:apiclient:1.0-SNAPSHOT' is underlined red in the gradle project window. I've tried compile project 'apiclient' as well and the outcome is unchanged.
settings.gradle
rootProject.name = 'apiclient'
include 'android'
findProject(':android')?.name = 'emby.apiclient.android'
In the /android build.gradle file, you need to add compile project(":apiclient") to your dependency list. I found out the hard way that IntelliJ's "Do you want to add a dependency to classpath" dialog only adds it to the .iml file, which isn't run as part of the build.