rahadur / capacitor-admob

This is Ionic Capacitor native AdMob plugin for IOS & Android
https://xplatform.app
MIT License
77 stars 28 forks source link

Android Error - package android.support.design.widget does not exists #61

Open JesusJoseph opened 4 years ago

JesusJoseph commented 4 years ago

I try to setup Admob for android but I am getting error as "package android.support.design.widget does not exists" in AdMob.java Working perfectly with ios but having this issue for last 2 days but couldn't fix it. Any guidance will be appreciated. Thanks

Followed the below steps to get this error.

  1. create new ionic 5 project

  2. npx cap add android

  3. Install capacitor-admob plugin using below command npm install --save capacitor-admob

  4. ionic build

  5. npx cap copy 6.npx cap open android -> this opens the project in Android Studio.

  6. Build the project and getting error as below

../node_modules/capacitor-admob/android/src/main/java/app/xplatform/capacitor/plugins/AdMob.java package android.support.design.widget does not exist package CoordinatorLayout does not exist package CoordinatorLayout does not exist package CoordinatorLayout does not exist package CoordinatorLayout does not exist

### build.gridle content

apply plugin: 'com.android.application'

android { compileSdkVersion rootProject.ext.compileSdkVersion defaultConfig { applicationId "io.ionic.starter" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } }

repositories { flatDir{ dirs '../capacitor-cordova-android-plugins/src/main/libs', 'libs' } }

dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion" implementation project(':capacitor-android') testImplementation "junit:junit:$junitVersion" androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion" androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion" implementation project(':capacitor-cordova-android-plugins') }

apply from: 'capacitor.build.gradle'

try { def servicesJSON = file('google-services.json') if (servicesJSON.text) { apply plugin: 'com.google.gms.google-services' } } catch(Exception e) { logger.warn("google-services.json not found, google-services plugin not applied. Push Notifications won't work") }

NP-Systems commented 4 years ago

Same problem a few weeks ago.

I suppose the issue below is useful for you. https://github.com/rahadur/capacitor-admob/issues/35.

If your app will crash after the fix, check my issue. https://github.com/rahadur/capacitor-admob/issues/59

Good luck!

JesusJoseph commented 4 years ago

Same problem a few weeks ago.

I suppose the issue below is useful for you.

35.

If your app will crash after the fix, check my issue.

59

Good luck!

Hi awesomerainbows, thanks for your reply.

I tried as mentioned in issue #35 but I am getting a similar error as below. package androidx.coordinatorlayout.widget does not exists

STEPS I did now,

STEP 1 Change the import statement in AdMob.java as below from //import android.support.design.widget.CoordinatorLayout; to import androidx.coordinatorlayout.widget.CoordinatorLayout;

STEP 2 add below 2 lines in build.gradle

implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0' implementation 'androidx.appcompat:appcompat:1.1.0'

But still getting compile-time error.

Any help will be appreciated.

Thanks,

spencerpauly commented 4 years ago

I get the same exact thing ^

NP-Systems commented 4 years ago

On Step2 did you add the lines on build.gradle for capacitor-admob, not app build.gradle ?

JesusJoseph commented 4 years ago

On Step2 did you add the lines on build.gradle for capacitor-admob, not app build.gradle ?

Thanks for your help. Sorry, I missed that, now I did step 2 correctly the app open successfully.

BUT the APP is CRASH when I try to open the banner. From the log, I can see the app crashed with null pointer exception from below line inside AdMob.java mViewGroup.addView(mAdViewLayout);

Can you please help me to solve this issue. thanks for your help.

### FULL EXCEPTION

2020-06-18 12:52:34.716 542-542/io.ionic.starter E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.ionic.starter, PID: 542
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.addView(android.view.View)' on a null object reference
        at app.xplatform.capacitor.plugins.AdMob$2.run(AdMob.java:208)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2020-06-18 12:52:34.808 542-542/io.ionic.starter I/Process: Sending signal. PID: 542 SIG: 9

Also, add below 2 lines in AndroidManifest.xml

AhSem commented 4 years ago

I recently upgrade to AndroidX as well, and encounter a series of issues related to this. Let me summarise what I encountered and what I did to fix.

  1. After migrate to AndroidX, I am getting error I am getting error as "package android.support.design.widget does not exists" in AdMob.java. So what I did is a. Change the import statement in AdMob.java as below from import android.support.design.widget.CoordinatorLayout; to import androidx.coordinatorlayout.widget.CoordinatorLayout; This AdMob.java file is located at node_modules/capacitor_admob, and therefore every time i reinstall node_modules, I will have to redo this step. b. In the admob build.gradle (located at node_modules/capacitor_admob), I added implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0'

  2. Step 1 actually fix the issue with this plugin, but because in my application, I do have other capacitor plugins and those are giving me the same error. So to do that, I search this entire application for android.support.design.widget.CoordinatorLayout; and replace them with androidx.coordinatorlayout.widget.CoordinatorLayout; One thing to take note is that these changes are in node_modules files, and therefore not committed to source control (for my case). Therefore, every time node_modules are reinstalled, I will need to re do all these manually.

darkkaze commented 4 years ago

On Step2 did you add the lines on build.gradle for capacitor-admob, not app build.gradle ?

Thanks for your help. Sorry, I missed that, now I did step 2 correctly the app open successfully.

BUT the APP is CRASH when I try to open the banner. From the log, I can see the app crashed with null pointer exception from below line inside AdMob.java mViewGroup.addView(mAdViewLayout);

Can you please help me to solve this issue. thanks for your help.

### FULL EXCEPTION

2020-06-18 12:52:34.716 542-542/io.ionic.starter E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.ionic.starter, PID: 542
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.addView(android.view.View)' on a null object reference
        at app.xplatform.capacitor.plugins.AdMob$2.run(AdMob.java:208)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2020-06-18 12:52:34.808 542-542/io.ionic.starter I/Process: Sending signal. PID: 542 SIG: 9

Also, add below 2 lines in AndroidManifest.xml

you are missing the AdMob.initialize("your-key") before the showBanner. i spend three hours in the same stupid thing. LOL

drugoi7 commented 4 years ago

On Step2 did you add the lines on build.gradle for capacitor-admob, not app build.gradle ?

Thanks for your help. Sorry, I missed that, now I did step 2 correctly the app open successfully. BUT the APP is CRASH when I try to open the banner. From the log, I can see the app crashed with null pointer exception from below line inside AdMob.java mViewGroup.addView(mAdViewLayout); Can you please help me to solve this issue. thanks for your help. ### FULL EXCEPTION

2020-06-18 12:52:34.716 542-542/io.ionic.starter E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.ionic.starter, PID: 542
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.addView(android.view.View)' on a null object reference
        at app.xplatform.capacitor.plugins.AdMob$2.run(AdMob.java:208)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2020-06-18 12:52:34.808 542-542/io.ionic.starter I/Process: Sending signal. PID: 542 SIG: 9

Also, add below 2 lines in AndroidManifest.xml

you are missing the AdMob.initialize("your-key") before the showBanner. i spend three hours in the same stupid thing. LOL

I have the same issue. I added the AdMob.initialize("your-key"), but still there.

inspire22 commented 4 years ago

There's a npm to help update plugins to android X https://github.com/mikehardy/jetifier

npm install jetifier
npx jetify
npx cap sync android