Compiling errors at API level 28

2020-04-15 14:46发布

问题:

I'm trying to compile an .apk from source code at API level 28 (compileSdkVersion 28). The app compiles fine but after installation it crashes. My app has Admob Ads in the UI itself on various pages.

Here's my project-level build.gradle -

    buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
        classpath 'com.google.gms:google-services:4.2.0'
    }
}

allprojects {
    repositories {
        jcenter()
        google()
    }
}

Here's my app-level build.gradle -

apply plugin: 'com.android.application'

android {
    useLibrary 'org.apache.http.legacy'
    compileSdkVersion 28

    defaultConfig {
        applicationId "****APP_ID****"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.android.gms:play-services-ads:17.2.1'
    implementation 'com.android.support:customtabs:27.0.2'
    implementation 'com.google.android.gms:play-services-maps:+'
    implementation 'com.google.code.gson:gson:2.8.0'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation 'com.squareup.okhttp3:okhttp:3.9.1'
}
apply plugin: 'com.google.gms.google-services'

Here's the Ad code from my AndroidManifest.xml -

 <application>
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="*****ADMOB-ID*****"/>
</application>

Here's the Ad code from my MainActivity.java

import com.google.android.gms.ads.MobileAds;
public class MainActivity extends Activity {


      private LinearLayout linear1;
      private AdView adview1;
      @Override
      protected void onCreate(Bundle _savedInstanceState) {
         super.onCreate(_savedInstanceState);
         setContentView(R.layout.main);
         initialize(_savedInstanceState);
         initializeLogic();
         MobileAds.initialize(this, "*****ADMOB-ID*****");
     }

I'm relatively new to Android coding. What might be the problem?

回答1:

You have to replace your application ID

 defaultConfig {
    applicationId "****APP_ID****"
    minSdkVersion 16
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
}

you have to add version

  implementation 'com.google.android.gms:play-services-maps:+'

you have to add admob app id here

   <application>
<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="*****ADMOB-ID*****"/>
</application>

and here

  MobileAds.initialize(this, "*****ADMOB-ID*****");

first try to fix these errors



回答2:

You have to disable org.apache.http.legacy from your application so add below lines in manifest

 <uses-library
  android:name="org.apache.http.legacy"
  android:required="false" />

remove useLibrary 'org.apache.http.legacy' from gradle

If you are using any http url in your application, refer this



回答3:

If you use an url in your code, just use "https://theTargetUrl..." in your url instead of "http://theTargetUrl..." and that's all! It should work like a charm.