Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NativeScript - Cannot fit requested classes in a single dex file

I am working on an app using the latest version of NativeScript with Angular. I have added most of the pro-ui plugins to my project and added the Firbase plugin.

When I run “tns run android” I get the error:

“D8: Cannot fit requested classes in a single dex file. Try supplying a main-dex list.”

Also, it takes over 5 minutes to build. I have been doing some searching but I cannot find what is causing this problem. Does anyone know what is wrong?

like image 704
Graham Avatar asked Jun 29 '18 20:06

Graham


3 Answers

After much trial and error I have finally come to a solution. All other answers I've read have said that the multiDexEnabled property must be set to true in the android project's build.gradle file.

I wasn't sure how to do this for NativeScript though since the platforms folder is not committed to version control and can be potentially overwritten.

However, in your_project_root/app/App_resources/Android there are two files, settings.gradle and settings.json.

settings.gradle is used to generate your projects's build.gradle file from the properties you set in settings.json.

So, to the defaultConfig string template in settings.gradle I added:

if(__multiDexEnabled__) {
    multiDexEnabled = __multiDexEnabled__
}

And to the JSON object defined in settings.json I added:

"multiDexEnabled": true

After that I deleted my project's platforms folder and ran:

> tns platforms add android
> tns run android

And my app started up on my phone with no problems.

like image 70
Graham Avatar answered Nov 02 '22 20:11

Graham


Go to the path platforms/app/build.gradle (careful NOT /platform/build.gradle) and add the line multiDexEnabled true

...    
defaultConfig {
            def manifest = new XmlSlurper().parse(file(android.sourceSets.main.manifest.srcFile))
            def minSdkVer = manifest."uses-sdk"."@android:minSdkVersion".text() ?: 17
            minSdkVersion minSdkVer
            targetSdkVersion computeTargetSdkVersion()
            ndk {
                if (onlyX86) {
                    abiFilters 'x86'
                } else {
                    abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
                }
            }
            multiDexEnabled true
            dexOptions {
                jumboMode = true
            }
        }
...
like image 34
ittradco Avatar answered Nov 02 '22 20:11

ittradco


I have solved this by Just adding multiDexEnabled true into your app.gradle under android->defaultConfig

like image 2
Arsalan Javed Avatar answered Nov 02 '22 19:11

Arsalan Javed