MultiDexApplication not recognized - android

Trying to use MultiDexApplication in my app, but the class is not recognized when I try to extend my application activity with it.
Here is my build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion '21.0.1'
defaultConfig {
applicationId 'com.myapp'
minSdkVersion 10
targetSdkVersion 21
versionCode 115
versionName '4.8'
}
buildTypes {
debug {
debuggable true
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
debuggable false
runProguard true
zipAlign true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
lintOptions {
checkReleaseBuilds false
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services:6.1.11'
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.viewpagerindicator:library:2.4.1#aar'
compile project(':facebook')
}
You can see that I'm compiling on 21, using the latest build tools, and the latest google play services and support library.
Has anyone gotten this to work?

MultiDexApplication class is not part of appcompat-v7 library. It is being shipped in a separate jar (called android-support-multidex).
Find the android-support-multidex.jar under /sdk/extras/android/support/multidex/library/libs (available from revision 21 of support library) and copy it to your project's libs folder.
Update (11/5/2014):
The jar is now available in central repository:
dependencies {
...
compile 'com.android.support:multidex:1.0.0'
}
For more info, see here.
Update (27/9/2021):
Jetpack (AndroidX) users should add this as a dependency:
dependencies {
...
implementation 'androidx.multidex:multidex:2.0.1'
}

Although this question is quite old, I got this error in a multi-module setup when trying to build the different modules together as one APK for API < 21. I already refactored to AndroidX, but the multidex docs don't mention AndroidX yet.
If you are using AndroidX, make sure to replace the old multidex dependency
compile 'com.android.support:multidex:1.0.3'
with the new one
implementation 'androidx.multidex:multidex:2.0.0'

I have followed THIS blog post according to which MultiDexApplication should be included in r21 of support library.
My IDE had trouble resolving it also.
I made it work for now with the help of MULTIDEX github project by adding (you can see more details on the project's page):
android {
dexOptions {
preDexLibraries = false
}
}
repositories {
jcenter()
}
dependencies {
compile 'com.google.android:multidex:0.1'
}
afterEvaluate {
tasks.matching {
it.name.startsWith('dex')
}.each { dx ->
if (dx.additionalParameters == null) {
dx.additionalParameters = []
}
dx.additionalParameters += '--multi-dex' // enable multidex
dx.additionalParameters += "--main-dex-list=$projectDir/multidex.keep".toString()
}
}
and adding $project_dir/multidex.keep file with following contents:
android/support/multidex/BuildConfig.class
android/support/multidex/MultiDex$V14.class
android/support/multidex/MultiDex$V19.class
android/support/multidex/MultiDex$V4.class
android/support/multidex/MultiDex.class
android/support/multidex/MultiDexApplication.class
android/support/multidex/MultiDexExtractor$1.class
android/support/multidex/MultiDexExtractor.class
android/support/multidex/ZipUtil$CentralDirectory.class
android/support/multidex/ZipUtil.class
The github project page mentions also some consideration for the contents of your implementation of MultiDexApplication class:
The static fields in your application class will be loaded before the
MultiDex#installbe called! So the suggestion is to avoid static fields
with types that can be placed out of main classes.dex file.
The methods of your application class may not have access to other classes
that are loaded after your application class. As workarround for this,
you can create another class (any class, in the example above, I use
Runnable) and execute the method content inside it.

I got the solution :)
Upgrade to jdk 8 and change JDK location in Android Studio in
File > Project Structure > SDK Location
Find and change JDK location and click OK

Related

Lint found fatal errors while assembling a release target - gradle error?

I am using AS 3.1 with gradle-4.5-all.zip and main build.gradle:
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
}
}
allprojects {
repositories {
jcenter()
google()
}
}
An app-level build.gradle looks like following:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "com.example"
minSdkVersion 14
targetSdkVersion 27
versionCode 6
versionName "1.00"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
implementation 'ch.acra:acra:4.6.1'
implementation 'commons-validator:commons-validator:1.5.0'
implementation 'com.android.support:support-v13:27.1.1'
implementation 'com.google.code.gson:gson:2.8.0'
implementation 'com.nineoldandroids:library:2.4.0'
implementation 'com.google.zxing:core:3.3.0'
}
and works fine when I set up a debug version under AS 3.1 to my phone, but when I try to make release apk it shows me an error:
Lint found fatal errors while assembling a release target.
To proceed, either fix the issues identified by lint, or modify your build
script as follows:
...
android {
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
}
As I can see in the lint-results-release-fatal.html the reason is:
I would not like to change lintOptions to supress this error because it doesn't solve the problem, it just hide it. More over, when I use
implementation files('libs/commons-validator-1.5.0.jar')
instead of
implementation 'commons-validator:commons-validator:1.5.0'
the release apk is compiled without any error messages. Is it a some gradle bug or what!?
P.S. I have attached a file androidDependencies.txt. Package commons-logging doesn't appears in the dependencies at all! How is it possible to get the solution of above problem analysing this file?
the release apk is compiled without any error messages. Is it a some
gradle bug or what!?
It seems like that dependency has a package which conflicts with the Android itself. The reason why it works without implementation and adding it manually, it might be that it downloads needed packages when you add it to be downloaded from maven repository and that's when the issue came up.
Anyways, the solution at these situations might be using the latest version:
implementation 'commons-validator:commons-validator:1.6'
Or, exclude it as follows:
implementation ('commons-validator:commons-validator:1.5.0') {
exclude group: 'commons-logging', module: 'commons-logging'
}
Note: The following part can't be helpful (for this issue) since the error says:
Commons-logging defines classes that conflict with classes now
provided by Android
You could go deeply by running ./gradlew app:dependencies in the IDE terminal to see which one conflicts with the Android itself and then excluding it as above.

Data binding cannot be added to my project

In my Android project, I have needed to enable the dataBinding library in module level build.gradle as below, but it gives me the error in the image. How can resolve it?
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion "27.0.2"
defaultConfig {
applicationId "com.nasser.studio.multipledeletelistview"
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding{
enabled = true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:27.0.2'
compile 'com.codesgood:justifiedtextview:1.0.2'
}
Edit 1.
I've changed the project level build.gradle to add support-v4 library, but now it throws the following error:
repositories {
google()
jcenter()
configurations.all {
resolutionStrategy.force "com.android.support:support-v4:27.0.2"
}
}
one of your 3rd party libraries or sdk you use rely on support version 21.0.3. Either remove it or have resolutionStrategy in your Gradle.
configurations.all {
resolutionStrategy {
force ....
}
}
Try upgrade your android gradle plugin.
Add compile "com.android.support:support-v4:27.0.2" manually to your gradle file.
That should solve your problem.
That's not a Databinding error, It's just saying that all your support-related libraries should use the same version. For example look at my gradle file:
implementation "com.android.support:recyclerview-v7:$libraries.googleSupportVersion"
implementation "com.android.support:appcompat-v7:$libraries.googleSupportVersion"
implementation "com.android.support:support-v13:$libraries.googleSupportVersion"
implementation "com.android.support:design:$libraries.googleSupportVersion"
implementation "com.android.support:cardview-v7:$libraries.googleSupportVersion"
implementation 'com.android.support.constraint:constraint-layout:1.1.1'
My support library version being:
ext.libraries = [
...
googleSupportVersion : '27.1.1',
...
]
Your issue seems to be that compile 'com.codesgood:justifiedtextview:1.0.2' internally is using the support library with a version different than yours. However your version is totally updated I wouldn't downgrade it just to have it match with the other, in any case you could just add a:
allprojects {
configurations.all {
resolutionStrategy.force "com.android.support:support-v4:27.0.2"
}
}
In your project gradle. You could also run gradlew app:dependencies in the Android Studio console, do a Ctrl+F on the output, search for that com.android.support:support-v4:27.0.2 and figure out from where is coming. (You only have one dependency for what I see, so there's not much science from where IT should be coming)
Let me know if this works for you.

Appcompat v7 isn't pulled into project

I inherited a project which uses appcompat-v7:20.0.0
I'm not able to build the project, because it seems that gradle doesn't include the appcompat library while synching/building.
My dependencies in the build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:20.0.0'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.google.android.gms:play-services:+'
}
Also no play services are pulled into the project, but nine old androids is(I tried including different libraries, it seemed that everything from jcenter is downloaded) As you can see in the following screenshot:
The gradle plugin is 1.0.0 and there are no problems during synching.
Are there any known solutions to such type of problem?
EDIT 1:
Android Support Repository
Android Support Library
Google Play Services
Are all installed. However it also works for newly created projects.
EDIT 2:
The ./gradlew build output:
Caused by: org.gradle.internal.UncheckedException: com.android.ide.common.internal.LoggedErrorException: Failed to run command:
/Users/stephan/Library/Android/sdk/build-tools/20.0.0/aapt package -f --no-crunch -I /Users/stephan/Library/Android/sdk/platforms/android-21/android.jar -M /Users/project-path/build/intermediates/manifests/full/flavor/beta/AndroidManifest.xml -S /Users/project-path/build/intermediates/res/flavor/beta -A /Users/project-path/build/intermediates/assets/flavor/beta -m -J /Users/project-path/build/generated/source/r/flavor/beta -F /Users/project-path/build/intermediates/res/resources-flavor-beta.ap_ --debug-mode --custom-package de.my.project -0 apk --output-text-symbols /Users/project-path/build/intermediates/symbols/flavor/beta
Error Code:
1
Output:
/Users/project-path/build/intermediates/res/flavor/beta/values/values.xml:2127: error: Error retrieving parent for item: No resource found that matches the given name 'Widget.AppCompat.Light.Base.Spinner'.
/Users/project-path/build/intermediates/res/flavor/beta/values-v16/values.xml:89: error: Error retrieving parent for item: No resource found that matches the given name 'Widget.AppCompat.Light.Base.Spinner'.
I also checked Widget.AppCompat.Light.Base.Spinner is part of the values.xml in appcompat-v7 20.0.0
Here are the sdk versions:
compileSdkVersion "Google Inc.:Google APIs:21"
buildToolsVersion "20.0.0"
defaultConfig {
minSdkVersion 10
targetSdkVersion 17
}
EDIT 3:
Project root build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
App build.gradle
apply plugin: 'com.android.application'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:20.0.0'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.google.android.gms:play-services:+'
}
android {
compileSdkVersion "Google Inc.:Google APIs:21"
buildToolsVersion "20.0.0"
defaultConfig {
minSdkVersion 10
targetSdkVersion 17
}
packagingOptions {
exclude 'META-INF/LICENSE.txt'
}
signingConfigs {
conf1 {
storeFile file("path")
storePassword ""
keyAlias ""
keyPassword ""
}
debug {
storeFile file("path")
storePassword ""
keyAlias ""
keyPassword ""
}
}
buildTypes {
debug {
zipAlignEnabled true
minifyEnabled false
proguardFile getDefaultProguardFile('proguard-android.txt')
proguardFile 'proguard-project.txt'
signingConfig signingConfigs.debug
}
release {
zipAlignEnabled true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')
proguardFiles 'proguard-rules.pro'
}
beta {
initWith debug
signingConfig signingConfigs.debug
}
}
productFlavors {
flavor1 {
applicationId "de.package"
versionCode 1
versionName "1.0"
signingConfig signingConfigs.conf1
}
}
lintOptions {
checkReleaseBuilds false
abortOnError false
}
}
I read the documentation for the Android appcompat library and it says that this library depends of v4 support library so you need include this in your project this is the link :https://developer.android.com/tools/support-library/features.html
You only need to change the dependencies in the build.gradle file and add the following line compile 'com.android.support:support-v4:22.2.1'
You can change the version(22.2.1) and put the version 20.0.0 if you want
I experience the same issue last week and resolved it by:
open Android SDK Manager
Scroll the the bottom in the section "Extras"
Check mark Android Support Repository, Android Support Library, and Google Repository
This will download the missing artifacts/jars for the libraries you're missing as a local maven repository so that gradle can find them.
Have you tried to invalidate the cache and restart the android studio?
We can add appcompat to our project in two ways,
Adding appcompat-v7 as jar file in lib folder
-First download Android Support Library from SDK manager.(If you already downloaded , u can skip this step).
-Redirect to Local SDK path\extras\android\support\v7\appcompat\libs\android-support-v7-appcompat.jar. Copy this jar file & keep it in libs folder of project.
-You have to build project, now appcompat-v7 jar file is added in your project.
-Make sure you have added this comment in build.gradle (Module:app) file of application.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
.....
}
Adding appcompat-v7 as library dependency
-File --> Project Structure --> Under Modules --> Select app
-Click Dependencies--> Click plus green color icon at top of right side--> Library dependencies --> Select appcompat-v7 from list.
-Now appcompat-v7 is added as library to your project.
Kindly try it out. If you face any problem, please let me know.
Happy coding..
Thanks.
I am working on a legacy project which is migrated from Eclipse to Android Studio and i was facing the same issue. No matter how many times I do Clean Project Build > Rebuild Invalidate and RestartSync project with gradle file It was not working for me, I even added support library as jar in libsAs it turns out in my build.gradle file there was this :
configurations {
all*.exclude group: 'com.android.support'
}
Why it was added? I don't know. Removing that fixed my issue. So guys, if doing all that and still your issue is not fixed, look for any exclude keywords in your gradle file.

NoClassDefFoundError with Android Studio on Android 4

I'm in the process of converting an existing app from Eclipse to Android Studio. However, when I run it on a device running 4.x (I've tested several versions on emulators), it immediately crashes with a NoClassDefFoundError.
I've tried commenting out references to the classes it can't find, but there's always another. As far as can tell, the offending class is always
Within my own code
An inner class (Update: With more investigation, this one isn't always true)
Everything works fine on a 5.0.1 emulator (I don't have a device to test on). My build.gradle file is fairly long, but looks like this:
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = "2.7.1"
android {
compileSdkVersion 19
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "com.myapp.android"
minSdkVersion 8
targetSdkVersion 19
multiDexEnabled = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
packagingOptions {
*snip*
}
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
repositories {
maven {
url "https://repo.commonsware.com.s3.amazonaws.com"
}
}
apt {
arguments {
androidManifestFile variant.outputs[0].processResources.manifestFile
resourcePackageName 'com.pact.android'
}
}
dependencies {
*snip compile project(':...') declarations
apt "com.googlecode.androidannotations:androidannotations:$AAVersion"
compile "com.googlecode.androidannotations:androidannotations-api:$AAVersion"
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
compile 'com.android.support:support-v4:21.0.3'
compile 'com.google.android.gms:play-services:3.1.36'
*snip many more compile declarations*
compile fileTree(dir: 'libs', include: ['*.jar'])
}
Some examples of classes that cause trouble:
An anonymous class that implements an interface inside the facebook library (as a library project)
Parcelable.CREATOR implementations in my models
An inner class that extends android.os.Library
An inner class in my own code
A class that implements an interface in a library from maven
What's going wrong, and how do I fix it?
I was incompletely implementing MultiDex support, so some of my classes weren't in the proper dex file. To fix it, you have to do more than just set multiDexEnabled = true in your defaultConfig block. You also have to:
Include compile 'com.android.support:multidex:1.0.1' in your dependencies
Have your Application class extend MultiDexApplication instead of just Application. Alternatively, you can call MultiDex.install() in attachBaseContext() of your application.
See https://developer.android.com/tools/building/multidex.html for more details.
1) Add multiDexEnabled = true in your default Config
2) Add compile com.android.support:multidex:1.0.0 in your dependencies
3) Application class extend MultiDexApplication instead of just Application
1) add multiDexEnabled = true in your defaultConfig in build.gradle.
2) You also have to Include compile 'com.android.support:multidex:1.0.1'
3) add the following to your "application" tag in your manifest:
android:name="android.support.multidex.MultiDexApplication"
its working with me , hope that help
I solved the problem by removing multiDexEnabled = true in my case.
I also issue get resolved after doing following steps
1) add multiDexEnabled = true in your defaultConfig in build.gradle.
2) You also have to Include compile com.android.support:multidex:1.0.1
3) add the following to your "application" tag in your manifest:
android:name="android.support.multidex.MultiDexApplication"
I got the same issue, tried all solutions suggested on StackOverflow but they did not work in my case.
After that I found out that one of libraries compiled by Java 8. So, I need to enable Java 8 in my project too.
android {
//...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
There are many reasons which leads to this error. Hope it helps.
In my case, it was Kotlin forEach was causing the exception.
More info here: java.lang.NoClassDefFoundError $$inlined$forEach$lambda$1 in Kotlin

Share external dependencies between modules in Android Studio

I'm trying to share 2 external dependencies between 2 modules in Android Studio.
The 2 dependencies are Twitter Core and Twitter4j (a Twitter library extension I'm experimenting with).
Here is the project structure:
Root project 'cineios-test'
+--- Project ':app'
\--- Project ':cineio-broadcast-android-sdk'
I set up the dependencies in my app build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.lgorse.cineios_test"
minSdkVersion 18
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions{
exclude 'META-INF/LICENSE.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile project(':cineio-broadcast-android-sdk')
//compile project(':cineio-broadcast-android')
//compile 'io.cine:cineio-broadcast-android-sdk:0.0.9'
compile ('org.twitter4j:twitter4j-stream:4.0.2'){
transitive = true;
}
compile('com.twitter.sdk.android:twitter:1.1.1#aar') {
transitive = true;
}
}
Here is the build.gradle file for the module, which is cineios-android-sdk:
apply plugin: 'android-library'
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
// applicationId 'io.cine.android'
minSdkVersion 18
targetSdkVersion 19
versionCode 11
versionName '0.0.11'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile 'com.google.guava:guava:16.0'
compile 'com.loopj.android:android-async-http:1.4.5'
compile 'com.android.support:appcompat-v7:20.0.0'
compile 'com.android.support:support-v4:20.0.0'
}
Finally here is settings.gradle:
include ':app', ':cineio-broadcast-android-sdk'
project(':cineio-broadcast-android-sdk').projectDir = new File('cineio-broadcast-android/cineio-broadcast-android-sdk')
I know there are answers on SO but they refer adding local libraries as modules - but since these dependencies are remote I'm not sure how to adapt the hints to this situation.
I did try adding the dependencies to the other module (cineios-android) but a) it seems ridiculous to double them up like that and b)that would imply registering a new app in the Twitter API, which will probably lead to errors.
The correct approach really is to specify the dependencies in both the main app and the module.
I did try adding the dependencies to the other module (cineios-android) but a) it seems ridiculous to double them up like that and
There's really nothing ridiculous about it. Don't think of it as trying to "share" the dependency between the main app and the module. Look at it this way: your module depends on Twitter4j and Twitter Core. If you were to reuse that module in a different application, the module should be self-contained and should be able to specify its own dependencies without the parent project needing to set them up. Making all its dependencies explicit does this.
If the parent app also depends on Twitter4j and Twitter Core, and if you use the Maven Coordinate-style remote dependency specs as you have, the build system will ensure that only one copy actually gets linked into the app, so everything will be okay.
b)that would imply registering a new app in the Twitter API, which will probably lead to errors.
I'm not familiar with how that works, so I can't comment on it other than to say that if their API is well designed, hopefully just including the library shouldn't imply something like that. If you're having problems, clarify your question or ask a new one.
In your build.gradle add dependency like below:
dependencies {
compile 'org.openimaj:twitter:1.3.1'
}
You can also use belowed link for more reference :
Gradle Please

Categories

Resources