Skip to content

[next] Can not extend Application - Execution failed for task ':myMergeDex' #621

Closed

Description

@roblav96

Did you verify this is a real problem by searching Stack Overflow and the other open issues in this repo?

Yes.

Tell us about the problem

While trying to extend the default com.tns.NativeScriptApplication, I get dex merging errors on build.

Source

app.gradle

dependencies {
	compile 'com.android.support:multidex:1.0.0'
}
android {
	defaultConfig {
		multiDexEnabled true
	}
}

main.ts

import * as application from 'application'

@JavaProxy('com.tns.NativeScriptApplication')
class Application extends android.app.Application implements android.app.Application {

	onCreate(): void {
		super.onCreate()
		application.android.init(this)
	}

	attachBaseContext(base: android.content.Context) {
		super.attachBaseContext(base)
		android.support.multidex.MultiDex.install(this)
	}
}

application.start({ moduleName: 'main-page' })

Please provide the following version numbers that your issue occurs with:

┌──────────────────┬────────────────────────────┬────────────────┬───────────────┐
│ Component        │ Current version            │ Latest version │ Information   │
│ nativescript     │ 2.5.0-2016-10-24-6930      │ 2.3.0          │ Up to date    │
│ tns-core-modules │ 2.5.0-2016-11-08-4705      │ 2.3.0          │ Up to date    │
│ tns-android      │ 2.5.0-next-2016-11-08-1367 │ 2.3.0          │ Up to date    │
│ tns-ios          │                            │ 2.3.0          │ Not installed │
└──────────────────┴────────────────────────────┴────────────────┴───────────────┘
Node v6.9.1
Java 8
Android 6.0.1

Did the error happen while the app was being constructed? (buildtime error)

Execution failed for task ':myMergeDex'.
> Multiple dex files define Lcom/tns/NativeScriptApplication;

pastebin

Please tell us how to recreate the issue in as much detail as possible.

tns create sandbox --tsc
cd sandbox
npm install tns-core-modules@next
tns platform add android@next
<modify app.ts as stated above source>
tns run android

Notes

I used to be able to do this with v2.2.0, but sure what's happened since then. I'm only trying to extend the default com.tns.NativeScriptApplication, not create a new one.

I can easily extend the default com.tns.NativeScriptActivity like so:

@JavaProxy('com.tns.NativeScriptActivity')
class Activity extends android.app.Activity {
    ...
}

with no troubles at all.

Activity

  1. Plamen5kov commented on Nov 14, 2016

    @Plamen5kov
    Contributor

    Hi @roblav96,
    it's not a good idead to try and initialize the modules in the onCreate method of the Applicaition class. See this issue for more info and detailed description of the problem.

  2. self-assigned this
    on Nov 14, 2016
  3. Plamen5kov commented on Nov 28, 2016

    @Plamen5kov
    Contributor

    @roblav96 did you manage to build your application?

  4. roblav96 commented on Nov 28, 2016

    @roblav96
    Author

    @Plamen5kov Not the way described here, extending android.app.Application, but I worked around it at the time so I'm not able to try.

  5. Plamen5kov commented on Dec 6, 2016

    @Plamen5kov
    Contributor

    Hi @roblav96, i figured out what the problem was:

    Problem:
    We have a NativescriptApplication.java file, which is part of the template. We use this file to initialize the runtime with the following code:

      public void onCreate() {
            super.onCreate();
            com.tns.Runtime runtime = RuntimeHelper.initRuntime(this);
            if (runtime !=null) {
                runtime.run();
            }
        }
    

    When you make a javascript extend like you have:

    @JavaProxy('com.tns.NativeScriptApplication')
    class Application extends android.app.Application implements android.app.Application {
    

    we create a dex implmentation of your javascript code and name it as you have specified in the @JavaProxy decorator. This creates a problem because there's a name conflict at build time as you pointed out in the error log.

    Solution

    • One possible solution if for you to name your application class differently and also change the application name in the AndroidManifest.xml.
    • Another possible solution would be to leave the name as it is and go and delete platforms/android/src/main/java/com/tns/NativeScriptApplication.java so there is only one file that's called this way, and there's no conflict.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions