Android Studio运行集成Mapbox的Android应用时Logcat出现大量报错的原因咨询
Hey there! Let's tackle the two main issues you're seeing with your Mapbox implementation step by step.
1. Resolving Conscrypt Hidden Method Access Warnings
Those Accessing hidden method warnings pop up because the older Mapbox SDK version (9.0.0) you're using relies on internal Android system APIs (from Conscrypt, the SSL library) that are restricted starting from Android 11. Here's how to fix this:
Steps to Fix:
- Upgrade Mapbox SDK to a newer stable version: Version 9.0.0 is quite outdated. Mapbox has fixed this hidden API issue in later releases. I recommend upgrading to the latest 9.x stable version (9.7.2) — it's a safe drop-in fix without breaking changes.
- Remove unused dependencies: You've included
com.google.android.gms:play-services-mapsbut aren't using Google Maps. Removing this avoids potential dependency conflicts. - Update project repositories: The
mapbox.bintray.comrepo is no longer active (Bintray was shut down). Mapbox now hosts packages on Maven Central, which you already have in yourallprojectsrepositories, so you can delete that old Maven entry.
Modified Module-level build.gradle Dependencies:
dependencies { // Upgrade to latest 9.x stable Mapbox SDK implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:9.7.2' implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-locationlayer:0.11.0' // Match telemetry version to SDK (6.2.0 is compatible with 9.7.2) implementation 'com.mapbox.mapboxsdk:mapbox-android-telemetry:6.2.0' implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.1' // Remove unused Google Maps dependency // implementation 'com.google.android.gms:play-services-maps:17.0.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' }
2. Fixing SecurityException: getDataNetworkTypeForSubscriber
This error comes from Mapbox Telemetry trying to access network type information without proper permissions. Since your targetSdk is 31, you need to handle runtime permissions correctly.
Options to Resolve:
Option 1: Request Runtime Permission for READ_PHONE_STATE
Add permission request logic to your MainActivity to ask the user for the required permission:
import android.Manifest; import android.content.pm.PackageManager; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; public class MainActivity extends AppCompatActivity { private static final int REQUEST_PHONE_STATE_PERMISSION = 101; private MapView mapView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Request READ_PHONE_STATE permission if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_PHONE_STATE_PERMISSION); } Mapbox.getInstance(this, getString(R.string.mapbox_access_token)); setContentView(R.layout.activity_main); mapView = findViewById(R.id.mapView); mapView.onCreate(savedInstanceState); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_PHONE_STATE_PERMISSION) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission granted, telemetry will work normally } else { // Permission denied, telemetry will be limited but map functionality remains intact } } } // Rest of your lifecycle methods... }
Option 2: Disable Mapbox Telemetry (if you don't need it)
If you don't use Mapbox's telemetry features, you can disable it entirely to avoid the permission issue:
- Add to Manifest:
<application> <!-- Disable Mapbox Telemetry --> <meta-data android:name="com.mapbox.mapboxsdk.telemetry.enabled" android:value="false" /> <!-- Your existing activity declaration --> </application>
- Or set programmatically:
Add this line beforeMapbox.getInstance()inonCreate:
com.mapbox.mapboxsdk.telemetry.MapboxTelemetry.getInstance().setTelemetryEnabled(false);
Final Notes
After making these changes, clean and rebuild your project. The warnings should disappear, and the security exception will be resolved either by granting the permission or disabling telemetry.
内容的提问来源于stack exchange,提问作者ryanongwx

