奇怪的SupportMapFragment行为

时间:2016-06-14 07:54:41

标签: android google-maps android-ndk java-native-interface google-maps-android-api-2

如果我们使用System.loadLibrary()使用我们的原生图书馆,那么SupportMapFragment只会在启动后的几秒钟内杀死activity。没有任何堆栈跟踪就可以做到这一点。这是我收集的最佳日志:

06-10 02:10:01.063 22787-23766/com.myapp.staging E/DynamiteModule: Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.android.gms.googlecertificates.ModuleDescriptor" on path: DexPathList[[zip file "/data/app/com.myapp.staging-1/base.apk"],nativeLibraryDirectories=[/data/app/com.myapp.staging-1/lib/arm, /data/app/com.myapp.staging-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
06-10 02:10:01.063 22787-23766/com.myapp.staging W/ResourcesManager: getTopLevelResources: /data/app/com.google.android.gms-1/base.apk / 1.0 running in com.myapp.staging rsrc of package com.google.android.gms
06-10 02:10:01.063 22787-23766/com.myapp.staging I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:1
06-10 02:10:01.063 22787-23766/com.myapp.staging I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 1
06-10 02:10:01.153 23793-23793/? I/dex2oat: /system/bin/dex2oat --debuggable --compiler-filter=speed --dex-file=/data/user/0/com.google.android.gms/app_chimera/m/00000000/DynamiteModulesA_GmsCore_prodmnc_alldpi_release.apk --oat-file=/data/dalvik-cache/arm/data@user@0@com.google.android.gms@app_chimera@m@00000000@DynamiteModulesA_GmsCore_prodmnc_alldpi_release.apk@classes.dex
06-10 02:10:01.153 23793-23793/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@user@0@com.google.android.gms@app_chimera@m@00000000@DynamiteModulesA_GmsCore_prodmnc_alldpi_release.apk@classes.dex: Permission denied
06-10 02:10:01.153 23793-23793/? I/dex2oat: dex2oat took 721.250us (threads: 4) 
06-10 02:10:01.153 5853-5853/? E/audit: type=1701 msg=audit(1465506601.153:5185): auid=4294967295 uid=10329 gid=10329 ses=4294967295 subj=u:r:untrusted_app:s0:c512,c768 pid=23766 comm="androidmapsapi-" reason="memory violation" sig=11
06-10 02:10:01.153 3542-4463/? D/libgps: OnGpsExtensionMessage: message_id(1), data(0x7f95455e44), size(2432)
06-10 02:10:01.153 3542-4463/? D/libgps: GpsiHook: API: gpsSvStatusCb(GpsiHookStateGps: e,c,g)
06-10 02:10:01.153 3542-4463/? D/libgps: proxy__gps_sv_status_cb : called
06-10 02:10:01.173 3542-3607/? I/PassiveLocTracker: GPS Satllite Status
06-10 02:10:01.243 3542-4368/? I/WindowState: WIN DEATH: Window{ae4e596 u0 d0 com.myapp.staging/com.myapp.ui.activity.FrameActivity}
06-10 02:10:01.243 3067-4605/? D/libEGL: eglTerminate EGLDisplay = 0x7f6bae1f88
06-10 02:10:01.243 3067-4605/? D/libEGL: eglTerminate EGLDisplay = 0x7f6bae1f88
06-10 02:10:01.243 3542-4286/? D/GraphicsStats: Buffer count: 21
06-10 02:10:01.253 3542-4368/? D/InputDispatcher: Focus left window: 22787
06-10 02:10:01.253 3542-4639/? I/AppOps: sendInfoToFLP, code=41 , uid=10329 , packageName=com.myapp.staging , type=finishOp
06-10 02:10:01.253 3542-4120/? I/ActivityManager: Process com.myapp.staging (pid 22787)(adj 0) has died(114,1020)
06-10 02:10:01.253 3542-3564/? I/WindowState: WIN DEATH: Window{6ba02d0 u0 d0 com.myapp.staging/com.myapp.ui.activity.VisitActivity}
06-10 02:10:01.253 3542-4227/? I/AppOps: sendInfoToFLP, code=42 , uid=10329 , packageName=com.myapp.staging , type=finishOp
06-10 02:10:01.263 3542-4120/? D/ActivityManager: isAutoRunBlockedApp:: com.myapp.staging, Auto Run ON
06-10 02:10:01.263 3542-4120/? W/ActivityManager: Force removing ActivityRecord{6796427 u0 com.myapp.staging/com.myapp.ui.activity.FrameActivity t5324}: app died, no saved state

我花了几天时间试图了解导致它的原因,似乎我们删除System.loadLibrary()调用,即不使用我们的原生lib,然后SupportMapFragment正常工作。有没有办法解决它而不删除我们的原生lib?

修改 我创建了一个示例应用程序来重现这个问题,在上面提到的日志之后我也得到了一些似乎有用的东西:

06-14 13:17:28.415 5756-5756/? E/audit: type=1701 msg=audit(1465892248.405:4500): auid=4294967295 uid=10332 gid=10332 ses=4294967295 subj=u:r:untrusted_app:s0:c512,c768 pid=6431 comm="androidmapsapi-" reason="memory violation" sig=11

EDIT2:

MapsActivity.java

public class MapsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Go.init(this);
    setContentView(R.layout.activity_maps);
}

public void showMap(View view) {
    getSupportFragmentManager().beginTransaction().replace(R.id.container, new MapFragment()).commit();
}
}

MapFragment.java

public class MapFragment extends android.support.v4.app.Fragment implements OnMapReadyCallback {

private GoogleMap mMap;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.map_frag, null, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
            .findFragmentById(R.id.maps);
    mapFragment.getMapAsync(this);

}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

Go.java

public final class Go {
// init loads libgojni.so and starts the runtime.
public static void init(final Context ctx) {
    if (Looper.myLooper() != Looper.getMainLooper()) {
        Log.wtf("Go", "Go.init must be called from main thread (thread="+Thread.currentThread().getName()+")");
    }
    if (running) {
        return;
    }
    running = true;

    // TODO(crawshaw): context.registerComponentCallbacks for runtime.GC

    System.loadLibrary("gojni");
    Go.run(ctx);
    new Thread("GoReceive") {
        public void run() { Seq.receive(); }
    }.start();
}

private static boolean running = false;

private static native void run(Context ctx);
}

这是文件夹结构:

enter image description here

1 个答案:

答案 0 :(得分:0)

据我所知,有关内存泄漏的gmaps-api问题中记录了问题,最新版本为issue 8596。回顾其他相关问题,这已在issue 4766中提到的最新版Google地图Android API中得到修复,但是,在其他版本中仍遇到同样的问题。

除此之外,MapFragment还提到了因为视图无法释放而导致超出视图生命周期的对象时导致的内存泄漏。

而且,这个SO帖子 - Google Maps Android API v2 SupportMapFragment memory leak也可能会有所帮助。