In clinical practice, heart arrhythmias are manually diagnosed by a doctor, which is a time-consuming process. Furthermore, this process is error-prone due to noise from the recording equipment and biological non-idealities of patients. Thus, an automated arrhythmia classifier would be time and cost-effective as well as offer better generalization across patients. In this paper, we propose an adversarial multitask learning method to improve the generalization of heartbeat arrythmia classification. We built an end-to-end deep neural network (DNN) system consisting of three sub-networks; a generator, a heartbeat-type discriminator, and a subject (or patient) discriminator. Each of these two discriminators had its own loss function to control its impact. The generator was "friendly" to the heartbeat-type discrimination task by minimizing its loss function and "hostile" to the subject discrimination task by maximizing its loss function. The network was trained using raw ECG signals to discriminate between five types of heartbeats - normal heartbeats, right bundle branch blocks (RBBB), premature ventricular contractions (PVC), paced beats (PB) and fusion of ventricular and normal beats (FVN). The method was tested with the MIT-BIH arrhythmia dataset and achieved a 17% reduction in classification error compared to a baseline using a fully-connected DNN classifier.Clinical Relevance-This work validates that it is possible to develop a subject-independent automated heart arrhythmia detection system to assist clinicians in the diagnosis process.