Android-runtime: [next] Can not extend Application - Execution failed for task ':myMergeDex'

Created on 9 Nov 2016  ยท  4Comments  ยท  Source: NativeScript/android-runtime

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.

bug

All 4 comments

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.

@roblav96 did you manage to build your application?

@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.

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.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

zahid-dapperapps picture zahid-dapperapps  ยท  5Comments

NathanaelA picture NathanaelA  ยท  4Comments

romulowspp picture romulowspp  ยท  4Comments

NathanaelA picture NathanaelA  ยท  3Comments

zahid-dapperapps picture zahid-dapperapps  ยท  3Comments