Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, ClassNotFoundException when using "provided project " OR IllegalArgumentException: already added , when using "compile project"

I’m getting a very unusual, ClassNotFoundException at app startup for my class com.xyz.sample.QuickSampleApplication when using "provided project" in gradle dependencies script OR another error Uncaught translation error: java.lang.IllegalArgumentException: already added: when using "compile project" in gradle dependencies.

Actual Error:

E/AndroidRuntime(17749): java.lang.RuntimeException: Unable to instantiate application com.xyz.sample.QuickSampleApplication: 

java.lang.ClassNotFoundException: Didn't find class 

"com.xyz.sample.QuickSampleApplication" on path: DexPathList[[zip file

"/data/app/com.uei.sample.library-2.apk"],nativeLibraryDirectories=[/data/app-

lib/com.uei.sample.library-2, /vendor/lib, /system/lib]]

Project Explanation:

The com.xyz.sample.QuickSampleApplication is used by com.xyz.sample.QuickSampleActivity , and both classes are in the same package com.xyz.sample.

The com.xyz.sample.QuickSampleApplication uses a JAR called ‘quicksdklibrary’ , that I included as a "provided dependency" in gradle, see gradle snippet below:

dependencies {
    provided project(':quicksdklibrary')
    //OR provided fileTree(dir: 'libs/MyExternalLibs', include: '*.jar')
    compile 'com.android.support:appcompat-v7:21.0.0'
}

The JAR actually resolves in the IDE, builds without error, and the JAR’s internal classes are visible to my app classes such as my:

com.xyz.sample.QuickSampleApplication OR com.xyz.sample.QuickSampleActivity.

Both com.xyz.sample.QuickSampleApplication And com.xyz.sample.QuickSampleActivity are declared/spelled correctly in the AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.xyz.sample.library"
      android:versionCode="1"
      android:versionName="1.0.002">
    <uses-sdk android:minSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application android:icon="@drawable/icon" android:label="@string/app_name"
        android:name="com.xyz.sample.QuickSampleApplication">
        <activity android:name="com.xyz.sample.QuickSampleActivity" android:screenOrientation="portrait"
                  android:label="@string/app_name" android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


    </application>
</manifest>

When I opened the actual generated .apk file I found that my com.xyz... classes were not being packaged in the .apk, why???

I tried including the source explicitly in the dependencies tag by:

compile fileTree(dir: 'src/main/java', include: ['*'])

OR

compile fileTree(dir: 'src/main/java/com/xyz', include: ['*'])

OR

compile fileTree(dir: 'src/main/java/com/xyz', include: ['*.java'])

OR

 compile fileTree(dir: 'src/main/java', include: ['*.java'])

OR

 compile file(libs/quicksdklibrary.jar)

Based on comments also tried using sourceSets tags:

OR using sourceSets tag:

sourceSets {

        main{
           java {
                java.srcDirs 'src/main/java'
            }

        }

    }

OR

  sourceSets {

        main{
           java {
                java.srcDirs 'src/main/java/com/xyz'
            }

        }

    }

However the generated ".apk" still does not contain the class packages com.xyz...

Latest update on the problem, please see below:

Alternative attempt #1:

When I use this tag in my gradle

dependencies {
    compile 'com.android.support:appcompat-v7:23.0.1'

    provided project(':quicksdklibrary')
}

Then I get ClassNotFoundException (inspecting the apk & class classes.dex confirms my quicksdklibrary classes are not being included) , however when I use:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    // OR compile files(libs/:quicksdklibrary.jar)
    // OR compile project(':quicksdklibrary')
    compile 'com.android.support:appcompat-v7:23.0.1'

}

Then i get this other error, which wont even allow me to deploy/install the apk:

:app:preDexDebug

Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/xyz/ace/ACEUtils$Creator;

Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/xyz/ace/AEUtils;

Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/xyz/ace/AEngine;

Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/xyz/ace/ATag;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/xyz/ace/ABehavior;

Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/xyz/ace/AContext;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/xyz/ace/AControl$List;

Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/xyz/ace/AControl;

Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/xyz/ace/AData;

UNEXPECTED TOP-LEVEL EXCEPTION:

Error:Execution failed for task ':app:preDexDebug'.

com.android.ide.common.process.ProcessException: 

org.gradle.process.internal.ExecException: Process 'command 'C:\Program 
Files\Java\jdk1.7.0_79\bin\java.exe'' finished with non-zero exit value 2

**Alternative attempts: **

I really checked the entire project and also recreated the project several times in order to make sure that I was not including this JAR several times, however I'm still getting the same error "Uncaught translation error: java.lang.IllegalArgumentException: already added..."

I also reinstalled Android SDK, and also update my JDK to jdk1.7.0_80, however Im still getting this error.

I look at other posts on stackoverflow mentioning that I should remove the reference to support library version 4 or 7, which I have also tried.

Other attempts:

I also check the actual JAR to see if multiple classes with same name in the same package were created, which are not.

I also tried adding obfuscated version, which will have different class names, but the obfuscated version of the JAR had the "Uncaught translation error: java.lang.IllegalArgumentException: already added:.." error!!!

Question:

How do I resolve this "Uncaught translation error: java.lang.IllegalArgumentException: already added:" , why does app:preDexDebug think there is multiple version of my classes or tries to add multiple versions???

Thanks a million to all!

like image 605
cyber101 Avatar asked Sep 28 '15 23:09

cyber101


1 Answers

I had the same issue, but the answer was related to the support library.

    compile 'com.android.support:appcompat-v7:21.0.0'

Try creating a new application without support library, or simply try to remove on this one and see what happens.

UPDATE (you should also check if this is there):

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
}
like image 140
Iliiaz Akhmedov Avatar answered Nov 14 '22 23:11

Iliiaz Akhmedov