0

I know this question has been asked a million times and yet, I couldn't find the answer to my specific situation. I have a library which has all of the code, and a couple of other modules that import the library.

-project
--mylibrary
---sr/main/java
----co/android/mylibrary
----BaseApp (extends MultidexApp)
--Application1
---sr/main/java
----co/android/app2
-----Android Manifest
--Application2
---sr/main/java
----co/android/app2
-----Android Manifest

And both the manifests use the base app like this.

<application
    android:name="co.android.mylibrary.BaseApp"
    android:allowBackup="false"
    android:fullBackupContent="false"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/AppTheme"
    tools:replace="android:icon,android:theme, android:allowBackup">

And the build dependencies look like this:

dependencies {
  releaseCompile project(path: ':mylibrary', configuration: 'release')
  debugCompile project(path: ':mylibrary', configuration: 'debug')
}

My base app's method to initialize multidex:

protected void attachBaseContext(Context base) {   
    super.attachBaseContext(base);
    try {
      MultiDex.install(this);
    }catch (RuntimeException e){}
}

Some of my proguard rules, that I've added at both locations (library and the application). These don't include some rules for 3rd party libraries and some of my own classes.

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.view.View 
-keep public class co.android.mylibrary.data.greendao.**{ *; }

So the app runs fine on my s8, but doesn't on some phones like the moto G. They would also run fine if Proguard is enabled and its shrinking resources, like for release builds. Another strange behavior I noticed is that, when I set my breakpoint on some parts of my code and run the release builds (with debuggable set to true), it would break on the s8, but not on the moto.

Why this strange behavior? Another question that I found super similar is unable to instantiate application - ClassNotFoundException. But still no resolution.

This is the complete log of the error. Ignore the package names. Exception log

Edit

After changing the way I compile the library on my application, based on suggestiongs from @Mostafa Anter:

compile project(path: ':mylibrary', configuration: 'debug')

It started giving me this error. I have my instant run turned off.

Class not found exception on Firebase.initProvider

1
  • can you show logcat details of ClassNotFoundException? Commented Sep 16, 2017 at 20:55

2 Answers 2

1

make base class extend Application then inside onCreate method call this lineMultiDex.install(this);

Modify the module-level build.gradle file to enable multidex and add the multidex library as a dependency, as shown here:

android {
defaultConfig {
    ...
    minSdkVersion 15 
    targetSdkVersion 26
    multiDexEnabled true
}
...
}

dependencies {
    compile 'com.android.support:multidex:1.0.1'
}
Sign up to request clarification or add additional context in comments.

7 Comments

are you using proguard ? if yes provide us with rules
try add this line to progaurd -keep public class your_missing_class_full_name
No. That didn't help. I added something like -keep public class co.android.mylibrary.BaseApp. Also, this is on debug builds, I don't think it even matters, since shrinking is disabled.
With shrinking enabled, it runs the app. But doesn't break at some codes, where I want it to, which is causing strange behavior on some devices.
try to replace releaseCompile project(path: ':mylibrary', configuration: 'release') debugCompile project(path: ':mylibrary', configuration: 'debug') with compile project(':mylibrary')
|
0

if you use JavaVersion.VERSION_1_8 please be sure that you use it in all modules

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.