Click here to Skip to main content
15,867,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

My app size is more than 150 MB because it contains 3 pdfs and play store dont let me upload my aap bundle.

I saw android Integrate Asset Delivery and followed the step the way below, now I dont know whether I followed the steps correctly or if I made any error coping and pasting the code. Please help.

I created a directory named assetspacks, then created subdirectories src/main/assets in it and then copied the code to my app.

Please help me, it also gives me an error:

Attempt to invoke interface method 'com.google.android.gms.tasks.Task com.google.android.play.core.assetpacks.AssetPackManager.getPackStates(java.util.List)' on a null object reference


Please help.

What I have tried:

Java
AssetPackManager assetPackManager;
AssetPackStateUpdateListener assetPackStateUpdateListener;

ConnectivityManager connManager;
NetworkInfo  mWifi;
boolean waitForWifiConfirmationShown;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final String assetPackName = "assetspacks";
        connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(mWifi.isConnected()){
    waitForWifiConfirmationShown = false;
}
else
{
    waitForWifiConfirmationShown = true;
}
        assetPackManager.getPackStates(Collections.singletonList(assetPackName))
                .addOnCompleteListener(new OnCompleteListener<AssetPackStates>() {
                    @Override
                    public void onComplete(Task<AssetPackStates> task) {
                        AssetPackStates assetPackStates;
                        try {
                            assetPackStates = task.getResult();
                            AssetPackState assetPackState =
                                    assetPackStates.packStates().get(assetPackName);
                        } catch (RuntimeExecutionException e) {
                            Log.d("MainActivity", e.getMessage());
                            return;
                        }
                    }
                        });


        assetPackStateUpdateListener = new AssetPackStateUpdateListener() {
            @Override
            public void onStateUpdate(AssetPackState assetPackState) {
                switch (assetPackState.status()) {
                    case AssetPackStatus.PENDING:
                        Log.i(TAG, "Pending");
                        break;

                    case AssetPackStatus.DOWNLOADING:
                        long downloaded = assetPackState.bytesDownloaded();
                        long totalSize = assetPackState.totalBytesToDownload();
                        double percent = 100.0 * downloaded / totalSize;

                        Log.i(TAG, "PercentDone=" + String.format("%.2f", percent));
                        break;

                    case AssetPackStatus.TRANSFERRING:
                        // 100% downloaded and assets are being transferred.
                        // Notify user to wait until transfer is complete.
                        break;

                    case AssetPackStatus.COMPLETED:
                        // Asset pack is ready to use. Start the game.
                        break;

                    case AssetPackStatus.FAILED:
                        // Request failed. Notify user.
                        Log.e(TAG, String.valueOf(assetPackState.errorCode()));
                        break;

                    case AssetPackStatus.CANCELED:
                        // Request canceled. Notify user.
                        break;

                    case AssetPackStatus.WAITING_FOR_WIFI:
                        if (!waitForWifiConfirmationShown) {
                            assetPackManager.showCellularDataConfirmation(MainActivity.this)
                                    .addOnSuccessListener(new OnSuccessListener<Integer>() {
                                        @Override
                                        public void onSuccess(Integer resultCode) {
                                            if (resultCode == RESULT_OK) {
                                                Log.d(TAG, "Confirmation dialog has been accepted.");
                                            } else if (resultCode == RESULT_CANCELED) {
                                                Log.d(TAG, "Confirmation dialog has been denied by the user.");
                                            }
                                        }
                                    });
                            waitForWifiConfirmationShown = true;
                        }
                        break;

                    case AssetPackStatus.NOT_INSTALLED:
                        // Asset pack is not downloaded yet.
                        break;
                    case AssetPackStatus.UNKNOWN:
                        Log.wtf(TAG, "Asset pack status unknown");
                        break;
                }
            }
        };


        setContentView(R.layout.activity_main);
Posted
Comments
David Crow 1-Aug-22 8:33am    
assetPackManager is null.
nyt1972 1-Aug-22 9:32am    
I added this:

String fastFollowAssetPackPath = getAbsoluteAssetPath(assetPackName, "");

        if (fastFollowAssetPackPath == null) {
            assetPackManager.registerListener(assetPackStateUpdateListener);
            List<String> assetPackList = new ArrayList<>();
            assetPackList.add(assetPackName);
            assetPackManager.fetch(assetPackList);
        }


but it say
Attempt to invoke interface method 'com.google.android.play.core.assetpacks.AssetPackLocation com.google.android.play.core.assetpacks.AssetPackManager.getPackLocation(java.lang.String)' on a null object reference
Richard Deeming 2-Aug-22 4:28am    
Read David's comment again: assetPackManager is null.

You cannot call a method on a null instance and expect it to do anything except throw an exception.

You've missed some code somewhere that initializes that field.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900