Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot Build Android Release APK #1

Closed
MarkyC opened this issue Mar 31, 2016 · 13 comments
Closed

Cannot Build Android Release APK #1

MarkyC opened this issue Mar 31, 2016 · 13 comments
Labels

Comments

@MarkyC
Copy link

MarkyC commented Mar 31, 2016

Hello!

First off, thank you for this library, it has immensely sped up my development time!

I'm trying to include this library in my Android app. When building for development, everything works fine, but when I build my release APK, I get this error:

Error:Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Ljavax/inject/Inject;

When I run gradle -q dependencies, I see that there is multiple Injects defined:

+--- net.sargue:mailgun:1.0.0
|    +--- org.glassfish.jersey.media:jersey-media-multipart:[2.22,) -> 2.22.2
|    |    +--- org.glassfish.jersey.core:jersey-common:2.22.2
|    |    |    +--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |    |    +--- javax.annotation:javax.annotation-api:1.2
|    |    |    +--- org.glassfish.jersey.bundles.repackaged:jersey-guava:2.22.2
|    |    |    +--- org.glassfish.hk2:hk2-api:2.4.0-b34
|    |    |    |    +--- javax.inject:javax.inject:1
|    |    |    |    +--- org.glassfish.hk2:hk2-utils:2.4.0-b34
|    |    |    |    |    \--- javax.inject:javax.inject:1
|    |    |    |    \--- org.glassfish.hk2.external:aopalliance-repackaged:2.4.0-b34
|    |    |    +--- org.glassfish.hk2.external:javax.inject:2.4.0-b34
|    |    |    +--- org.glassfish.hk2:hk2-locator:2.4.0-b34
|    |    |    |    +--- org.glassfish.hk2.external:javax.inject:2.4.0-b34

Is there a way around this?

@sargue
Copy link
Owner

sargue commented Mar 31, 2016

Hi Marco.

The output for gradle -q dependencies is correct. The fact that you see two entries for javax.inject:javax.inject:1 just means that there are two different dependencies to that package. That doesn't mean that your build will have two instances (it shouldn't). Gradle ensures that.

I have no experience building APK releases but I would try to clean the project and rebuild. Check the directories where the intermediate files are gathered.

If the problem persists it may be that some other library on your project is including the CDI (javax.inject) with different coordinates. Unlikely, but possible.

Check these SO threads:

You can also add an explicit exclude in the gradle build file, but I think you should understand the root cause before going that way.

Check this: http://blog.gaku.net/multiple-dex-files-define-with-gradle/

@sargue
Copy link
Owner

sargue commented Mar 31, 2016

I'm closing this at I don't think is a bug of the library.

Marco, please share your findings when you manage to solve this.

@sargue sargue closed this as completed Mar 31, 2016
@MarkyC
Copy link
Author

MarkyC commented Mar 31, 2016

I think it's actually the javax.inject:javax.inject:1 and org.glassfish.hk2.external:javax.inject:2.4.0-b34 that conflict. It looks like this is not specific to your project, as I get the same error when adding org.glassfish.jersey.media:jersey-media-multipart to the project.

Workaround

To get around this, I used this approach:

compile('net.sargue:mailgun:1.0.0') {
    exclude module: 'javax.inject'
}
compile 'org.glassfish.hk2.external:javax.inject:2.4.0-b34'

@sargue
Copy link
Owner

sargue commented Apr 1, 2016

Yes, you are right. I totally missed the repackaged javax.inject (org.glassfish.hk2.external:javax.inject:2.4.0-b34) which of course is the culprit of all of this madness.

Seems a problem with javax.inject, look at this: http://stackoverflow.com/questions/24598961/this-dependency-gives-me-two-versions-of-one-jar-how-do-i-fix-this

Seems that the problem is HK2, the dependency injection framework used by Jersey. Explained here: http://stackoverflow.com/questions/25212944/why-does-hk2-repackage-everything

Thanks for the research!

You can also exclude the other way; exclude the repackaged HK2 which may provide more sense. It does for me so I've included this in my projects depending on mailgun:

configurations {
    compile.exclude group: 'org.glassfish.hk2.external', module:'javax.inject'
}

Or even better (to include test suites, for example):

configurations {
    all*.exclude group: 'org.glassfish.hk2.external', module:'javax.inject'
}

@roiemandler
Copy link

I am trying to run this in eclipse and no matter what I try to exclude/include some of the hk2 dependency when I run my code and try to use the mailgun wrapper you wrote I get the following:

Exception in thread "Thread-10" java.lang.NoSuchMethodError: org.glassfish.hk2.api.ServiceLocatorFactory.create(Ljava/lang/String;Lorg/glassfish/hk2/api/ServiceLocator;Lorg/glassfish/hk2/extension/ServiceLocatorGenerator;Lorg/glassfish/hk2/api/ServiceLocatorFactory$CreatePolicy;)Lorg/glassfish/hk2/api/ServiceLocator;

I am using maven in eclipse.

Any idea?

@sargue
Copy link
Owner

sargue commented Jun 6, 2016

Hi. Thanks for reporting.

Could you please post your pom.xml and the full stacktrace?

@danmtplay
Copy link

I get following exceptions with your mailgun 1.1.0.

FATAL EXCEPTION: main
javax.ws.rs.ProcessingException
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:261)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343)
at net.sargue.mailgun.Mail.send(Mail.java:38)
...

in my app level gradle,
compileSdkVersion 23
buildToolsVersion 23.0.3

defaultConfig {
minSDKVersion 17
targetSDKVersion 23
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.google.android.gms:play-services-maps:9.2.0'
compile ('net.sargue:mailgun:1.1.0') {
exclude group: 'javax.inject', module: 'javax.inject'
}
compile 'org.glassfish.hk2.external:javax.inject:2.4.0-b34'
}

I use Android Studio 2.1.2.
I hope your help.

@MarkyC
Copy link
Author

MarkyC commented Jul 25, 2016

@danmtplay Can you post the full stack trace? I see that in ClientRuntime.java:261 it is repackaging the thrown Exception as a ProcessingException. With more of the stack trace we could see the cause (caused by ... etc)

@sargue
Copy link
Owner

sargue commented Jul 25, 2016

Thanks @MarkyC for the help. Also, @danmtplay , post how are you invoking the library to send the message. Thanks.

@danmtplay
Copy link

Thanks @MarkyC, @sargue for help.

this is my exception full stack.
com.dolphin.apppro E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dolphin.apppro, PID: 20250
javax.ws.rs.ProcessingException
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:261)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343)
at net.sargue.mailgun.Mail.send(Mail.java:38)
at com.dolphin.apppro.ServiceRequestFragment.onClick(ServiceRequestFragment.java:186)
at android.view.View.performClick(View.java:4471)
at android.view.View$PerformClick.run(View.java:18778)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:254)
at org.glassfish.jersey.client.internal.HttpUrlConnector$4.getOutputStream(HttpUrlConnector.java:385)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:200)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:194)
at org.glassfish.jersey.message.internal.CommittingOutputStream.write(CommittingOutputStream.java:228)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$UnCloseableOutputStream.write(WriterInterceptorExecutor.java:299)
at java.io.OutputStreamWriter.flushBytes(OutputStreamWriter.java:167)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:158)
at org.glassfish.jersey.message.internal.ReaderWriter.writeToAsString(ReaderWriter.java:193)
at org.glassfish.jersey.message.internal.AbstractMessageReaderWriterProvider.writeToAsString(AbstractMessageReaderWriterProvider.java:130)
at org.glassfish.jersey.message.internal.AbstractFormProvider.writeTo(AbstractFormProvider.java:116)
at org.glassfish.jersey.message.internal.FormProvider.writeTo(FormProvider.java:112)
at org.glassfish.jersey.message.inter

and this is my module to send message to mailgun.
Configuration conf = new Configuration()
.domain("sandbox______.mailgun.org")
.apiKey("key-da3773______")
.from("App Pro", "noreply@apppro.com");

Mail.using(conf).to("Dan Matei", "danmtplay@hotmail.com")
.subject("App Report")
.text(messageBody)
.build().send();

this is my app level build.gradle.
android {
compileSdkVersion 24
buildToolsVersion '24.0.1'
defaultConfig {
applicationId "com.dolphin.apppro"
minSdkVersion 17
targetSdkVersion 24
versionCode 4
versionName "1.0"
multiDexEnabled true
signingConfig signingConfigs.config
}
dexOptions {
javaMaxHeapSize "4g"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
pickFirst 'META-INF/services/javax.ws.rs.ext.MessageBodyReader'
pickFirst 'META-INF/services/javax.ws.rs.ext.MessageBodyWriter'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:design:24.1.1'
compile 'com.android.support:multidex:1.0.1'
compile 'com.google.android.gms:play-services-maps:9.2.1'
compile('net.sargue:mailgun:1.1.0') {
exclude group: 'javax.inject', module: 'javax.inject'
}
compile 'org.glassfish.hk2.external:javax.inject:2.4.0-b34'
}

this is my project level build.gradle.
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
}

this is my current gradle version.
distributionUrl=https://services.gradle.org/distributions/gradle-2.13-all.zip

my jdk version - jdk1.8.0_74.jdk

could you let me know what is error?
and all version setting of your running example?

Thanks again.

@sargue
Copy link
Owner

sargue commented Jul 25, 2016

I am not an Android expert but it seems you can't run network code in the main thread.

See this: http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception

@danmtplay
Copy link

Thanks @sargue.

You are right.
I changed network code to execute in background.
So it is good working now.

Thanks again.

@hemant16apr
Copy link

hemant16apr commented Apr 1, 2018

I am using Glassfish 4.0 with Java 8.x and JEE 7.0. I get the following exception when I try to build and create a war file.

Exception:

remote failure: Error occurred during deployment: Exception while loading the app : 
CDI deployment failure:WELD-001408 Unsatisfied dependencies for type [Providers] with qualifiers [@Default] at injection point [[BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject public org.glassfish.jersey.media.multipart.internal.MultiPartReaderServerSide(@Context Providers, Provider<CloseableService>)]. Please see server.log for more details.
Command deploy failed.

Here is my POM.xml

		<dependency>
			<groupId>org.glassfish.jersey.core</groupId>
			<artifactId>jersey-client</artifactId>
			<version>2.25</version>
		</dependency>
		<dependency>
			<groupId>org.glassfish.jersey.media</groupId>
			<artifactId>jersey-media-multipart</artifactId>
			<version>2.25</version>
		</dependency>
		<dependency>
			<groupId>net.sargue</groupId>
			<artifactId>mailgun</artifactId>
			<version>1.5.0</version>
		</dependency>

I would appreciate any help. Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants